Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1.
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="learnings" lazy="true" inverse="true" cascade="save-update">
- <key column="MONKEY_ID" />
- <one-to-many class="mypack.Learning" />
- </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" />
- <set name="learnings" lazy="true" inverse="true" cascade="save-update">
- <key column="TEACHER_ID" />
- <one-to-many class="mypack.Learning" />
- </set>
- </class>
- </hibernate-mapping>
4.
- <?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.Learning" table="LEARNING" >
- <id name="id" type="long" column="ID">
- <generator class="increment"/>
- </id>
- <property name="gongfu" column="GONGFU" type="string" />
- <many-to-one name="monkey" column="MONKEY_ID" class="mypack.Monkey" not-null="true" />
- <many-to-one name="teacher" column="TEACHER_ID" class="mypack.Teacher" not-null="true" />
- </class>
- </hibernate-mapping>
5.
- package mypack;
- import java.util.Set;
- import java.util.HashSet;
- import java.util.Iterator;
- public class Monkey{
- private Long id;
- private String name;
- private Set learnings=new HashSet();
- public Monkey(String name, Set learnings) {
- this.name = name;
- this.learnings = learnings;
- }
- /** 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 getLearnings() {
- return this.learnings;
- }
- public void setLearnings(Set learnings) {
- this.learnings = learnings;
- }
- }
6.
- package mypack;
- import java.util.Set;
- import java.util.HashSet;
- public class Teacher{
- private Long id;
- private String name;
- private Set learnings=new HashSet();
- /** full constructor */
- public Teacher(String name,Set learnings ) {
- this.name = name;
- this.learnings=learnings;
- }
- /** 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;
- }
- public Set getLearnings() {
- return this.learnings;
- }
- public void setLearnings(Set learnings) {
- this.learnings = learnings;
- }
- }
7.
- package mypack;
- public class Learning{
- private Long id;
- private Teacher teacher;
- private Monkey monkey;
- private String gongfu;
- private int quantity;
- public Learning(Teacher teacher,Monkey monkey,String gongfu) {
- this.teacher= teacher;
- this.monkey = monkey;
- this.gongfu=gongfu;
- }
- /** default constructor */
- public Learning() {
- }
- public Long getId() {
- return this.id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getGongfu() {
- return this.gongfu;
- }
- public void setGongfu(String gongfu) {
- this.gongfu = gongfu;
- }
- public Monkey getMonkey() {
- return this.monkey;
- }
- public void setMonkey(Monkey monkey) {
- this.monkey = monkey;
- }
- public Teacher getTeacher() {
- return this.teacher;
- }
- public void setTeacher(Teacher teacher) {
- this.teacher = teacher;
- }
- }
8.
- 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 void saveTeacher(Teacher teacher){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- session.save(teacher);
- 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);
- Set learnings=monkey.getLearnings();
- Iterator it=learnings.iterator(); //初始化Learnings
- while(it.hasNext()){
- Learning learning=(Learning)it.next();
- Hibernate.initialize(learning.getTeacher()); //初始化Teacher
- }
- tx.commit();
- return monkey;
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void printMonkey(Monkey monkey){
- System.out.println("名字:"+monkey.getName());
- Set learnings=monkey.getLearnings();
- Iterator it=learnings.iterator();
- while(it.hasNext()){
- Learning learning=(Learning)it.next();
- System.out.println("-----------------------");
- System.out.println("老师:"+learning.getTeacher().getName());
- System.out.println("功夫:"+learning.getGongfu());
- }
- }
- public void test(){
- Teacher teacher1=new Teacher("二郎神",null);
- Teacher teacher2=new Teacher("红孩儿",null);
- saveTeacher(teacher1);
- saveTeacher(teacher2);
- Monkey monkey=new Monkey();
- monkey.setName("智多星");
- Learning learning1=new Learning(teacher1,monkey,"七十三变");
- Learning learning2=new Learning(teacher2,monkey,"三昧真火");
- monkey.getLearnings().add(learning1);
- monkey.getLearnings().add(learning2);
- saveMonkey(monkey);
- monkey=loadMonkey(monkey.getId());
- printMonkey(monkey);
- }
- public static void main(String args[]){
- new BusinessService().test();
- sessionFactory.close();
- }
- }
9.
- 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(
- ID bigint not null,
- MONKEY_ID bigint not null,
- TEACHER_ID bigint not null,
- GONGFU varchar(15),
- primary key(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);
10.
Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)的更多相关文章
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
随机推荐
- .Net中的Socket通讯
.NetFrameWork为Socket通讯提供了System.Net.Socket命名空间,在这个命名空间里面有以下几个常用的重要类分别是: ·Socket类 这个低层的类用于管理连接,WebReq ...
- NOSQL之【redis的安全策略】
原文:http://redis.io/topics/security 1.Redis的安全模式 可信环境下的可信用户才可访问redis.这意味着,将redis服务器直接暴露在Internet或者不可信 ...
- 如何修改 Discuz 门户文章页默认视频大小
在 Discuz 系统中,论坛插入 Flash 等可以输入自定义的尺寸,但是门户文章页不可以修改.经过一番研究,找到了修改门户文章页默认视频大小的方法如下,希望对你有用:找到:/source/func ...
- 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog
SweetAlertDialog(sweet-alert-dialog)是一个套制作精美.动画效果出色生动的Android对话.消息提示框 SweetAlertDialog(sweet-alert-d ...
- Configure Log Shipping
准备工作 两台装有的Windows Server 2012R2以及SQL Server 2012的服务器 下载评估版 Windows Server 2012 R2 下载 Microsoft SQL S ...
- Java String.split()注意点
//String[] aa = "aaa|bbb|ccc".split("|");//错误 String[] aa = "aaa|bbb|ccc&qu ...
- oracle-11g创建用户名的时候默认区分大小写
oracle11g-11.2.0.3.0 - 64bit oracle-11g创建用户名的时候默认区分大小写 设置不区分大小写: alter system set sec_case_sensitive ...
- myeclipse配置下tomcat debug启动很无比慢
myeclipse配置下tomcat debug启动很无比慢,而run启动很快今天照常使用MyEclipse 6.5 Blue Edition进行开发,但是却遇到一个怪问题.在MyEclipse环境下 ...
- error:LNK2005 已经在*.obj中定义
为什么会出现这个错误??“error LNK2005: 已经在*.obj中定义” 编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误,弄清楚它形成的原 ...
- OC的@property 和 @synthesize id
学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...