我在BD中有表MOBILE和VESSEL。他们之间存在着传承的关系。 MOBILE是父级,VESSEL有一个具有相同ID的外键。
在Java中我有两个类:
@Entity
public class Mobile implements Serializable, ITechnicalBO {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id")
private Vessel vessel;
{.................}
}
@Entity
public class Vessel implements java.io.Serializable, ITechnicalBO {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id")
private Mobile mobile;
{.................}
}
我需要很好地声明id。我通过互联网找到了很多类似的主题,但我无法解决我的问题。
我是如何真正声明要向Hibernate声明的id必须生成VESSEL的id来分配MOBILE的id?
感谢您的帮助。
答案 0 :(得分:0)
你说Mobile是父母,所以Vessel应该扩展Mobile:
@Entity
@PrimaryKeyJoinColumn(name = "id_vessel", referencedColumnName = "id_mobile")
public class Vessel extends Mobile implements java.io.Serializable {
@Column(name="id_vessel", insertable = false, updatable = false)
private Integer id;
// Vessel inherits all the fields from Mobile. You can add some if you want.
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Mobile implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id_mobile")
private Integer id;
// Some fields
}