Hibernate:根据另一列的值确定类型列

时间:2017-06-15 01:29:57

标签: java mysql spring hibernate orm

在我们的数据库的Hibernate模型中,我们有两种用户,标准用户和管理员用户,每种用户都有一个对应的表(standard_user,admin_user)。修改实体后,我们会保存哪个用户对修改后的记录进行了最后一次更改。这是通过两列user_type和user_id完成的。 user_types 1和2引用standard_user表,user_type 3引用admin_user表。因此,例如,user_type = 3和user_id = 2将引用admin_user表中id = 2的记录,user_type = 1,user_id = 2将引用standard_user表中的id = 2。

我想对此进行建模,以便在具有此关系的任何实体上都会出现User类型的字段,该字段可以是StandardUser,也可以是基于user_type值的AdminUser。我有点不知所措,因为我不知道该怎么做。 Discrimator值似乎可能有用,但它们似乎只适用于实体,而不是实体中的列。

帮助说明的例子:

@Entity
@Table(name = "standard_user")
public class StandardUser implements java.io.Serializable {

    private Integer userId;
    private Integer userTypeId;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "user_id", unique = true, nullable = false)
    public Integer getUserId() {
        return this.userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Column(name = "user_type", nullable = false)
    public Integer getUserTypeId() {
        return userTypeId;
    }

    public void setUserTypeId(Integer userTypeId) {
        this.userTypeId = userTypeId;
    }
}

@Entity
@Table(name = "admin_user")
public class AdminUser implements java.io.Serializable {

    private Integer userId;
    private Integer userTypeId;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "user_id", unique = true, nullable = false)
    public Integer getUserId() {
        return this.userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Column(name = "user_type", nullable = false)
    public Integer getUserTypeId() {
        return userTypeId;
    }

    public void setUserTypeId(Integer userTypeId) {
        this.userTypeId = userTypeId;
    }
}

@Entity
@Table(name = "store")
public class Store implements java.io.Serializable {

    private Integer storeId;
    private Integer modifiedByUserTypeId;
    private int modifiedBy;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "storeId", unique = true, nullable = false)
    public Integer getStoreId() {
        return this.storeId;
    }

    public void setStoreId(Integer storeId) {
        this.storeId = storeId;
    }

    @Column(name = "modifiedByUserTypeId", nullable = false)
    public Integer getModifiedByUserTypeId() {
        return modifiedByUserTypeId;
    }

    public void setModifiedByUserTypeId(Integer modifiedByUserTypeId) {
        this.modifiedByUserTypeId = modifiedByUserTypeId;
    }

    @Column(name = "modifiedBy", nullable = false)
    public int getModifiedBy() {
        return this.modifiedBy;
    }

    public void setModifiedBy(int modifiedBy) {
        this.modifiedBy = modifiedBy;
    }
}

商店实体显示我们如何记录上次编辑实体的用户。

我如何绘制这种关系?

0 个答案:

没有答案