Hibernate实体映射关系配置

Hibernate实体关系:双向关联,mappedBy必设

注解方式

1、一对一单向

@Entity       

public class Husband{

           @Id

           @GenerateValue

             pirvate int id;

             private String name;

            @OneToOne

            @JoinColumn(name="wifeId")

             private Wife wife;

      }

         @Entity

     public class Wife{

           @Id

           @GenerateValue

         private int id;

         private String name;

    }

2.、一对一双向

@Entity       

public class Husband{

           @Id

           @GenerateValue

             pirvate int id;

             private String name;

            @OneToOne

            @JoinColumn(name="wifeId")

             private Wife wife;

      }

 

         @Entity

     public class Wife{

           @Id

           @GenerateValue

         private int id;

         private String name;

       @OneToOne(mappedBy="wife")

        private Husband husband;

    }

3.、一对多单向

package com.bjsxt.hibernate;
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.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_group")
public class Group {
 private int id;
 private String name;
 private Set<User> users = new HashSet<User>();
 @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;
 }
 @OneToMany
 @JoinColumn(name="groupId")  //
不加出现3张表
 public Set<User> getUsers() {
  return users;
 }
 public void setUsers(Set<User> users) {
  this.users = users;
 }
}

package com.bjsxt.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
 private int id;
 private String name;
 @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;
 }
}

4、多对一单向

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_group")
public class Group {
 private int id;
 private String name;
 @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;
 }
}

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {
 private int id;
 private String name;
 private Group group;
 @ManyToOne
 
 public Group getGroup() {
  return group;
 }
 public void setGroup(Group group) {
  this.group = group;
 }
 @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;
 }
}

5、一对多双向

package com.bjsxt.hibernate;
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.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_group")
public class Group {
 private int id;
 private String name;
 private Set<User> users = new HashSet<User>();
 @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;
 }
 @OneToMany(mappedBy="group")
 
 public Set<User> getUsers() {
  return users;
 }
 public void setUsers(Set<User> users) {
  this.users = users;
 }
}

package com.bjsxt.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
 private int id;
 private String name;
 private Group group;
 @ManyToOne
 public Group getGroup() {
  return group;
 }
 public void setGroup(Group group) {
  this.group = group;
 }
 @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;
 }
}

6、多对多单向

@Entity
public class Student {
 private int id;
 private String name;
 @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;
 }
}

@Entity
public class Teacher {
 private int id;
 private String name;
 private Set<Student> students = new HashSet<Student>();
 @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;
 }
 @ManyToMany
 @JoinTable(name="t_s",
  
joinColumns={@JoinColumn(name="teacher_id")},
  
inverseJoinColumns={@JoinColumn(name="student_id")}
  )    //
改变中间表的名字和字段名字
 public Set<Student> getStudents() {
  return students;
 }
 public void setStudents(Set<Student> students) {
  this.students = students;
 }
}

7、多对多双向

@Entity
public class Student {
 private int id;
 private String name;
 private Set<Teacher> teachers = new HashSet<Teacher>();
 @ManyToMany(mappedBy="students")
 public Set<Teacher> getTeachers() {
  return teachers;
 }
 public void setTeachers(Set<Teacher> teachers) {
  this.teachers = teachers;
 }
 @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;
 }
}

@Entity
public class Teacher {
 private int id;
 private String name;
 private Set<Student> students = new HashSet<Student>();
 @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;
 }
 @ManyToMany
 @JoinTable(name="t_s",
  
joinColumns={@JoinColumn(name="teacher_id")},
  
inverseJoinColumns={@JoinColumn(name="student_id")}
  )
 public Set<Student> getStudents() {
  return students;
 }
 public void setStudents(Set<Student> students) {
  this.students = students;
 }
}

XML配置

1、一对一单向

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <many-to-one name="address" 
        column="addressId" 
        unique="true"
        not-null="true"/>
</class>
 
<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
</class>

2.、一对一双向

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <many-to-one name="address" 
        column="addressId" 
        unique="true"
        not-null="true"/>
</class>
 
<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
   <one-to-one name="person" 
        property-ref="address"/>
</class>

3.、一对多单向

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <set name="addresses">
        <key column="personId" 
            not-null="true"/>
     <one-to-many class="Address"/>
    </set>
</class>
 
<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
</class>

4、多对一单向

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <many-to-one name="address" 
        column="addressId"
        not-null="true"/>
</class>
 
<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
</class>

5、一对多双向

<hibernate-mapping>

 <class name="com.bjsxt.hibernate.Group" table="t_group">

  <id name="id">

   <generator class="native"></generator>

  </id>

  

  <property name="name"></property>

  <set name="users" inverse=”true”>

   <key column="groupId"></key>  //<many-to-one>column一致

   <one-to-many class="com.bjsxt.hibernate.User"/>

  </set>

    </class>

 

</hibernate-mapping>

<hibernate-mapping>
 <class name="com.bjsxt.hibernate.User" table="t_user">
  <id name="id">
   <generator class="native"></generator>
  </id>
  
  <property name="name"></property>
  <many-to-one name="group" column="groupId"></many-to-one>
    </class>
</hibernate-mapping>

6、多对多单向

<hibernate-mapping>
 <class name="com.bjsxt.hibernate.Student">
  <id name="id">
   <generator class="native"></generator>
  </id>
  
  <property name="name"></property>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
 <class name="com.bjsxt.hibernate.Teacher">
  <id name="id">
   <generator class="native"></generator>
  </id>
  <property name="name"></property>
  <set name="students" table="t_s">

   <key column="teacher_id"></key>
   <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/>
  </set>
    </class>
</hibernate-mapping>

7、多对多双向

<hibernate-mapping>
 <class name="com.bjsxt.hibernate.Student">
  <id name="id">
   <generator class="native"></generator>
  </id>
  
  <property name="name"></property>
  
<set name="teachers" table="t_s" inverse=”true”>
   <key column="student_id"></key>
   <many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/>
  </set>

    </class>
</hibernate-mapping>

<hibernate-mapping>
 <class name="com.bjsxt.hibernate.Teacher">
  <id name="id">
   <generator class="native"></generator>
  </id>
  
  <property name="name"></property>
  
<set name="students" table="t_s">
   <key column="teacher_id"></key>
   <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/>
  </set>
    </class>
</hibernate-mapping>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值