using hibernate with embedded derby

前端 未结 1 2160
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 19:35

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

  1. What jar
1条回答
  •  没有蜡笔的小新
    2020-12-28 19:42

    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

                                          
      ...                               
                                            
        org.hibernate                
        hibernate-entitymanager
        3.6.8.Final                  
                                           
                                            
        org.apache.derby             
        derby                  
        10.8.2.2                     
        runtime                          
                                           
      
                                            
        org.slf4j                    
        slf4j-simple           
        1.6.4                        
        runtime                          
                                           
      ...                             
                                         
    

    persistence.xml

    
    
      
        org.hibernate.ejb.HibernatePersistence
        Test
        
          
          
          
          
          
          
        
      
    
    

    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();    
    

    0 讨论(0)
提交回复
热议问题