首先要创建要映射的实体类
一对多映射,一个人可以有多个收货地址
User.java
package star.july.b_annotation;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
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;
/**
* 使用注解映射
*/
/**
* @author Starjuly
*
*/
//实体类
@Entity
//表名称
@Table(name="t_user")
public class User {
@Id
//自增长
@GeneratedValue
@Column
private int id;
@Column(name="uName")
private String name;
//一对多连接
//级联操作:CascadeType.ALL
//主键维护策略:mappedBy:把关联维护权交给Address的user
@OneToMany(cascade={CascadeType.ALL},mappedBy="user")
//加入外键
//使用mappedBy后就不能使用JoinColumn和JoinTable了
// @JoinColumn(name="uid")
private Set<Address> address = new HashSet<Address>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
}
Address.java
package star.july.b_annotation;
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;
@Entity
@Table(name="t_address")
public class Address {
@Id
@GeneratedValue
@Column
private int id;
@Column(name="adsName")
private String name;
@Column
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//多对一连接
@ManyToOne
@JoinColumn(name="uid")
private User user = new User();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
再要hibernate.cfg.xml中配置映射类
<!-- 映射类 -->
<
mapping
class
=
"star.july.b_annotation.User"
/>
<
mapping
class
=
"star.july.b_annotation.Course"
/>
测试类,包括一对多,多对多,一对一
package star.july.b_annotation;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import star.july.util.HibernateUtil;
//测试注解配置
public class TestDemo {
//一对多连接
@Test
public void test(){
Session session = HibernateUtil.getSession();
Transaction ts = session.getTransaction();
try{
ts.begin();
User u = new User();
u.setName("徐凤年");
Address ads = new Address();
ads.setName("姜妮");
ads.setAddress("北凉龙虎山");
Address ads2 = new Address();
ads2.setName("婓南苇");
ads2.setAddress("北凉胭脂郡");
u.getAddress().add(ads);
u.getAddress().add(ads2);
ads.setUser(u);
ads2.setUser(u);
session.save(u);
ts.commit();
}catch(Exception e ){
e.printStackTrace();
ts.rollback();
}
}
//一对多连接
@Test
public void test2(){
Session session = HibernateUtil.getSession();
Transaction ts = session.getTransaction();
try{
ts.begin();
Student s1 = new Student();
s1.setName("小火");
Student s2 = new Student();
s2.setName("小火");
Course c1 = new Course();
c1.setName("java开发");
Course c2 = new Course();
c2.setName("java开发");
s1.getCourse().add(c1);
s1.getCourse().add(c2);
s2.getCourse().add(c1);
c1.getStudent().add(s1);
c1.getStudent().add(s2);
c2.getStudent().add(s1);
session.save(s1);
session.save(s2);
session.save(c1);
session.save(c2);
ts.commit();
}catch(Exception e ){
e.printStackTrace();
ts.rollback();
}
}
//一对一连接
@Test
public void test3(){
Session session = HibernateUtil.getSession();
Transaction ts = session.getTransaction();
try{
ts.begin();
Person p1 = new Person();
p1.setName("左莫");
Person p2 = new Person();
p2.setName("杨紫");
IdCard c1 = new IdCard();
c1.setName("232443453");
IdCard c2 = new IdCard();
c2.setName("5687651543");
p1.setCard(c1);
p2.setCard(c2);
c1.setPerson(p1);
c2.setPerson(p2);
session.save(p1);
session.save(p2);
session.save(c1);
session.save(c2);
ts.commit();
}catch(Exception e ){
e.printStackTrace();
ts.rollback();
}
}
}
接下来是多对多的注解映射
Course
package star.july.b_annotation;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
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;
@Entity
@Table(name="t_course")
public class Course {
@Id
@GeneratedValue
@Column
private int id;
@Column(name="cName")
private String name;
@ManyToMany(mappedBy="course",cascade={CascadeType.ALL})
/*@JoinTable(
//中间表的表名
name="student_course",
//当前表的外键
joinColumns=@JoinColumn(name="cid"),
//关联表的外键
inverseJoinColumns=@JoinColumn(name="sid")
)*/
private Set<Student> student = new HashSet<Student>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
}
Student..java
package star.july.b_annotation;
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.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.hibernate.annotations.CascadeType;
@Entity
@Table(name="t_student")
public class Student {
@Id
@GeneratedValue
@Column
private int id;
@Column(name="sName")
private String name;
@ManyToMany
@JoinTable(
//中间表的表名
name="student_course",
//当前表的外键
joinColumns=@JoinColumn(name="sid"),
//关联表的外键
inverseJoinColumns=@JoinColumn(name="cid")
)
private Set<Course> course = new HashSet<Course>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Course> getCourse() {
return course;
}
public void setCourse(Set<Course> course) {
this.course = course;
}
}
最后是一对一的注解映射
人和身份证
摘取其中最重要的代码片段:
Person.java
@OneToOne
(cascade={CascadeType.
ALL
},mappedBy=
"person"
)
private
IdCard
card
;
IdCard.java
@OneToOne
//外键唯一
@JoinColumn
(name=
"uid"
,unique=
true
)
private
Person
person
;