Java Derby and Hibernate 3.x configuration using annotations

Key words
Java 1.6, JPA, Java Derby database, Hibernate 3.x, Maven 2.x or higher

Note that this post is based on a Maven project.

Project dependencies
Project basic configuration and dependencies list (in the pom.xml file)

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>3.5.6-Final</version>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.12.0.GA</version>
		</dependency>
		<dependency>
			<groupId>org.apache.derby</groupId>
			<artifactId>derby</artifactId>
			<version>10.8.2.2</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.derby</groupId>
			<artifactId>derbyclient</artifactId>
			<version>10.8.2.2</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-annotations</artifactId>
			<version>3.5.6-Final</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.6</version>
		</dependency>
	</dependencies>

Hibernate configuration
Hibernate basic configuration; the hibernate.cfg.xml file (in the src/main/resources/ folder)

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

	<session-factory>

		<!-- Database connection settings -->
		<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
		<property name="connection.url">jdbc:derby:data;create=true</property>
		<property name="connection.username">application</property>
		<property name="connection.password">password</property>
		
		<property name="hibernate.default_schema">my_application</property>

		<!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">1</property>

		<!-- SQL dialect -->
		<property name="dialect">org.hibernate.dialect.DerbyDialect</property>

		<!-- Enable Hibernate's automatic session context management -->
		<property name="current_session_context_class">thread</property>

		<!-- Disable the second-level cache -->
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>

		<!-- Drop and re-create the database schema on startup -->
		<property name="hbm2ddl.auto">update</property>

		<mapping class="com.ichir.projects.entities.Entity1" />
		<mapping class="com.ichir.projects.entities.Entity2" />

	</session-factory>

</hibernate-configuration>
  • jdbc:derby:data;create=true Once the Hibernate session is instantiated for the first time, the Database is created in the data folder
  • hbm2ddl.auto set to update The tables are created on demand.
  • All the JPA annotated classes are declared in the mapping class tags

Instantiation and Bootraping
Initialize the factory

	public SessionFactory getFactory() {
		if (factory == null) {
			synchronized (MyClass.class) {
				if (factory == null) {
					SessionFactory factory = new AnnotationConfiguration()
					  .configure()
					  .buildSessionFactory();
				}
			}
		}
		return factory;
	}

Working with entities

	Session session = getFactory().openSession();
	// do CRUD operations on entities, please refer to JPA and Hibernate for that
	session.close();

That’s all folks!

Downloads

References

Advertisements

7 thoughts on “Java Derby and Hibernate 3.x configuration using annotations”

    1. Hi,
      Please find here a good answer on The difference between JPA and hibernate JPA, to resume JPA is the Java specification for persistence and hibernate is one implementation among others (openJPA, toplink, …).
      Secondly, In my post I used JPA 1.0 (along with hibernate). In the post you pointed to, the people used JPA 2.0 (along with the corresponding hibernate implementation). JPA 2.0 is the latest Java specification for persistence (though there is a 2.1 version specification I think), offering more and improved persistence functionalities, for this please refer directly to the JPA specification here.

  1. Good code example! However I was trying to query the table after the insert with:
    Query q = session.createQuery(“from Entity1″);
    However the result always comes back with an empty set.
    Any clues?

  2. Hello.This post was extremely motivating, especially because I was browsing for thoughts on this subject last Thursday. deeegfadccdd

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s