数据库mysql:
1.一对一
person50表password50表是一对一的关系; password50表中有外键 person_id
person实体类:
package com.c50.entity; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="person50") public class Person { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private String name; private Integer age; private Date birth; //关系属性 //mappedBy="对方中映射着外键的关系属性"=="对方的关系属性" @OneToOne(mappedBy="person50") //ALL:增删改都可以级联(cascade) //save-update:增加和更新可以级联 //delete:删除可以级联 //如果不设置,则增删改都不可以级联 //注意:查询例外,不受级联权限的控制,默认级联。 @Cascade(value=CascadeType.ALL) private Passport passport50; public Passport getPassport50() { return passport50; } public void setPassport50(Passport passport50) { this.passport50 = passport50; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + ", birth=" + birth + "]"; } public Person(){} public Person(Integer id, String name, Integer age, Date birth) { super(); this.id = id; this.name = name; this.age = age; this.birth = birth; } }
password实体类: package com.c50.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="passport50") public class Passport { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private String note; private Integer expire; //关系属性 @OneToOne @JoinColumn(name="person_id") //@Cascade private Person person50; public Person getPerson50() { return person50; } public void setPerson50(Person person50) { this.person50 = person50; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public Integer getExpire() { return expire; } public void setExpire(Integer expire) { this.expire = expire; } @Override public String toString() { return "Passport [id=" + id + ", note=" + note + ", expire=" + expire + "]"; } public Passport(){} public Passport(Integer id, String note, Integer expire) { super(); this.id = id; this.note = note; this.expire = expire; } }
2.一对多
user50表和order50表是一对多的关系 ; order50表有外键: user_id
1)双向关系:user可以找到order,order也可以找到user
user类:
package com.c50.entity; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; //代表此类参与ORM映射,此注解必须要有 @Entity //代表user这个类映射了一个表user50,如果表名和类名一样,此注解可以省略 @Table(name="user50") //读写缓存权限 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) public class User { //主键,此注解必须要有 @Id //数据库表中主键列名为id,如果实体类属性名和列名相同,此注解可以省略 @Column(name="id") //主键的维护策略 @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer userID; private String name; private Integer age; @Column(name="birth") //此属性是日期类型,time:只保存时间 date:只保存日期 timestamp:(默认)日期+时间 @Temporal(TemporalType.DATE) private Date birthday; //关系属性 //mappedBy="对方中映射着外键的关系属性"=="order类中关系属性为user" //延迟加载:fetch = FetchType.LAZY @OneToMany(mappedBy="user",fetch=FetchType.LAZY) //ALL:增删改都可以级联(cascade) //save-update:增加和更新可以级联 //delete:删除可以级联 //如果不设置,则增删改都不可以级联 //注意:查询例外,不受级联权限的控制,默认级联。 @Cascade(value=CascadeType.DELETE) private Set<Order> orders=new HashSet<Order>(); public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } public User(){} public User(Integer userID, String name, Integer age, Date birthday) { super(); this.userID = userID; this.name = name; this.age = age; this.birthday = birthday; } public Integer getUserID() { return userID; } public void setUserID(Integer userID) { this.userID = userID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [userID=" + userID + ", name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } }
order类:
package com.c50.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="order50") public class Order { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private Double price; private String note; //关系属性 @ManyToOne //数据库中order表,外键名为user_id,指向user表 @JoinColumn(name="user_id") private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "Order [id=" + id + ", price=" + price + ", note=" + note + "]"; } public Order(){} public Order(Integer id, Double price, String note) { super(); this.id = id; this.price = price; this.note = note; } }
2)单向关系:由user维护外键,user可以级联order,order不可以级联user
user实体:
package com.c50.entity.single; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="user50") public class User { @Id @Column(name="id") @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer userID; private String name ; private Integer age; @Column(name="birth") @Temporal(TemporalType.DATE) private Date birthday; //@OneToMany(mappedBy="user") //单向 @OneToMany @JoinColumn(name="user_id") @Cascade(value=CascadeType.SAVE_UPDATE) private Set<Order> orders=new HashSet<Order>(); public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } public User(){} public User(Integer userID, String name, Integer age, Date birthday) { super(); this.userID = userID; this.name = name; this.age = age; this.birthday = birthday; } public Integer getUserID() { return userID; } public void setUserID(Integer userID) { this.userID = userID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [userID=" + userID + ", name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } }
package com.c50.entity.single; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="user50") public class User { @Id @Column(name="id") @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer userID; private String name ; private Integer age; @Column(name="birth") @Temporal(TemporalType.DATE) private Date birthday; //@OneToMany(mappedBy="user") //单向 @OneToMany @JoinColumn(name="user_id") @Cascade(value=CascadeType.SAVE_UPDATE) private Set<Order> orders=new HashSet<Order>(); public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } public User(){} public User(Integer userID, String name, Integer age, Date birthday) { super(); this.userID = userID; this.name = name; this.age = age; this.birthday = birthday; } public Integer getUserID() { return userID; } public void setUserID(Integer userID) { this.userID = userID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [userID=" + userID + ", name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } }
order实体:无user类的信息
package com.c50.entity.single; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="order50") public class Order { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private Double price; private String note; //关系属性 /*@ManyToOne @JoinColumn(name="user_id") private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }*/ public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "Order [id=" + id + ", price=" + price + ", note=" + note + "]"; } public Order(){} public Order(Integer id, Double price, String note) { super(); this.id = id; this.price = price; this.note = note; } }
3.多对多
course50表和student50表是多对多的关系; r_c_s表关系,有外键:cour_id和stu_id
course实体:
package com.c50.entity; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="course50") public class Course { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private String name; private Integer duration; //关系属性 @ManyToMany @JoinTable(name="r_c_s" ,joinColumns={@JoinColumn(name="cour_id")} ,inverseJoinColumns={@JoinColumn(name="stu_id")}) @Cascade(value=CascadeType.ALL) private Set<Student> students=new HashSet<Student>(); public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getDuration() { return duration; } public void setDuration(Integer duration) { this.duration = duration; } @Override public String toString() { return "Course [id=" + id + ", name=" + name + ", duration=" + duration + "]"; } public Course(){} public Course(Integer id, String name, Integer duration) { super(); this.id = id; this.name = name; this.duration = duration; } }
student实体:
package com.c50.entity; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="student50") public class Student { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private String name; private Integer age; //关系属性:实例化,接口,泛型 @ManyToMany(fetch=FetchType.EAGER) //中间表,两个外键: joinColumns=指向自己的外键 inverseJoinColumns=指向对方的外键 @JoinTable(name="r_c_s" ,joinColumns={@JoinColumn(name="stu_id")} ,inverseJoinColumns={@JoinColumn(name="cour_id")}) @Cascade(value=CascadeType.ALL) private Set<Course> courses=new HashSet<Course>(); public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } public Student(){} public Student(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } }