Hibernate IS-A关系映射



我有一个实体模型(a(和一些其他实体(x(,如移动设备、平板电脑、汽车等。

实体(x(有一个主键,该主键引用模型(a(的主键,因此实体(x。我说的是IS-A关系。

我还需要从两端访问。

我需要帮助在hibernate中进行映射。我现在做的事情不起作用:

  • 模型实体
@Data
@Entity(name = "Model")
@Table(name = "model", schema = "mysch")
public class Model {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "model_id", columnDefinition = "BIGINT UNSIGNED")
private long id;
@Column(name = "description", length = 255, nullable = false, unique = true)
private String description;
@OneToOne(mappedBy = "model")
private Mobile mobile;
  • 移动实体
@Data
@Entity(name = "Mobile")
@Table(name = "mobile", schema = "mysch")
public class Mobile {
@Id
@Column(name = "mobile_id", columnDefinition = "BIGINT UNSIGNED")
private long id;
@OneToOne
@JoinColumn(name = "mobile_id", referencedColumnName = "model_id", nullable = false, foreignKey = @ForeignKey(name = "FK_Mobile_Model"))
private Model model;

我想要的是在移动表中创建一个引用模型表PK的PK。

我可能已经找到了解决方案。现在看来有效。我稍后会进行jpa查询来测试它。尽管DB中的模式似乎正是我想要的。以下是我所做的:

  • 模型实体
@Data
@Entity(name = "Model")
@Table(name = "model", schema = "sch")
public class Model {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "model_id", columnDefinition = "BIGINT UNSIGNED")
private long id;
@Column(name = "description", length = 255, nullable = false, unique = true)
private String description;
@OneToOne(mappedBy = "model")
private Mobile mobile;
}
  • 移动
@Data
@Entity(name = "Mobile")
@Table(name = "mobile", schema = "sch")
public class Mobile {
@Id
@Column(name = "mobile_id", columnDefinition = "BIGINT UNSIGNED")
private long id;
@OneToOne
@MapsId
private Model model;
}

我不喜欢手机生成的PK列名。。。它是这样的:"model_model_id";。

尽管它看起来运行得很好。。。我遇到了一个问题。我得到:

Exception in thread "JavaFX Application Thread" java.lang.StackOverflowError
at java.base/java.lang.String.equals(String.java:1009)
at com.dc.sch.entity.Model.equals(Model.java:6)
at com.dc.sch.entity.Mobile .equals(Mobile .java:6)
at com.dc.sch.entity.Model.equals(Model.java:6)
at com.dc.sch.entity.Mobile .equals(Mobile .java:6)
at com.dc.sch.entity.Model.equals(Model.java:6)
at com.dc.sch.entity.Mobile .equals(Mobile .java:6)
at com.dc.sch.entity.Model.equals(Model.java:6)

最新更新