如何使用@OneToMany链接两个表而不使用外键或连接表

时间:2016-07-05 06:53:09

标签: mysql hibernate

我有两个休眠对象"报告"和" ReportContent"与关键" Uuid"

有关
CREATE TABLE Report(
  Id BIGINT AUTO_INCREMENT PRIMARY KEY,
  Uuid CHAR(32) BINARY NOT NULL UNIQUE
)

CREATE TABLE ReportContent(
  Id BIGINT AUTO_INCREMENT PRIMARY KEY,
  Uuid CHAR(32) BINARY NOT NULL,
  Type INT NOT NULL
)
ALTER TABLE (ReportContent) ADD UNIQUE (Uuid, Type);

public class Report {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id")
    private long id;
    @Column(name = "Uuid", columnDefinition = "char(32)", nullable = false, unique = true, updatable = false)
    private String uuid;
    @OneToMany
    @JoinColumn(name = "Uuid")
    private List<ReportContent> contents;

    // setter and getter
}

public class ReportContent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id")
    private long id;
    @Column(name = "Uuid", nullable = false, columnDefinition = "char(32)", updatable = false)
    private String uuid;

    // setter and getter
}

如何做,选择Report和hibernate发送sql以获取具有条件的ReportContents Report.Uuid = ReportContent.Uuid?

1 个答案:

答案 0 :(得分:0)

尝试类似

的内容
public class Report {

    // ...

    @OneToMany(mappedBy = "report")
    private List<ReportContent> contents;

    // ...
}

并且

public class ReportContent {

    // ...

    @ManyToOne
    @JoinColumn(name = "Uuid")
    private Report report;

    // ...
}