using hibernate with embedded derby

Mahmoud Saleh picture Mahmoud Saleh · Dec 10, 2011 · Viewed 24.9k times · Source

i want to use hibernate with embedded derby in a standalone application, and i have some questions:

  1. What jars do i need ?
  2. What are the necessary hibernate configuration ?
  3. Are there any other necessary configuration ?
  4. Does it have any problems/limitations with queries/criteria ?

if you can also suggest me some good tutorial for this approach that will be preferable, thanks in advance.

Answer

Kohányi Róbert picture Kohányi Róbert · Dec 11, 2011

I use Apache Derby with Hibernate for testing one of my project's model classes (their equals, hashCode implementations, queries, etc.). MySQL is used as the production database. I choose Derby instead of HSQLDB, because I've experienced some incompatibilities with Hibernate and HSQLDB, meaning, given my entities (their name, schema, keys) and their relation Hibernate couldn't create my database schema in HSQLDB, while it could with Derby. That said, maybe I messed up something; also the incompatibilities could have been resolved.

Anyway, here is what I use in my tests (I've modified my pom.xml so that it would include Derby as a runtime dependency and run a single test, just to make sure it's working).

pom.xml

<dependencies>                                      
  ...                               
  <dependency>                                      
    <groupId>org.hibernate</groupId>                
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.8.Final</version>                  
  </dependency>                                     
  <dependency>                                      
    <groupId>org.apache.derby</groupId>             
    <artifactId>derby</artifactId>                  
    <version>10.8.2.2</version>                     
    <scope>runtime</scope>                          
  </dependency>                                     
  <!-- 
     an slf4j implementation is needed by
     hibernate so that it could log its *stuff*
  -->
  <dependency>                                      
    <groupId>org.slf4j</groupId>                    
    <artifactId>slf4j-simple</artifactId>           
    <version>1.6.4</version>                        
    <scope>runtime</scope>                          
  </dependency>                                     
  ...                             
</dependencies>                                     

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0">
  <persistence-unit name="test">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>Test</class>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <!--
        if you don't have a database already created
        append ;create=true to end of the jdbc url
      -->
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:test"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.password" value="root"/>
      <!--  
        if you just created the database, maybe
        you want hibernate to create a schema for you

        <property name="hibernate.hbm2ddl.auto" value="create"/> 
      -->
    </properties>
  </persistence-unit>
</persistence>

Test entity

@Entity
@Table(name = "test")
public class Test {

  @Id
  public int id;

  @Basic
  public String data;
}

Test

EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");  
EntityManager em = emf.createEntityManager();                               
EntityTransaction tx = em.getTransaction();                                 

Test test = em.find(Test.class, 1);                                         
if (test == null) {                                                         
  test = new Test();                                                        
  test.id = 1;                                                              
  test.data = "a";                                                          

  tx.begin();                                                               
  em.persist(test);                                                         
  tx.commit();                                                              
}                                                                           

System.out.format("Test{id=%s, data=%s}\n", test.id, test.data);            

em.close();                                                                 
emf.close();