hibernate多对多例子
来源:互联网 发布:php软件 编辑:程序博客网 时间:2024/05/05 13:30
1.建表
create table student(sid varchar ( 32 ) not null primary key ,sname varchar ( 16 ),sage varchar ( 16 ),)create table course(cid varchar ( 32 ) not null primary key ,cname varchar ( 16 ))create table student_course_link(sid varchar ( 32 ) not null ,cid varchar ( 32 ) not null ,primary key (sid,cid))
2.写VO
StudentVO2.写VO
StudentVO
package com.test;import java.util.Set;public class Student{ private String sid; private String sname; private String sage; private Set course; public Student() { } //写上get setCourse vopackage com.test;import java.util.Set;public class Course{ private String cid; private String cname; private Set student; //写上get set
写配置文件
Student.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="com.test.Student" table="student" > <id name="sid" type="string" unsaved-value="null" > <column name="sid" sql-type="char(32)" not-null="true"/> <generator class="uuid.hex"/> </id> <property name="sname"> <column name="sname" sql-type="varchar(16)" not-null="true"/> </property> <property name="sage"> <column name="sage" sql-type="varchar(16)" not-null="true"/> </property> <set name="course" table="student_course_link" cascade="all" outer-join="false"> <key column="sid"/> <many-to-many class="com.test.Course" column="cid"/> </set> </class></hibernate-mapping>
Course.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="com.test.Course" table="course" > <id name="cid" type="string" unsaved-value="null" > <column name="cid" sql-type="char(32)" not-null="true"/> <generator class="uuid.hex"/> </id> <property name="cname"> <column name="cname" sql-type="varchar(16)" not-null="true"/> </property> <set name="student" table="student_course_link" lazy="false" cascade="all"> <key column="cid"/> <many-to-many class="com.test.Student" column="sid"/> </set> </class></hibernate-mapping>
接着把下面的hibernate.properties文件拷到classes目录下。。这里用的是mysql
hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'
## MySQL
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/wjcms
hibernate.connection.username root
hibernate.connection.password wujun
hibernate.connection.pool_size 1
hibernate.proxool.pool_alias pool1
hibernate.show_sql true
hibernate.jdbc.batch_size 0
hibernate.max_fetch_depth 1
hibernate.cache.use_query_cache true
写测试类了..
package com.test;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.cfg.Configuration;import net.sf.hibernate.*;import java.util.Set;import java.util.HashSet;import java.sql.*;import java.util.List;import java.util.Iterator;public class TestManyToMany{ SessionFactory sf; Session session; public TestManyToMany() { try { Configuration cfg = new Configuration(); sf = cfg.addClass(Student.class).addClass(Course.class).buildSessionFactory(); } catch(HibernateException ex) { ex.printStackTrace(); } } public void doCreate() { try { session = sf.openSession(); Student student = new Student(); student.setSname("小王"); student.setSage("22"); Set courseSet = new HashSet(); Course course = null; for(int i=0;i<2;i++) { course = new Course(); if(i==0) course.setCname("c++"); else if(i==1) course.setCname("java"); courseSet.add(course); } student.setCourse(courseSet); session.save(student); session.flush(); session.connection().commit(); } catch(HibernateException ex) { ex.printStackTrace(); } catch(SQLException ex1) { ex1.printStackTrace(); } finally { try{ session.close(); } catch(HibernateException ex2){ } } } public void doQuery() { try{ session = sf.openSession(); Query q = session.createQuery("select s from Student as s"); List l = q.list(); Student s = null; Course course = null; for(int i=0;i<l.size();i++) { s = (Student)l.get(i); System.out.println("姓名: "+s.getSname()); System.out.println("年龄: "+s.getSage()); System.out.println("所选的课程:"); Iterator it = s.getCourse().iterator(); while(it.hasNext()) { course = (Course)it.next(); System.out.println("课程名: "+course.getCname()); } } } catch(HibernateException ex){ ex.printStackTrace(); } finally{ try{ session.close(); } catch(HibernateException ex2){ } } } public static void main(String[] args) { TestManyToMany t = new TestManyToMany(); //t.doCreate(); t.doQuery(); }}
- hibernate多对多例子
- hibernate多对多例子
- Hibernate 多对多 例子
- Hibernate 实现 多对一 例子
- hibernate入门教程3-----多对多的例子
- Hibernate中 一个单向多对多例子
- hibernate多对多例子-方便以后查看
- hibernate EJB注解双向(一对多、多对一)例子
- hibernate的多对多例子讲解(加图片)
- hibernate一对多例子
- hibernate多对多
- hibernate多对多
- Hibernate多对多
- hibernate 多对多
- Hibernate多对多
- hibernate 多对多
- Hibernate多对多
- hibernate多对多
- Android自定义action与permission!!!
- ASCII码表(美国标准信息交换标准码)
- 分析对象所占储存空间大小 我还看不懂···
- STL概述
- Android文件下载 HttpURLConnection
- hibernate多对多例子
- linux 批量删除及修改文件shell命令
- 敏捷开发智慧敏捷系列之二:写不写文档?
- 视频编码中的PAFF和MBAFF的区别 转自:http://blog.csdn.net/kerryhung/article/details/4433256
- 使用spring MVC框架进行文件上传
- android学习笔记一(基础部分)
- 快速学习AJAX之六 Ajax实现无刷新上传头像
- 如何判断 linux内核 中 如何判断 条件编译
- 矩阵连乘 动态规划 详解