为什么我得到“类型[类...]不是注册实体?”

时间:2015-11-27 08:03:46

标签: ebean playframework-2.4

我在过去的三个月里一直致力于开发一个新的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"]

,但这没有任何区别。

另请注意,我没有使用“默认”服务器。我用

play.ebean.defaultDatasource=h2

在开发期间,使用

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

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

更新:

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

2 个答案:

答案 0 :(得分:2)

终于解决了这个问题。

相反的是我相信,玩,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。

答案 1 :(得分:0)

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

  

最后,将Ebean映射类配置为列表而不是逗号分隔字符串(仍受支持但已弃用):

     

ebean.default = [“models。*”]

     

ebean.orders = [“models.Order”,“models.OrderItem”]

有可能将弃用和移动到更新版本的Ebean相结合,你就会遇到这个问题。