Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0.


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.
Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多的更多相关文章
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- 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章 映射实体关联关系-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逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第5章映射一对多-01单向<many-to-one>、cascade="save-update"、lazy、TransientObjectException
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 ...
随机推荐
- 常用的php数组排序函数
分享几个php数组排序函数,每个函数出去sort是排序的意思前缀字母的含义分别代表: a 索引 k 数组键 r 逆向 u 用户自定义 顺序排序函数 sort — 对数组排序 ksort — 对数组按 ...
- kdbchk: the amount of space used is not equal to block size
一.对数据文件检查 注意:应该在关闭数据库模式下进行bbed的操作 [oracle@ora10 controlfile]$ dbv file=/u01/app/oracle/oradata/ORCL/ ...
- Python中docstring文档的写法
该写法根据Python的PEP 257文档总结. 类的函数称为方法(method),模块里的函数称为函数(function) 每一个包,模块,类,函数,方法都应该包含文档,包括类的__init__方法 ...
- js执行上下文(由浅入深)
每一个函数都有自己的执行上下文EC(执行环境 execution context),并且每个执行上下文中都有它自己的变量对象VO(Variable object),用于存储执行上下文中的变量 .函数声 ...
- Oracle计算两个整数的和与这两个整数的差与商
PL/SQL(Procedural Language/SQL)是一种过程化语言. PL/SQL都是以(BLOCK)块为基本单位,整个PL/SQL块分为三部分 1.声明(Declare) 2.执行(以B ...
- backbone collection add 事件回调参数
this.listenTo(this.collection, 'add', this.renderBook); renderBook: function (item) { var bookView = ...
- IOS UIWebView截获html并修改便签内容,宽度自适应
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服 ...
- 面试问到:JDBC、hibernate、ibati
一.JDBC.Connection(连接) 优点:运行高效.快捷. 缺点:代码多.异常多.不支持跨平台. 二.ibatis 1.根据jdbc的基本建立连接. 2.通过anntation+xml.jav ...
- mysql存储过程中传decimal值会自动四舍五入,没有小数
通过 call proc(0.2,0.5); 查看结果数据库竟然是0 和 1 原因:proc的参数没有设置好 参数:原本是 in a decimal,in b decimal 应该改为:in ...
- C#WinForm中显示实时时间:年/月/日 时/分/秒 星期X
//加载窗体时 string weekstr = ""; private void Form22_Load(object sender, EventArgs e) { this.t ...