如何解决Hibernate IS-A关系映射
我有一个实体模型(a)和其他一些实体(x),例如手机,平板电脑,汽车等。
entity(x)有一个主键,该主键引用了model(a)的主键,因此entities(x)只能采用model(a)实体的值。 我说的是IS-A关系。
我还需要从两端访问。
我需要有关休眠中映射的帮助。 我现在正在做什么,但是行不通:
- 模型实体
@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",foreignKey = @ForeignKey(name = "FK_Mobile_Model"))
private Model model;
我想要在移动表中创建一个引用模型表PK的PK。
解决方法
我可能找到了解决方案。 似乎现在可以工作。稍后我将进行jpa查询以对其进行测试。尽管数据库中的架构似乎正是我想要的。 这是我所做的:
- 模型实体
@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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。