使用嵌入式德比的hibernate

Mah*_*leh 14 hibernate derby maven

我想在独立的应用程序中使用嵌入式德比的hibernate,我有一些问题:

  1. 我需要什么罐子?
  2. 什么是必要的休眠配置?
  3. 还有其他必要的配置吗?
  4. 查询/标准是否有任何问题/限制?

如果你也可以建议我这个方法的一些好的教程,这将是更好的,提前谢谢.

Koh*_*ert 30

我使用Apache Derby和Hibernate来测试我的项目模型类之一(它们的equals,hashCode实现,查询等).MySQL用作生产数据库.我选择Derby而不是HSQLDB,因为我遇到了一些与Hibernate和HSQLDB不兼容的问题,这意味着,鉴于我的实体(他们的名字,架构,密钥)及其关系,Hibernate无法在HSQLDB中创建我的数据库模式,而它可以与德比.那就是说,也许我搞砸了什么; 不兼容性也可以解决.

无论如何,这是我在测试中使用的内容(我已经修改了我的内容,pom.xml以便将Derby作为运行时依赖项并运行单个测试,以确保它正常工作).

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>                                     
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Test 实体

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

  @Id
  public int id;

  @Basic
  public String data;
}
Run Code Online (Sandbox Code Playgroud)

测试

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();    
Run Code Online (Sandbox Code Playgroud)

  • @Msaleh如果使用像`jdbc:derby:test; create = true`这样的JDBC URL,将在第一次连接时创建一个数据库.如果使用`<property name ="hibernate.hbm2ddl.auto"value ="create"/>`那么Hibernate将创建保存实体所需的表.生成的模式的名称,表名等都可以通过标准Java注释来控制.但是我建议你手动创建数据库和表,不要依赖Hibernate为你创建一个*good*架构.您可以使用`ij`与Apache derby进行交互,这类似于MySQL的mysql管理数据库等. (4认同)
  • @Msaleh使用`ij`,它就像MySQL的mysql`(客户端命令行界面工具).[它有广泛的文档](http://db.apache.org/derby/docs/10.8/tools/tools-single.html#ctoolsij34525).基本上,你启动`ij`并连接到数据库:`ij> connect'jdbc:derby:<path-to-db>/database; create = true';`.如果不存在,`create = true`告诉`ij`创建数据库.从那里开始你做你想做的事,创建表,删除它们,查询它们或者其他什么.如果您对某些事情不确定,我建议您参考文档,因为它写得很好且很广泛. (2认同)