[英]JPA Hibernate one to many
Really confused by how one to many works in JPA, all the documents that I read, uses both one to many and many to one in their example, and I don't know if they are necessary or not, and it doesn't work when I tried it. 我对JPA中一对多的工作方式感到困惑,我阅读的所有文档在其示例中都使用了一对多和多对一的方式,而且我不知道它们是否是必需的,并且不起作用当我尝试过。
My question is, suppose I have two tables, and I want to populate the College object using findCollegeData() method, so that all the student in this college are in a list when I initialize the object. 我的问题是,假设我有两个表,并且我想使用findCollegeData()方法填充College对象,以便在初始化对象时该学院的所有学生都在列表中。
Below is my approach, I am able to store all the students in the college list using storeCollegeData() method, but I am not able to retrieve the college object fully, the student list is always empty, even though the data is in the database, and it works if I try to search for student using college name directly. 下面是我的方法,我可以使用storeCollegeData()方法将所有学生存储在大学列表中,但是我无法完全检索该大学对象,即使数据在数据库中,学生列表也始终为空,并且如果我尝试直接使用大学名称搜索学生的话,该方法也适用。
public static EntityManager entityManager = something;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public College {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int cId;
private String collegeName;
private int numOfStudent;
@OneToMany(mappedBy="collegeName", cascade=CascadeType.ALL, orphanRemoval=true)
private List<Student> studentList = new ArrayList<>();
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public Student {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int sId;
private String name;
private String collegeName;
private String city;
}
// college.getStudentList is always empty and I don't know why
public findCollegeData(String collegeName) {
College college = entityManager.find(College.class, collegeName);
}
// Student data in the studentList are inserted into student table
public storeCollegeData(College college) {
entityManager.persist(college);
}
// This method works
public findStudent(String collegeName) {
CriteriaBuilder cb = provider.get().getCriteriaBuilder();
CriteriaQuery<Student> query = cb.createQuery(Student.class);
Root<Student> student = query.from(Student.class);
query.where(
cb.and(
cb.equal(student.get("collegeName"), collegeName)
)
);
JobStatisticDB Student = provider.get().createQuery(query).getSingleResult();
}
Am i missing something??? 我想念什么吗??? Is join more appropriate than map here???
加入比这里的地图更合适吗??? I dont know wat to do man
我不知道做男人的w
EDITED: Got it to work by changing both of the collegeName as the primary key of table by adding @Id annotation, however though, how can I add an sId and cId to the table, so they can have duplicate college name???? 编辑:通过添加@Id批注将两个collegeName更改为表的主键,使其起作用,但是,如何将sId和cId添加到表中,以便它们可以具有重复的大学名称? Right now, I can't have duplicate college with the same name, and student that that goes to the same college!
现在,我不能有重复的同名大学和去同一所大学的学生!
Final Edited: Changed database design to use foreign key see solution below 最终编辑:更改数据库设计以使用外键,请参见下面的解决方案
The field you reference in mappedBy
must contain a value that equates to College
's id field. 您在
mappedBy
引用的字段必须包含等于College
的id字段的值。 Change it to collegeName
instead of city
, and it should work. 将其更改为
collegeName
而不是city
,它应该可以工作。
The accepted answer is incorrect: you define relationships between entities . 接受的答案不正确:您定义了实体之间的关系。 The mappings should be as below for a bi-directional
@OneToMany
双向
@OneToMany
的映射应如下所示
College: 学院:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public College {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int cId;
private String collegeName;
private int numOfStudent;
@OneToMany(mappedBy="college", cascade=CascadeType.ALL, orphanRemoval=true)
private List<Student> studentList = new ArrayList<>();
}
Student: 学生:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public Student {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int sId;
private String name;
private String city;
//student table has a FK column college_id
@ManyToOne
@JoinColumn(name = "college_id")
private College college;
}
EntityManager find() takes the PK as an argument: EntityManager find()将PK作为参数:
public findCollege(int collegeId) {
College college = entityManager.find(College.class, collegeId);
college.getStudents(); //will be populated
}
public findStudent(int studentId) {
Student student = entityManager.find(Student.class, studentId);
student.getCollege(); //will be populated
student.getCollege().getStudents(); //will be populated
}
If you want to find a college by name create a JPQL or Criteria query: 如果要按名称查找大学,请创建JPQL或Criteria查询:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.