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)


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"



		<!-- 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="">update</property>

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


  • jdbc:derby:data;create=true Once the Hibernate session is instantiated for the first time, the Database is created in the data folder
  • 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()
		return factory;

Working with entities

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

That’s all folks!




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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s