简体   繁体   中英

Hibernate IS-A relation mapping

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM