休眠:复合键内的ManyToOne关系

时间:2019-06-24 22:09:10

标签: java mysql hibernate

我具有以下数据库(mysql)模式:

CREATE TABLE hq_type (
  id_hq_type INTEGER NOT NULL,
  ....
  PRIMARY KEY (id_hq_type)
);

CREATE TABLE hq (
  year INTEGER NOT NULL,
  id_hq_type INTEGER NOT NULL,
  ....
  PRIMARY KEY (year,id_hq_type),
  ....
  CONSTRAINT fk_hq_hq_type FOREIGN KEY (id_hq_type) REFERENCES hq_type (id_hq_type)
);

我正在尝试使用此代码来表示hq表中的组合键和ManyToOne关系,但未成功:

class IdClass implements Serializable {
        private Integer year;
        private Integer idHqType;

        ....
}

@Entity
@Table(name = "hq")
@IdClass(IdClass.class)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Hq implements Serializable{

    @Id
    @Column(name = "year", nullable = false)
    private Integer year;

    @Id
    @ManyToOne
    @JoinColumn(name = "id_hq_type")
    private Tipo idHqType;

    .......

@Entity
@Table(name = "hq_type")
public class Tipo implements Serializable{

    @Id
    @GeneratedValue
    @Column(name = "id_hq_type")
    private Integer idHqType;

如何正确表示该表?

0 个答案:

没有答案