In hibernate environment there would be one class_name.hbm.xml file for each entity/object. This file will be used to map an object with the corresponding table in database.
For example - Person.hbm.xml would be used to map the table 'PERSON' in database which stores the Person objects.
Below is a simple example to write hbm.xml file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="path.to.the.class.package.Person" table="PERSON">
<id name="lastName" type="string" unsaved-value="null" >
<column name="L_NAME" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="firstName" column="F_NAME" type="string"/>
<property name="age" column="AGE" type="integer"/>
<property name="city" column="CITY" type="string"/>
<property name="pinCode" column="PIN" type="integer"/>
</class>
</hibernate-mapping>
To make hibernate to use this file or to load this file in environment you need to specify the property for this in hibernate.cfg.xml as below
<mapping resource="path/to/the/hbmXml/file/Person.hbm.xml">
In next section we will see what is the next thing to learn to start the Hibernate development.
Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts
Wednesday, January 6, 2010
Tuesday, January 5, 2010
Simple hibernate.cfg.xml file example
Here I'm giving an example to right a simple hibernate.cfg.xml file.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- jdbc connection properties -->
<property name="hibernate.connection.datasource">myDataSourceName</property>
<property name="connection.driver_class">properDriverForDatabase</property>
<property name="connection.url">databaseURL</property>
<!-- for example : jdbc:oracle:thin:@myHost:port:orcl-->
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="path/to/the/hbmXml/file/fileName.hbm.xml">
</session-factory>
</hibernate-configuration>
We will see in coming posts about the mandatory properties and optional properties and what they stands for.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- jdbc connection properties -->
<property name="hibernate.connection.datasource">myDataSourceName</property>
<property name="connection.driver_class">properDriverForDatabase</property>
<property name="connection.url">databaseURL</property>
<!-- for example : jdbc:oracle:thin:@myHost:port:orcl-->
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="path/to/the/hbmXml/file/fileName.hbm.xml">
</session-factory>
</hibernate-configuration>
We will see in coming posts about the mandatory properties and optional properties and what they stands for.
Subscribe to:
Posts (Atom)
SpringBoot: Features: SpringApplication
Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging · To add additional logging during startup...
-
There was a requirement in which user should be able to download an excel report from application, make some amendments and upload the exce...
-
As we all know, one of the key features introduced in Java 8 is functional interface . Functional can have as many as default and static me...
-
I was facing 'java.rmi.NoSuchObjectException: Bean has been deleted' for the stateful bean instance. While reading about the error, ...