hibernate入门教程3-----多对多的例子
2007-03-30 10:47
555 查看
Hibernate入门--多对多的例子 | |
所有代码使用上一篇中的。 在Person类中加入: public Set getCourses() { return courses; } private void setCourses(Set courses) { this.courses = courses; } 在Person.hbm.xml文件<property name="sex"/>后加入: <set name="courses" table="PERSON_COURSE"> <key column="PERSON_ID"/> <many-to-many column="COURSE_ID" class="examples.Course"/> </set> 这个set对应Person类中的courses,我们知道多对多要从两个表变成三个表,talb e指明中间关系表的名称,key指明中间表中Person的标识字段,many-to-many表示 是一个多对多关系 新建一个Course类: package examples; import java.util.HashSet; import java.util.Set; public class Course { private Long id; private String name; private Set persons = new HashSet(); public Set getPersons() { return persons; } private void setPersons(Set persons) { this.persons = persons; } public Long getId() { return id; } private void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 新建相应的Course.hbm.xml文件: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="examples.Course"> <id name="id"> <generator class="increment"/> </id> <property name="name"/> <set name="persons" inverse="true" table="PERSON_COURSE"> <key column="COURSE_ID"/> <many-to-many column="PERSON_ID" class="examples.Person"/> </set> </class> </hibernate-mapping> 需要说明的是inverse=true的属性,多对多有一方是主控方,这里应该是Person类 ,所以Course类加上inverse=true 在hibernate.cfg.xml中加入 <mapping resource="examples/Course.hbm.xml"/> 在Test.java中加入: Course c1 = new Course(); Course c2 = new Course(); Course c3 = new Course(); c1.setName("c1"); c2.setName("c2"); c3.setName("c3"); p1.getCourses().add(c2); p2.getCourses().add(c3); p3.getCourses().add(c1); session.save(c1); session.save(c2); session.save(c3); 修改Test.java的fetchData方法中的打印语句为: System.out.println("" + p.getId() +" :" + p.getName() + " :" + p.getSex( ) + " :" + ((Course)p.getCourses().iterator().next()).getName()); 运行,应该有结果: 1 :p1 :M :c2 3 :p3 :M :c1 相应的sql语句为: 11:21:34,921 DEBUG SchemaUpdate:142 - create table Course (id bigint not null, name varchar(255), primary key (id)) 11:21:34,921 DEBUG SchemaUpdate:142 - create table PERSON_COURSE (PERSON _ID bigint not null, COURSE_ID bigint not null, primary key (PERSON_ID, COURSE_ID)) 11:21:34,921 DEBUG SchemaUpdate:142 - create table Person (id bigint not null, name varchar(255), sex char(1), primary key (id)) 11:21:34,937 DEBUG SchemaUpdate:142 - alter table PERSON_COURSE add cons traint FKE448459882F6FF foreign key (PERSON_ID) references Person 11:21:34,937 DEBUG SchemaUpdate:142 - alter table PERSON_COURSE add cons traint FKE44845A766793F foreign key (COURSE_ID) references Course 11:21:34,937 INFO SchemaUpdate:153 - schema update complete 11:21:34,937 INFO SessionFactoryImpl:379 - Checking 0 named queries Hibernate: insert into Person (name, sex, id) values (?, ?, ?) Hibernate: insert into Person (name, sex, id) values (?, ?, ?) Hibernate: insert into Person (name, sex, id) values (?, ?, ?) Hibernate: insert into Course (name, id) values (?, ?) Hibernate: insert into Course (name, id) values (?, ?) Hibernate: insert into Course (name, id) values (?, ?) Hibernate: insert into PERSON_COURSE (PERSON_ID, COURSE_ID) values (?, ? ) Hibernate: insert into PERSON_COURSE (PERSON_ID, COURSE_ID) values (?, ? ) Hibernate: insert into PERSON_COURSE (PERSON_ID, COURSE_ID) values (?, ? ) Hibernate: select person0_.id as col_0_0_ from Person person0_ where per son0_.sex=? Hibernate: select person0_.id as id0_, person0_.name as name0_0_, person 0_.sex as sex0_0_ from Person person0_ where person0_.id=? Hibernate: select person0_.id as id0_, person0_.name as name0_0_, person 0_.sex as sex0_0_ from Person person0_ where person0_.id=? Hibernate: select courses0_.PERSON_ID as PERSON1_1_, courses0_.COURSE_ID as COURSE2_1_, course1_.id as id0_, course1_.name as name2_0_ from PERS ON_COURSE courses0_ inner join Course course1_ on courses0_.COURSE_ID=co urse1_.id where courses0_.PERSON_ID=? Hibernate: select courses0_.PERSON_ID as PERSON1_1_, courses0_.COURSE_ID as COURSE2_1_, course1_.id as id0_, course1_.name as name2_0_ from PERS ON_COURSE courses0_ inner join Course course1_ on courses0_.COURSE_ID=co urse1_.id where courses0_.PERSON_ID=? |
相关文章推荐
- Hibernate入门配置详解及例子
- Hibernate入门教程1
- struts+hibernate+spring一个入门的实用例子(上)
- JPA入门例子(采用JPA的hibernate实现版本)
- 06_传智播客hibernate教程_完善HibernateUtil类及hql查询入门 &amp;amp;&amp;amp;07_传智播客hibernate教程_实体类或属性名与数据库关键字冲突问题
- hibernate官方入门教程
- Hibernate+Junit-史上最详细图解教程-从环境配置到入门
- Java事务处理全解析(八)——分布式事务入门例子(Spring+JTA+Atomikos+Hibernate+JMS)
- Caffe 深度学习入门教程 运行caffe自带的两个简单例子
- hibernate入门教程2增删查改
- JPA入门例子(采用JPA的hibernate实现版本)
- Hibernate 简易入门教程
- iBatis.Net 入门例子 入门指南 入门教程
- MyEclipse Hibernate 入门教程中文版(含视频)
- Hibernate入门1 - Hibernate概述及第一个小例子
- 入门例子:myeclipse开发hibernate(SQLServer)
- hibernate官方入门教程
- 【Java基础】JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本) .
- Java事务处理全解析(八)——分布式事务入门例子(Spring+JTA+Atomikos+Hibernate+JMS)