Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using hibernate with embedded derby

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.

like image 205
Mahmoud Saleh Avatar asked Dec 10 '11 19:12

Mahmoud Saleh


People also ask

How do I access my embedded Derby database?

If you want to connect to the Derby database in embedded mode you can use the following command. In this example the database is located at c:\temp\db\FAQ\db. connect 'jdbc:derby:c:\temp\db\FAQ\db'; If you want to connect to a Derby database which is running in server mode then you can use the following command.

Is Derby supported by JPA starter?

To include derby into the project, All you have to do is to add the Derby and JPA Starter dependencies from Spring Boot.

How do you use a Derby jar?

To install Apache Derby in embedded mode, include the jar file derby. jar in your CLASSPATH. After setting up Apache Derby, to access it, run Java programs using the embedded driver.


1 Answers

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();    
like image 89
Kohányi Róbert Avatar answered Oct 26 '22 18:10

Kohányi Róbert