代码之家  ›  专栏  ›  技术社区  ›  Sam Orozco

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

  •  0
  • Sam Orozco  · 技术社区  · 7 年前

    我试图基于一个简单的连接创建表上的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 {
    }
    

    我想我能这么做是因为 AssetView 以及 Asset 具有相同的精确字段。

    当我这样做的时候,我得到了一个例外: Caused by: javax.persistence.PersistenceException: models.asset.AssetView is NOT an Entity Bean registered with this server?

    所以我的下一个尝试是添加 @Entity 视图类的注释。例如

    @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?

    但我不能移除 @实体 我的注释 资产 类,因为我需要它来做插入。

    我的问题是: 是否有任何方法可以使视图和表共享同一模型,以便我可以从视图进行查询并将其插入/更新到表中?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sam Orozco    7 年前

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

    基本上,我刚把我的基础等级定为a级 @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);
    }
    
    推荐文章