【问题标题】:JPA not generating tables from entitiesJPA 不从实体生成表
【发布时间】:2013-01-31 20:49:18
【问题描述】:

这是一些实体:

@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

我的带有设置的 persistence.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 不起作用,我不知道为什么。

【问题讨论】:

  • 哪个版本的休眠?

标签: database hibernate jpa persistence entity


【解决方案1】:

根据配置,org.hibernate.dialect.HSQLDialect 与 MySQL 数据库一起使用。使用 MySQL 方言而不是 HSQL 之一可能会有所帮助。可能会使用 InnoDB - 如果是这样,那么 MySQL5InnoDBDialect 是可行的方法。

【讨论】:

  • 非常感谢!我太天真了!
猜你喜欢
  • 2017-08-06
  • 2017-02-02
  • 2016-01-02
  • 2014-09-18
  • 2011-06-09
  • 2018-06-03
  • 1970-01-01
  • 2013-12-23
相关资源
最近更新 更多