Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
2016-03-20 21:49
579 查看
0.
1.
2.
3.
4.
5.
6.
7.
1.
drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS( ID bigint not null, NAME varchar(15), primary key (ID) ); create table TEACHERS( ID bigint not null, NAME varchar(15), primary key(ID) ); create table LEARNING( MONKEY_ID bigint not null, TEACHER_ID bigint not null, primary key(MONKEY_ID,TEACHER_ID) ); alter table LEARNING add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID); alter table LEARNING add index IDX_TEACHER(TEACHER_ID), add constraint FK_TEACHER foreign key (TEACHER_ID) references TEACHERS(ID);
2.
<?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="mypack.Monkey" table="MONKEYS" > <id name="id" type="long" column="ID"> <generator class="increment"/> </id> <property name="name" column="NAME" type="string" /> <set name="teachers" table="LEARNING" lazy="true" cascade="save-update"> <key column="MONKEY_ID" /> <many-to-many class="mypack.Teacher" column="TEACHER_ID" /> </set> </class> </hibernate-mapping>
3.
<?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="mypack.Teacher" table="TEACHERS" > <id name="id" type="long" column="ID"> <generator class="increment"/> </id> <property name="name" column="NAME" type="string" /> </class> </hibernate-mapping>
4.
package mypack; import java.util.Set; import java.util.HashSet; public class Monkey { private Long id; private String name; private Set teachers=new HashSet(); public Monkey(String name, Set teachers) { this.name = name; this.teachers = teachers; } /** default constructor */ public Monkey() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Set getTeachers() { return this.teachers; } public void setTeachers(Set teachers) { this.teachers = teachers; } }
5.
package mypack; public class Teacher{ private Long id; private String name; /** full constructor */ public Teacher(String name ) { this.name = name; } /** default constructor */ public Teacher() { } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } }
6.
package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util.*; public class BusinessService{ public static SessionFactory sessionFactory; static{ try{ Configuration config = new Configuration().configure(); sessionFactory = config.buildSessionFactory(); }catch(RuntimeException e){e.printStackTrace();throw e;} } public void saveMonkey(Monkey monkey){ Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(monkey); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } public Monkey loadMonkey(Long id){ Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Monkey monkey=(Monkey)session.get(Monkey.class,id); Hibernate.initialize(monkey.getTeachers()); tx.commit(); return monkey; }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } public void printMonkey(Monkey monkey){ Set teachers=monkey.getTeachers(); Iterator it=teachers.iterator(); while(it.hasNext()){ Teacher teacher=(Teacher)it.next(); System.out.println(monkey.getName()+" "+teacher.getName()); } } public void test(){ Teacher teacher1=new Teacher("¶þÀÉÉñ"); Teacher teacher2=new Teacher("ºìº¢¶ù"); Monkey monkey1=new Monkey(); monkey1.setName("ÖǶàÐÇ"); monkey1.getTeachers().add(teacher1); monkey1.getTeachers().add(teacher2); Monkey monkey2=new Monkey(); monkey2.setName("ÀÏÍçͯ"); monkey2.getTeachers().add(teacher1); saveMonkey(monkey1); saveMonkey(monkey2); monkey1=loadMonkey(monkey1.getId()); printMonkey(monkey1); } public static void main(String args[]){ new BusinessService().test(); sessionFactory.close(); } }
7.
相关文章推荐
- 利息的后续要求
- 主要几个浏览器的内核是什么
- UVA 10895——Matrix Transpose
- 选择排序
- Java开发流程自动化(svn+maven+nexus+jenkins) --自动编译,持续集成,测试,打包,发布/部署
- [Chromium中文文档]多进程资源加载
- android中Martix中的数学原理
- Java客户端C++服务端Socket交互通信
- Hibernate Tools for Eclipse的使用
- 从头到尾彻底解析Hash表算法
- SQL SERVER-identity | @@identity | scope_identity
- 博客园环境测试
- 利用Spring AOP 更新memcached 缓存策略的实现(一)
- Lucene 加权的值
- Spark读取数据库(Mysql)的四种方式讲解
- hdoj1060Leftmost Digit
- Hibernate Tools-代码生成
- 快速排序
- 面向对象下-2
- linux性能测试之nmon性能监控工具安装和使用分析