2013-02-15 24 views
2

這裏JPA不會產生表的一些實體:從實體

@Entity 
public class Forest { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 


    public Forest() { 
    } 

    public long getId() { 
     return id; 
    } 

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

我要插入表森林一些元素:

public class Main { 
private static EntityManagerFactory emf = 
     Persistence.createEntityManagerFactory("server"); 

    public static void main(String[] args) { 

     EntityManager em = emf.createEntityManager(); 
     EntityTransaction trx = em.getTransaction(); 
     Forest forest = new Forest(); 


     trx.begin(); 
     em.persist(forest); 
     trx.commit(); 

    } 
} 

拋出的異常:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Table 'server.forest' doesn't exist 

Caused by: org.hibernate.exception.SQLGrammarException: Table 'server.forest' doesn't exist 

我的堅持帶設置的.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 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"> 
    <persistence-unit name="server"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 

     <properties> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
      <property name="javax.persistence.jdbc.url"    value="jdbc:mysql://localhost:3306/server"/> 
      <property name="javax.persistence.jdbc.user" value="root" /> 
      <property name="javax.persistence.jdbc.password" value="root" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

當我刪除@GeneratedValue(strategy = GenerationType.AUTO)併爲林設置ID: forest.setID(1)時,沒有發生異常並且已生成表。所以,ID的自動生成不起作用,我不知道爲什麼。

+0

哪個版本的hibernate? – 2013-02-15 19:44:13

回答

4

根據配置有org.hibernate.dialect.HSQLDialect與MySQL數據庫一起使用。使用MySQL方言而不是HSQL可能會有所幫助。可能使用InnoDB - 如果是這樣,那麼MySQL5InnoDBDialect就是要走了。

+0

非常感謝!我很防腐! – 2013-02-15 19:58:41