为什么我得到 "The type [class ...] is not a registered entity?"

Why do I get "The type [class ...] is not a registered entity?"

过去三个月我一直在开发一个新的 playframework 2.4 application,它在开发中使用 h2,在服务器上使用 postgres。我们使用 ebean 作为 ORM,使用 java 8 作为开发语言,我对这种体验总体上很满意。

应用程序的要求之一是将一些数据存储在外部 mysql 数据库中。

所以我将数据库添加到我的application.conf:

db.quba.driver = com.mysql.jdbc.Driver
db.quba.url = "jdbc:mysql://localhost:3306/quba"
db.quba.username = "..."
db.quba.password = "..."

将该数据库的迁移设置为关闭:

play.evolutions.db.quba.enabled = false

添加了 ebean 配置:

ebean.quba= "quba.models.*"

创建模型:

package quba.models;

import javax.persistence.*;

import com.avaje.ebean.Expr;
import com.avaje.ebean.Model;

@Entity
@Table(name = "st_profile")
public class QubaStationProfile extends Model {
  public static Model.Finder<Long, QubaStationProfile> find = new Model.Finder<>("quba",QubaStationProfile.class);

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "stationid")
  public Long id;

  public Integer session;
  public String profiles;
  public String exception;
  public String site;

  public static QubaStationProfile findStProfileByStationAndTermin(Long stationid, int termin) {
    return QubaStationProfile.find.where()
        .conjunction()
          .add(Expr.eq("stationid", stationid))
          .add(Expr.eq("session", termin))
        .findUnique();
  }

}

并实现了使用模型的服务。

我的问题是,尽管我可以使用该模型通过 finder 方法查询数据库,但当我尝试 save() 实体时,我会收到以下错误消息:

javax.persistence.PersistenceException: The type [class quba.models.QubaStation] 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:1189) ~[avaje-ebeanorm-4.6.2.jar:na]

我已经添加了

playEbeanDebugLevel := 9

到我的 build.sbt 并验证 quba.models.* 中的 类 确实被 ebean sbt 代理拾取和检测。

ebean-enhance> cls: quba/models/QubaStation  msg: already enhanced entity
ebean-enhance> cls: quba/models/QubaStation  msg: no enhancement on class

我还验证了 mysql 用户可以访问数据库并有权在该数据库中创建和更新记录。

这是一个失败的测试用例

package models;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import play.test.FakeApplication;
import play.test.Helpers;
import quba.models.QubaStation;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class QubaStationModelTest  {

  public FakeApplication app;

  @Test
  public void testCreateStation(){
    QubaStation station = new QubaStation();
    station.name ="X";
    station.latitude = 1;
    station.longitude = 2;
    station.save();

    station = QubaStation.findStationByName("X");

    assertNotNull(station);
    assertEquals("","X",station.name);

    station.delete();

  }


  @Before
  public void before() {
    Map<String, String> mysql = new HashMap<>();

    mysql.put("db.quba.driver","com.mysql.jdbc.Driver");
    mysql.put("db.quba.url","jdbc:mysql://localhost:3306/quba");
    mysql.put("db.quba.username","...");
    mysql.put("db.quba.password","...");

    app = Helpers.fakeApplication(mysql);
    Helpers.start(app);
  }

  @After
  public void after() {
    Helpers.stop(app);
  }

}

测试输出:

[error] Test models.QubaStationModelTest.testCreateStation failed: javax.persistence.PersistenceException: The type [class quba.models.QubaStation] 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()., took 3.608 sec
[error]     at com.avaje.ebeaninternal.server.persist.DefaultPersister.createRequest(DefaultPersister.java:1189)
[error]     at com.avaje.ebeaninternal.server.persist.DefaultPersister.insert(DefaultPersister.java:208)
[error]     at com.avaje.ebeaninternal.server.persist.DefaultPersister.save(DefaultPersister.java:199)
[error]     at com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1461)
[error]     at com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1454)
[error]     at com.avaje.ebean.Model.save(Model.java:208)
[error]     at models.QubaStationModelTest.testCreateStation(QubaStationModelTest.java:26)

如果您能帮助解决此问题,我们将不胜感激。

更新:

我也尝试了新的列表语法

ebean.quba= ["quba.models.*"]

以及枚举单个模型 类:

ebean.quba= ["quba.models.QubaStationProfile","quba.models.QubaStation"]

,但这没有什么区别。

另请注意,我没有使用 "default" 服务器。我用

play.ebean.defaultDatasource=h2

在开发期间,使用

在服务器配置中覆盖
play.ebean.defaultDatasource=postgres

我还注意到,即使 quba 的进化被禁用,游戏仍然创建了进化脚本。这可能与此问题相关,也可能不相关。

更新:

我确定 play-ebean 在尝试保存属于 'quba' 数据库的对象时正在使用 'postgres' serverConfig。由于 'postgres' 的 BeanDescriptorManager 不包含 'quba' 数据库的任何 BeanDescriptor,因此失败。

如果您查看 2.4 的 migration guide,您可以看到以下内容:

And finally, configure Ebean mapped classes as a list instead of a comma separated string (which is still supported but was deprecated):

ebean.default = ["models.*"]

ebean.orders = ["models.Order", "models.OrderItem"]

可能是弃用和迁移到更新版本的 Ebean 的组合,您遇到了这个问题。

终于解决了这个问题。

与我所相信的相反,play-ebean 没有将数据源与模型完全关联 类,即使配置为 'ebean.quba="quba.models.*"'。它适用于读取,但不适用于写入。

修改外部数据库时,需要对应用程序进行不同的编码:

private final EbeanServer quba = Ebean.getServer("quba");
....
private QubaStation saveStationPosition(PositionModel positionModel)   
  {
   QubaStation station = new QubaStation();
   ...
   quba.save(station);
  }

更新

另一种方法是使用 Model.insert(serverName) 或 Model.update(serverName) 而不是显式引用 EbeanServer:

private QubaStation insertStationPosition(PositionModel positionModel)   
   {
       QubaStation station = new QubaStation();
       ...
       station.insert("quba");
   }

请注意,Model 只有 insert(String serverName) 和 update(String serverName) 方法,没有 Model.save(String serverName)。所以代码在插入之前需要检查实体是否已经存在,否则必须调用Model.update。