Hibernate 的多对一、一对多、多对多

配置单向多对一关联

如表:

 

第一步:实现Student表和Teacher表的实体类

第二步:

1、进行Student表 的配置,如下:

<class name="entity.Student">

      <id name="id">

           <generator class="sequence">

                 <param name="sequence">seq_student</param>

           </generator>

      </id>

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

      <many-to-one name="teacher" column="teacherid" class="entity.Teacher"></many-to-one>

</class>

PS:

在学生的配置上的这句代码:<many-to-one name="teacher" column="teacherid" class="entity.Teacher"></many-to-one>
<many-to-one>元素建立了 teacher 属性和Teacher表的teacherid之间的映射。
name:设定持久化类的属性。
column:设定持久化类的属性对应的表的外键。
class:设定持久化的属性的类型。

 

2、进行Teacher表的配置,如下:

<class name="entity.Teacher">

      <id name="id">

            <generator class="sequence">

                 <param name="sequence">seq_teacher</param>

           </generator>

      </id>

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

</class>

第三步:使用hibernate.cfg.xml映射其的路径

<mapping resource="entity/Student.hbm.xml" />

<mapping resource="entity/Teacher.hbm.xml" />

 

第四步:进行数据操作

Session session = HibernateSessionFactory.getSession();

Transaction tx = null;

Teacher teacher = new Teacher("李炎恢");

Student student1 = new Student("吴者然");

Student student2 = new Student("何开");

try {

      student1.setTeacher(teacher);

      student2.setTeacher(teacher);

      tx = session.beginTransaction();

      session.save(teacher);

      session.save(student1);

      session.save(student2);

      tx.commit();

      System.out.println("保存成功!!!");

} catch (HibernateException e) {

      e.printStackTrace();

      tx.rollback();

}finally{

      HibernateSessionFactory.closeSession();

}

 

 

 

效果图:


 

配置单向一对多关联

如表:


 

第一步:实现Teacher2表和Student2表的实体类

第二步:

1、进行Teacher2表 的配置,如下:

<class name="entity.Teacher2">

      <id name="id">

           <generator class="sequence">

                 <param name="sequence">seq_teacher2</param>

           </generator>

      </id>

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

      <set name="student2">

           <key column="teacher2id"></key>

           <one-to-many class="entity.Student2" />

      </set>

</class>

2、进行Student2表 的配置,如下:

<class name="entity.Student2">

      <id name="id">

           <generator class="sequence">

                 <param name="sequence">seq_student2</param>

           </generator>

      </id>

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

</class>

 

第三步:使用hibernate.cfg.xml映射其的路径

<mapping resource="entity/Student2.hbm.xml" />

<mapping resource="entity/Teacher2.hbm.xml" />

 

第四步:进行数据操作

Session session = HibernateSessionFactory.getSession();

Transaction tx = null;

Teacher2 teacher = new Teacher2("李炎恢");

Student2 student1 = new Student2("吴者然");

Student2 student2 = new Student2("何开");

try {

      teacher.getStudent2().add(student1);

      teacher.getStudent2().add(student2);

      tx = session.beginTransaction();

      session.save(teacher);

      session.save(student1);

      session.save(student2);

      tx.commit();

      System.out.println("保存成功!!!");

} catch (HibernateException e) {

      e.printStackTrace();

      tx.rollback();

}finally{

      HibernateSessionFactory.closeSession();

}

 

效果图:


 

 

配置单向多对多关联

如表:


 

这时产生了一个新表

 

第一步:实现Pupil表和Subject表的实体类

第二步:

1、进行Pupil表 的配置,如下:

<class name="entity.Pupil">

      <id name="id">

           <generator class="sequence">

                 <param name="sequence">seq_pupil</param>

           </generator>

      </id>

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

      <set name="subject" table="p_s">

           <key column="p_id" />

           <many-to-many class="entity.Subject" column="s_id" />

      </set>

</class>

2、进行Subject表的配置,如下:

<class name="entity.Subject">

      <id name="id">

           <generator class="sequence">

                 <param name="sequence">seq_subject</param>

           </generator>

      </id>

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

      <set name="pupil" table="p_s">

           <key column="s_id" />

           <many-to-many class="entity.Pupil" column="p_id" />

      </set>

</class>

 

第三步:使用hibernate.cfg.xml映射其的路径

<mapping resource="entity/Pupil.hbm.xml" />

<mapping resource="entity/Subject.hbm.xml" />

 

第四步:进行数据操作

Session session = HibernateSessionFactory.getSession();

Transaction tx = null;

Pupil pupil1 = new Pupil("吴者然");

Pupil pupil2 = new Pupil("景临境");

Subject subject1 = new Subject("Java");

Subject subject2 = new Subject("php");

subject1.getPupil().add(pupil1);

subject1.getPupil().add(pupil2);

subject2.getPupil().add(pupil1);

try {

      tx = session.beginTransaction();

      session.save(pupil1);

      session.save(pupil2);

      session.save(subject1);

      session.save(subject2);

      tx.commit();

      System.out.println("保存成功!!!");

} catch (HibernateException e) {

      e.printStackTrace();

      tx.rollback();

}finally{

      HibernateSessionFactory.closeSession();

}

 

效果图:


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值