简体   繁体   English

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

[英]How to use @OneToMany link two table without make foreign key or join table

i have two hibernate object "Report" and "ReportContent" that are related with key "Uuid" 我有两个与键“ Uuid”相关的休眠对象“ Report”和“ ReportContent”

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
}

how do i do, that select Report and hibernate send sql to get ReportContents with the condition Report.Uuid = ReportContent.Uuid? 我该怎么办,即选择Report并休眠发送sql以获取带有Report.Uuid = ReportContent.Uuid条件的ReportContents?

Try something like 尝试类似

public class Report {

    // ...

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

    // ...
}

And

public class ReportContent {

    // ...

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

    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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