使用Ebean View`@ View`而不复制模型

时间:2018-09-06 16:52:51

标签: java playframework orm ebean

我试图基于简单的联接在表上创建Ebean视图,当我尝试为基础表extend Model时遇到问题。

“视图”字段和“模型”字段完全相同。

我的表格模型如下:

@Entity
@Table(name = "assets")
public class Asset extends EnvironmentModel<Integer> {
    @Id
    @Column
    @PrimaryKey
    @Attribute(index = 0)
    private int assetId;
    @Column
    @Attribute(index = 1)
    private String make;
    etc...
}

那很好。

现在我要对View进行的操作是:

@View(name = "assets_view")
public class AssetView extends Asset {
}

我想我可能会这样做,因为AssetViewAsset具有相同的确切字段。

以这种方式执行操作时,出现异常: Caused by: javax.persistence.PersistenceException: models.asset.AssetView is NOT an Entity Bean registered with this server?

因此,我的下一个尝试是将@Entity批注添加到View类。例如

@Entity
@View(name = "assets_view")
public class AssetView extends Asset {
}

编译时出现以下异常: Error injecting constructor, java.lang.IllegalStateException: Checking class models.asset.AssetView and found class models.asset.Asset that has @Entity annotation rather than MappedSuperclass?

但是我无法从我的@Entity类中删除Asset注释,因为我需要这样做来进行插入。

我的问题是: 有没有什么方法可以让视图和表共享同一模型,所以我可以从视图中查询并插入/更新到表中?

1 个答案:

答案 0 :(得分:1)

好的,我找到了答案,不知道这是否显而易见。

基本上,我只是将基类设为@MappedSuperClass,例如

@MappedSuperclass
public class _Asset extends EnvironmentModel<Integer> {
    @Id
    @Column
    @PrimaryKey
    @Attribute(index = 0)
    private int assetId;
    @Column
    @Attribute(index = 1)
    private String make;
    etc..
}

然后,我从那个映射的超类扩展了Asset表和AssetView,例如

@Entity
@Table(name = "assets")
public class Asset extends _Asset {
}

-

@Entity
@View(name = "assets_view")
public class AssetView extends _Asset {
    public static final Model.Find<Integer, AssetView> finder = new Model.Finder<>(AssetView.class);
}