gpt4 book ai didi

java - 如何在Hibernate中映射多个一对多关联

转载 作者:行者123 更新时间:2023-12-01 22:18:53 25 4
gpt4 key购买 nike

我的数据库中有这 3 个表

CREATE TABLE IF NOT EXISTS `User`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `Project` (
`id` INT NOT NULL AUTO_INCREMENT,
`manager` INT NOT NULL,
`name` VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
FOREIGN KEY (`manager`) REFERENCES `User`(`id`)
)

CREATE TABLE IF NOT EXISTS `Work`(
`id` INT NOT NULL AUTO_INCREMENT,
`project_id` INT NOT NULL DEFAULT -1,
`name` VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
FOREIGN KEY (`project_id`) REFERENCES `Project`(`id`),
FOREIGN KEY (`creator`) REFERENCES `User`(`id`)
)

然后我想使用Hibernate来连接它们。

这些是我的 java 类

用户:

// ... import code
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String name;

@OneToMany(mappedBy = "manager")
private List<Project> projects;

public User() {
}
// ... Getter & Setter code
}

项目:

// ... import code
@Entity
@Table
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String name;

@ManyToOne
@JoinColumn(name = "manager")
private User manager;

public Project () {
}
// ... Getter & Setter code
}

工作:

// ... import code
public class Work {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String name;
@Column(name = "project_id")
private int projectId;

@ManyToOne
@JoinColumn(name = "creator")
private User creator;

public Work() {
}
// ... Getter & Setter code
}

现在可以工作了。

但是,在映射项目和工作的关系(一对多/多对一)后,它给了我错误消息。

我所做的映射是:

将这些代码添加到 Project 类中:

@OneToMany(mappedBy = "project_id")
private List<Work> works;

public List<Work> getWorks() {
return works;
}

public void setWorks(List<Work> works) {
this.works = works;
}

并将这些代码放入 Work 类中:

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
public Project getProject() {
return project;
}

public void setProject(Project project) {
this.project = project;
}

并删除@Column(name = "project_id") private int projectId;

然后它给我错误消息:

Exception in thread "main" org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: database.tables.Work.project_id in database.tables.Project.works
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:768)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)

所以我想知道如何映射项目与工作的关系。

最佳答案

在“Project”类中,代码为:

@OneToMany(mappedBy = "project_id")
private List<Work> works;

并且,在“Work”类中,代码是:

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
//var name should match with mappedBy name in other class

('mappedBy="project_id") 中的名称和其他类(私有(private) Project 项目)中的变量名称都应该匹配。

关于java - 如何在Hibernate中映射多个一对多关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362898/

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