首页 > 代码库 > hibernate中对象与对象的关系(二):list,一对一,多对多

hibernate中对象与对象的关系(二):list,一对一,多对多

①:list  还是用学生和班级举例子,list是将学生添加进list集合时的顺序

Clazz类

public class Clazz {
    private Long id;
    private String name;
    private List<Student> stus = new ArrayList<>();
}

Student类:

public class Student {
    private Long id;
    private String name;
    private Clazz clz;//Clazz类为Student的字段
}

Clazz.hbm.xml;

<hibernate-mapping package="objectn.exus.s3_list">
    <class name="Clazz">
        <id name="id" column="c_id">
            <generator class="native"/>
        </id>
      <property name="name" />

    <list name="stus">
        <key column="c_id"></key>
        <!-- list是有序的,将学生对象添加到list集合的顺序 -->
        <index column="index123" ></index>
        <one-to-many class="Student"/>
    </list>

    </class>
</hibernate-mapping>

Student.hbm.xml;

<hibernate-mapping package="objectn.exus.s3_list">
    <class name="Student">
        <id name="id">
            <generator class="native"/>
        </id>
      <property name="name" />
      <!-- 多对一,name为类中的变量 ,column为生成的列名-->
      <many-to-one name="clz" column="c_id"></many-to-one>
    </class>
</hibernate-mapping>

②:一对一  即身份证号对应每一个人

IDCard类:

public class IDCard {
    private String name;
    private Long id;
    private Person per;
}

Person类:

public class Person {
    private Long id;
    private String name;
    private IDCard card;
}

IDcard.hbm.xml;

<hibernate-mapping package="objectn.exus.s4_one2one">
    <class name="IDCard">
        <id name="id" column="c_id">
            <generator class="foreign">
                <param name="property">per</param>
            </generator>
        </id>
      <property name="name" />
      <!-- constrained:添加一个主键约束 -->
      <one-to-one name="per" constrained="true"/>
    </class>
</hibernate-mapping>

person.hbm.xml;

<hibernate-mapping package="objectn.exus.s4_one2one">
    <class name="Person">
        <id name="id">
            <generator class="native"/>
        </id>
      <property name="name" />
      <one-to-one name="card"/>
    </class>
</hibernate-mapping>

测试类:

public void testsave12() {
        IDCard card = new IDCard("asdasd");
        Person p = new Person("张三");
        card.setPer(p);
        p.setCard(card);
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        session.save(p);//先保存主键
        session.save(card);
        transaction.commit();
        session.close();
}

③:多对多,即学生和老师的关系

Student类:

 

public class Student {
    private Long id;
    private String name;
    private Set<Teacher> teac = new HashSet<>();
}

 

Teacher类:

public class Teacher {
    private Long id;
    private String name;
    private Set<Student> stus = new HashSet<>();
}

Student.hbm.xml;

<hibernate-mapping package="objectn.exus.s5_many2many">
    <class name="Student">
        <id name="id" column="s_id">
            <generator class="native"></generator>
        </id>
      <property name="name" />
         <!--  
        t_tea_stu: 设置中间表的表名
        -->
      <set name="teac" table="t_tea_stu" >
        <!-- 
        key:代表中间表的和当前对象对应的ID的列名
            设置中间表的那一列关联到当前表
        -->
          <key  column="id_stu"/>
          <!--  
        class: 设置对方的类名
        column: 代表对方对象在中间表的列名
                设置中间表的哪一列关联到对方的表 
        -->
          <many-to-many class="Teacher" column="id_tea" ></many-to-many>
      </set>
    </class>
</hibernate-mapping>

teacher.hbm.xml;

<hibernate-mapping package="objectn.exus.s5_many2many">
    <class name="Teacher">
        <id name="id">
            <generator class="native"/>
        </id>
      <property name="name" />
      <set name="stus" table="t_tea_stu">
          <key column="id_tea"></key>
          <many-to-many class="Student" column="id_stu"></many-to-many>
      </set>
    </class>
</hibernate-mapping>

 

hibernate中对象与对象的关系(二):list,一对一,多对多