开发者

Tables not getting auto-created in Apache Derby when using EJB3/Hibernate

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;
    }
}

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)

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


Add the below property

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜