2

我正在尝试使用 JPA 使用 Play 2 保存数据,但我得到: Execution exception [IllegalArgumentException: Unknown entity: models.Stream];

.java`

package models;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.Persistence;

import com.restfb.Facebook;

@Entity
public class Stream {
  @Facebook
  @Id
  public String post_id;

  @Facebook
  public String created_time;

  @Facebook
  public String message;

  @Facebook 
  public String permalink;

  @Override
  public String toString() {
    return String.format("%s, %s, %s, %s", created_time, message, permalink, post_id);
  }

  public void save(){
      EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("defaultPersistenceUnit");
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      entityManager.getTransaction().begin();
      entityManager.persist(this);        
      entityManager.getTransaction().commit();
      entityManager.close();
  }
}

`

持久性.xml

`

<persistence-unit name="defaultPersistenceUnit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/fiatbr_db"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value="1234"/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hbm2ddl.auto" value="create"/>
    </properties>
</persistence-unit>

`

有什么遗漏吗?任何配置错误?看起来模型 Stream 没有找到,但是怎么找不到呢?还有更多要在类中添加注释@Entity 的东西吗?谢谢 :)

4

2 回答 2

1

实体类未在您的 persistence.xml 文件中列出:

<class>models.Stream</class>
于 2013-06-01T15:55:49.373 回答
1

还要确保您导入了哪个@Entity,

我导入了hibernate类,找了半个小时左右。导入 javax.persistence.Entity 工作...

不是那么熟练Java,但这可能是问题所在。

于 2014-10-17T10:06:44.757 回答