本文介绍了在使用EJB3 / Hibernate时,表格不会在Apache Derby中自动创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EJB3实体bean播放器,它注释如下:

I have an EJB3 entity bean Player which is annotated as given below:

@Entity
@Table(name = "PLAYER")
public class Player {
    public Player() {
        super();
    }

    @Id
    @GeneratedValue
    private String id;

    @Column(nullable = false)
    private String firstName;

    private String lastName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

我使用Apache Derby DB来坚持这一点。我有一个解释hibernate属性的 persistence.xml 文件,并且定义了 hibernate.hbm2ddl.auto = create

I am using Apache Derby DB to persist this. I have a persistence.xml file which explains the hibernate properties and I have define hibernate.hbm2ddl.auto=create.

<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="PlayerApp" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.cricinfo.domain.Player</class>
    <properties>
        <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>  
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.connection.url"
            value="jdbc:derby://localhost:1527/PlayerAppDB;create=true" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />           
    </properties>
</persistence-unit>



但是当我尝试坚持这个对象我得到一个异常说:

But when I try to persist this object I get an exception saying

Caused by: org.apache.derby.client.am.SqlException: Table/View 'PLAYER' does not exist.
at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)

是否它应该自动创建表,因为我已经设置了属性 hibernate.hbm2ddl.auto ?或者有什么我缺少的?

Isn't it supposed to auto create the tables since I have set the property hibernate.hbm2ddl.auto ? Or is there something that I am missing?

我的主要方法如下所示:

My main method is as shown below:

public static void main(String[] args) {

    Player p = new Player();
    p.setFirstName("A");
    p.setLastName("BC");

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("PlayerApp");
    EntityManager entityMgr =  factory.createEntityManager();
    EntityTransaction tx = entityMgr.getTransaction();
    tx.begin();
    entityMgr.persist(p);
    tx.commit();
    entityMgr.close();
    factory.close();
}


推荐答案

p>

Add the below property

<property name="hibernate.generateDdl" value="true" />

这篇关于在使用EJB3 / Hibernate时,表格不会在Apache Derby中自动创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 03:09