如何在 Hibernate 中映射多个一对多关联
How to map multiple One-to-Many association in Hibernate
我的数据库中有这 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 classes
用户:
// ... 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和Work的关系(一对多/多对一)映射后,它给我错误信息。
我做的映射是:
将这些代码添加到项目中 class:
@OneToMany(mappedBy = "project_id")
private List<Work> works;
public List<Work> getWorks() {
return works;
}
public void setWorks(List<Work> works) {
this.works = works;
}
并将这些代码放入工作class:
@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.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'class中,代码为:
@OneToMany(mappedBy = "project_id")
private List<Work> works;
并且,在'Work'class中,代码是:
@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
//var name should match with mappedBy name in other class
('mappedBy="project_id") 中的名称和另一个 class(私有项目项目)中的变量名称应匹配。
我的数据库中有这 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 classes
用户:
// ... 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和Work的关系(一对多/多对一)映射后,它给我错误信息。
我做的映射是:
将这些代码添加到项目中 class:
@OneToMany(mappedBy = "project_id")
private List<Work> works;
public List<Work> getWorks() {
return works;
}
public void setWorks(List<Work> works) {
this.works = works;
}
并将这些代码放入工作class:
@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.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'class中,代码为:
@OneToMany(mappedBy = "project_id")
private List<Work> works;
并且,在'Work'class中,代码是:
@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
//var name should match with mappedBy name in other class
('mappedBy="project_id") 中的名称和另一个 class(私有项目项目)中的变量名称应匹配。