hibernate基于注解实现映射关系的配置

关联关系的配置步骤

①要理清楚管理关系
②确定管理依赖关系的哪一方

1一对一
例如:person 和IdCard

  ①确定依赖关系:一对一

  ②依赖关系由person类管理
代码如下:

person:

@Entity
@Table(name="t_person")
public class Person
{
    private int id;
    private String name;
    private IDCard card;
    
    @OneToOne(mappedBy="person")  --->  指定了OneToOne的关联关系,
   @joinColmun(name="idcard")
public IDCard getCard() { return card; } public void setCard(IDCard card) { this.card = card; } @Id @GeneratedValue 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; } }

IDCard:

@Entity
@Table(name="t_id_card")
public class IDCard
{
    private int id;
    private String no;
    private Person person;
    
    @Id
    @GeneratedValue
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getNo()
    {
        return no;
    }
    public void setNo(String no)
    {
        this.no = no;
    }
    @OneToOne(mappedBy="idCard")--->  OnetoOne指定了一对一的关联关系,一对一中随便指定一方来维护映射关系,mappedBy同样指定由对方来进行维护关联关系,这里选择Person来进行维护   public Person getPerson() { 
return person;
}
publicvoid setPerson(Person person) {
this.person = person;
}
}

2一对多

例如:teacher和student

  ①确定依赖关系:一对多

  ②依赖关系由多方(student)管理

代码:

 teacher:

public class Teacher {

    
    private String name;
    private  Set<Student> student=new HashSet<Student>();
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @OneToMany(mappedBy="teacher",fetch=FetchType.LAZY)
    public Set<Student> getStudent() {
        return student;
    }
    public void setStudent(Set<Student> student) {
        this.student = student;
    }
    
}

student:

public class Student{
    private String name;
    private  Teacher teacher;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="teacher")
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    
}

3多对多

例如:classRoom和student

  ①确定依赖关系:多对多

  ②依赖关系由多方(student)管理

classRoom:

public class ClassRoom{

    private String name;
    private Set<Student> students=new HashSet<Student>();
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @ManyToMany(mappedBy="calssRooms",fetch=FetchType.LAZY)
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    
    
}

student:

public class Student{
    private String name;
    private  Set<ClassRoom> classRooms=new HashSet<ClassRoom>();
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="xx_classRoom_student")
    public Set<ClassRoom> getClassRooms() {
        return classRooms;
    }
    public void setClassRooms(Set<ClassRoom> classRooms) {
        this.classRooms = classRooms;
    }
    
}

 

posted @ 2015-12-29 16:13  Jeremy_software  阅读(218)  评论(0编辑  收藏  举报