15

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.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

1 Answers1

30

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();    
Kohányi Róbert
  • 9,791
  • 4
  • 52
  • 81
  • how do you create the database and populate the tables ? – Mahmoud Saleh Dec 11 '11 at 11:41
  • 4
    @Msaleh If you use a JDBC URL like `jdbc:derby:test;create=true` a database will be created on your first connect. If you use the `` then Hibernate will create the tables necessary to hold your entities. The generated schema's name, table names, etc. all can be controlled through standard Java annotations. However I advise you to create your database and tables by hand and don't rely on Hibernate to create a *good* schema for you. You can interface with Apache derby with `ij` which is like `mysql` for MySQL to manage databases and such. – Kohányi Róbert Dec 11 '11 at 12:36
  • how to create the derby embedded database by hand ? – Mahmoud Saleh Dec 11 '11 at 13:07
  • 2
    @Msaleh Use `ij`, which is like `mysql` (the client command-line interface tool) to MySQL. [It has an extensive documentation](http://db.apache.org/derby/docs/10.8/tools/tools-single.html#ctoolsij34525). Basically, you start `ij` and connect to a database: `ij> connect 'jdbc:derby:/database;create=true';`. `create=true` tells `ij` to create the database if doesn't exist. From there on you do what you want, create tables, drop them, query them or whatever. I advise you to refer to the documentation if you're unsure about something, because it is well written and extensive. – Kohányi Róbert Dec 11 '11 at 14:01
  • thanks a lot for such great answer, please update the answer with what we discussed in the comments. – Mahmoud Saleh Dec 11 '11 at 14:22
  • @Msaleh You're question was about how to use Derby and Hibernate together with Maven, what JARs are needed, etc. Derby management is outside of the answer's scope and if anyone wants to know about it he/she can read these comments. Aside from this, I'm a lazy ass guy! (If I'll have time next week at work I'll try and update it a bit though.) – Kohányi Róbert Dec 11 '11 at 14:35
  • can ij be used with embedded derby ? – Mahmoud Saleh Dec 23 '11 at 10:10
  • @Msaleh For `ij` an embedded and non-embedded Derby database are the same thing, so the answer is yes. Check out [this](http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html#ij_intro) page for more info. – Kohányi Róbert Dec 23 '11 at 10:47