Java Reference
In-Depth Information
In the mapping file, you can specify a table name for this entity class and a table column for each
simple property. You can also specify the column details such as column length, not-null constraints,
and unique constraints. In addition, each entity must have an identifier defined, which can be generated
automatically or assigned manually. In this example, the identifier will be generated using a table
identity column.
Each application that uses Hibernate requires a global configuration file to configure properties
such as the database settings (either JDBC connection properties or a data source's JNDI name), the
database dialect, the mapping metadata's locations, and so on. When using XML mapping files to define
mapping metadata, you have to specify the locations of the XML files. By default, Hibernate will read the
hibernate.cfg.xml file from the root of the classpath. The middle extension cfg stands for configuration.
If there is a file on the classpath, that file will be consulted first.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
" ">
<property name="connection.driver_class">
<property name="connection.url">
jdbc: derby://localhost:1527/course; create=true
<property name="connection.username">app</property>
<property name="connection.password">app</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="show_sql">true</property>
<property name="">update</property>
<mapping resource="com/apress/springenterpriserecipes/course/
Course.hbm.xml" />
Before you can persist your objects, you have to create tables in a database schema to store the
object data. When using an ORM framework like Hibernate, you usually needn't design the tables by
yourself. If you set the property to update , Hibernate can help you to update the database
schema and create the tables when necessary. Naturally, you shouldn't enable this in production, but it
can be a great speed boost for development.
Now let's implement the DAO interface in the hibernate subpackage using the plain Hibernate API.
Before you call the Hibernate API for object persistence, you have to initialize a Hibernate session
factory (e.g., in the constructor).
package com.apress.springenterpriserecipes.course.hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
Search WWH ::

Custom Search