Hibernate的IS-A关系映射

huangapple 未分类评论77阅读模式
英文:

Hibernate IS-A relation mapping

问题

我有一个实体模型(a),还有一些其他的实体(x),比如手机、平板电脑、汽车等。

这些实体(x)都有一个主键,该主键引用了模型(a)的主键,因此实体(x)只能取模型(a)实体的值。
我在谈论一个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;

我想要的是在手机表中创建一个引用模型表主键的主键。

英文:

I have an entity model(a) and some other entities(x) like mobile, tablet, car etc.

The entities(x) have a primary key that references to the primary of the model(a), so the entities(x) can take only the values of the model(a) entity.
I'm talking about an IS-A relationship.

I also need to have access from both ends.

I need help with the mapping in hibernate.
What i do right now and does not work:

  • model entity
@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;
  • mobile entity
@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;

What i want is to create a PK in mobile table that references to the PK of the model table.

答案1

得分: 0

我可能已经找到了解决方案。
目前似乎可以正常工作。稍后我将进行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;

}

不过,我不喜欢移动设备生成的主键列名...它是这样的:
"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)
英文:

I may have found a solution.
Seems to work for now. I will make jpa queries later to test it. Though the schema in the DB, seems to be exactly what i wanted.
Here is what i did:

  • model entity
@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;

}
  • 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;

}

I don't like the generated PK column name of the mobile though... It goes like:
"model_model_id".

Though it seemed to be working well... I fell into a problem. I get:

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)

huangapple
  • 本文由 发表于 2020年8月15日 01:20:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63417352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定