Java – tables not automatically created in Apache Derby when using EJB3 / Hibernate
•
Java
I have an EJB 3 entity bean player with the following comments:
@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'm using Apache Derby DB to stick to this I have a persistence XML file, which explains the hibernate attribute. I defined 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 stick to this object, I get an exception
Caused by: org.apache.derby.client.am.sqlException: Table/View 'PLAYER' does not exist. at org.apache.derby.client.am.Statement.completesqlca(UnkNown Source)
Should I create the table automatically because I have set the attribute hibernate hbm2ddl. auto? Or something I miss?
My main methods are shown in the figure 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(); }
Solution
Add the following attributes
<property name="hibernate.generateDdl" value="true" />
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码