为什么我得到“类型不是注册实体”的PersistenceException?

时间:2015-05-10 15:28:46

标签: java exception playframework ebean

我正在尝试使用播放框架2.3.8中的Ebean创建并将一些问题/答案保存到数据库中。

我的模型包中有一个question.java类:

package model;

import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import play.db.ebean.Model;

@Entity
public class Question extends Model {

    // Auto-generated serial
    private static final long serialVersionUID = 1L;

    @Id
    public String questionID;
    public String questionText;
    public Integer voteScore;
    public String userID;

    // Constructor
    public Question(String inputID, String inputQuestionText, Integer inputVoteScore, String inputUserID){
        this.questionID = inputID;
        this.questionText = inputQuestionText;
        this.voteScore = inputVoteScore;
        this.userID = inputUserID;
    }

    // Default Constructor is needed for the form, else the play framework breaks!
    public Question(){

    }

    public static Finder<String,Question> find = new Finder<String, Question>(
            String.class, Question.class
          );

    public static List<Question> all() {
          return find.all();
        }

    public static void create(Question question) {
          question.save();
        }

    public void delete(String id) {
          find.ref(id).delete();
        }

    // Mostly for debugging
    public String toString(){
        return "questionID = " + questionID + " questionText = " + questionText + " voteScore = " + voteScore + " userID = " + userID;
    }
}

在我的application.java控制器类中,我定义了一个新问题,并尝试使用create方法将其放入数据库中:

Question question1 = new Question("xyz", "Do Androids dream?", 127, "Marcus");
Question.create(question1);

我使用了@Entity标记,我在application.conf中取消注释了这三行:

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"

现在,当我启动项目时,我在浏览器中收到此错误:

[PersistenceException: The type [class model.Question] is not a registered entity? If you don't explicitly list the entity classes to use Ebean will search for them in the classpath. If the entity is in a Jar check the ebean.search.jars property in ebean.properties file or check ServerConfig.addJar().]

它指向这一行:

Question.java:48 
47      public static void create(Question question) {
48        question.save();
49      }

控制台输出:

play.api.Application$$anon$1: Execution exception[[PersistenceException: The type [class model.Question] is not a registered entity? If you don't explicitly list the entity classes to use Ebean will s
earch for them in the classpath. If the entity is in a Jar check the ebean.search.jars property in ebean.properties file or check ServerConfig.addJar().]]
        at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8]
        at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
        at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
Caused by: javax.persistence.PersistenceException: The type [class model.Question] is not a registered entity? If you don't explicitly list the entity classes to use Ebean will search for them in the
classpath. If the entity is in a Jar check the ebean.search.jars property in ebean.properties file or check ServerConfig.addJar().
        at com.avaje.ebeaninternal.server.persist.DefaultPersister.createRequest(DefaultPersister.java:1299) ~[avaje-ebeanorm-3.3.4.jar:na]
        at com.avaje.ebeaninternal.server.persist.DefaultPersister.saveRecurse(DefaultPersister.java:273) ~[avaje-ebeanorm-3.3.4.jar:na]
        at com.avaje.ebeaninternal.server.persist.DefaultPersister.save(DefaultPersister.java:244) ~[avaje-ebeanorm-3.3.4.jar:na]
        at com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1610) ~[avaje-ebeanorm-3.3.4.jar:na]
        at com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1600) ~[avaje-ebeanorm-3.3.4.jar:na]

1 个答案:

答案 0 :(得分:4)

您的配置

ebean.default="models.*"

如果我正确地解释文档,则意味着它会在models包中搜索实体。

但您实体的包裹不是models,而是model

package model;