Java Persistence with Hibernate Part 4 – Create the Mapping File and Configuration File
We need to tell Hibernate how our persistent class(Student.java) maps to our table(student_table) for Hibernate to be able to load and persist(save) data.
Create the Mapping File
The basic structure of a mapping file is like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.phoenixone.hibernate.model">
<class name="com.tutorial.enrollment.model.Student" table="student">
<id name="id" column="student_id">
<generator class="org.hibernate.id.enhanced.SequenceStyleGenerator" />
</id>
<property name="firstName" column="first_name" />
<property name="lastName" column="last_name" />
</class>
</hibernate-mapping>
The DOCTYPE is required in the mapping file. You can use it for auto-completion of the XML elements of the mapping file. The DTD is included in hibernate3.jar.
<hibernate-mapping> is the root element of the XML mapping file. You need to provide the package where the class belongs.
<class> tags is used for mapping the persistent class and other classes to which your persistent class is dependent.
e.g. If we have an Address property
private Address address;inside the Student class then we need to create another <class> tag for Address but that’s another topic.
The <id> tag is used to mapped the primary key in the table to the property in the class.
The <generator> tag provides auto-generation of the id. You can choose from a list of generator classes or short names which you can find here. You can also plugin your own generator strategy.
The <property> tag is used to map the other properties of the class. Just make sure that the value of name attribute has the same casing as you used in the class file.
After creating the mapping file save it as Student.hbm.xml and put it on the same directory as Student.java. All mapping files should have the same name as the class it maps and ends with .hbm.xml.
Create the Hibernate Configuration File
Ok, at this point we already have our database(enrollment) and persistent class(Student.java) which was done in Part 3 and earlier in this post we have the mapping file for Student.java(Student.hbm.xml). There’s only one thing left to configure so we can store and load objects with Hibernate and that is tell Hibernate our database configuration, how to locate the mapping files(*.hbm.xml), etc.
Here’s how our configuration file may look like…
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/enrollment</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/tutorial/enrollment/model/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
The DOCTYPE here is different from the mapping file’s DOCTYPE.
<hibernate-configuration> is the root element of the file.
<session-factory> allows you to configure Hibernate’s SessionFactory which is the one responsible for a specific database. The SessionFactory is also the one responsible for creating Session objects that will allow us to store and load objects as we will see in Part 5. If you have several databases then you should have several <session-factory> elements too.
The first four properties take care of our database connection. You can search for your particular database’s connection configuration by searching on how to use the JDBC driver for it. As for MySQL it’s here.
The dialect property determines what particular SQL variant Hibernate will generate depending on your database. For other dialects go here.
The show_sql property tells Hibernate to print the generated sql to the console.
If by chance you accidentally dropped the database for our persistent class you can add hbm2ddl.auto and it will automatically create the database schema for you, handy isn’t it?
The current_session_context_class property enables the automatic session context management.
You can visit this page if you want to learn about other configurations you can set.
Part 3 – Create the Database and Persistent Class o0o Part 5 – Creating a SessionFactory Helper and Storing/Loading Objects

Recent Comments