我有一个sql表,如下所示。
id type_communicationChannel id_phone id_cellphone +----+--------------------------------+-------------+----------------+ | 1 | "PHONE" | 1 | NULL | | 2 | "CELLPHONE" | NULL | 1 | +----+--------------------------------+-------------+----------------+
对于实现,我有两种对Java对象建模的解决方案:
第一个解决方案使用注解@AttributesOverrides:
@Entity
public class Parent {
private @Id @GeneratedValue int id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="id",column=@Column(name="id_phone")})
private CommunicationChannel phone;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="id",column=@Column(name="id_cellphone")})
private CommunicationChannel cellphone;
private String type_communicationChannel;
}
@Embeddable
public class CommunicationChannel {
private int id;
}
第二种解决方案使用注释@DiscriminatorColumn:
@Entity
public class Parent {
private @Id @GeneratedValue int id;
@Embedded
private CommunicationChannel communicationChannel;
}
@Embeddable
@DiscriminatorColumn(
name="type_communicationChannel",
discriminatorType=DiscriminatorType.STRING
)
public abstract class CommunicationChannel {}
@DiscriminatorValue("CELLPHONE")
public class CellPhone extends CommunicationChannel {
private int id_cellphone;
}
@DiscriminatorValue("PHONE")
public class PhoneFix extends CommunicationChannel {
private int id_phone;
}
最合适的解决方案是什么?
感谢您的回答。