Gar*_*eth 2 java inheritance hibernate
在简单的继承树中,抽象超类有两个子类.
子类都存储一个键值对,但是其中一个将保存一个类型的加密字符串,另一个将保存一个普通的旧字符串.
现在,我的问题是我可以这样做:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract Attribute {
@Id
private Integer id;
@Column(name="attribute_key")
private String key;
}
@Entity
public EncryptedAttribute extends Attribute {
@Column(name="attribute_value")
private EncryptedString encryptedValue;
}
@Entity
public UnEncryptedAttribute extends Attribute {
@Column(name="attribute_value")
private String plainValue;
}
Run Code Online (Sandbox Code Playgroud)
Encrypted String和plain String最终都应该作为db中的varchars,但是我可以在同一列中存储与不同子类关联的持久属性吗?这将避免在未在特定行中使用的列中存储空值的"瑞士奶酪"副作用.
是的你可以.(我会这样做你的情况)
但是,您将遇到一些约束:两个属性将具有相同的最大长度.
在子类之间共享类似列之类的列更值得怀疑的是外键.假设您希望两个外键(每个子类中有一个)引用不同的表,如果使用相同的列,则无法设置外键约束.
另一方面,如果两个外键都是必需的,但您选择使用两个不同的列以便可以使用fk约束,则无法设置非空约束.
| 归档时间: |
|
| 查看次数: |
1082 次 |
| 最近记录: |