gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:49:08 26 4
gpt4 key购买 nike

我有两个与键“Uuid”相关的 hibernate 对象“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
}

我该怎么做,选择Report并hibernate发送sql以获取条件为Report.Uuid = ReportContent.Uuid的ReportContents?

最佳答案

尝试类似的事情

public class Report {

// ...

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

// ...
}

public class ReportContent {

// ...

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

// ...
}

关于mysql - 如何使用@OneToMany链接两个表而不使用外键或连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38196974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com