In the previous blog, "A Mouthful of eating hibernate (d)-multi-One one-way association mapping", introduced a Many-to-many association mapping, today on the contrary to a One-to-many one-way association mapping.
There may be some confusion about the topic of these 2 blogs, which is a little different from the daily story. We talk about such as father-son relationship, the relationship between husband and wife is a two-way relationship, and now the discussion is one-way relationship, so there are more than one and a pair of many of the argument.
The relationship between the two is actually very simple, but the angle is different. For example, the relationship between students and classes. If you look at it from a student's point of view, it's a many-to-many relationship. From the class point of view, it is a one-to-many relationship. The argument is simple, but the object and the relationship are built differently.
Let's take a look at a couple of class diagrams (seemingly many people are wrong about the aggregation):
Hibernate for a one-to-many relationship is handled by manipulating the class end, indirectly, or automatically manipulating the student end. For example, add, I directly add the class end of the data, multiple student will be added automatically added. You can also use this class to get all the corresponding student information. Follow me to configure it:
First define entity class "Class" "Student"
Package com.bjpowernode.hibernate;
Import Java.util.Set;
/** * Class * @author Longxuan * */public class class {private int id;
private String name;
Private set<student> students;
Public set<student> getstudents () {return students;
public void Setstudents (set<student> students) {this.students = students;
public int getId () {return id;
The public void setId (int id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
}} package com.bjpowernode.hibernate; /** * Student Class *url:http://www.bianceng.cn/programming/java/201410/45831.htm * @author Longxuan * */public class Stud
ENT {private int id; Private String name;
public int getId () {return id;
The public void setId (int id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name; }
}