首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate多对多例证

2013-04-21 
hibernate多对多例子学习笔记,转自:http://www.blogjava.net/wujun/archive/2006/04/08/39956.html?1.建表

hibernate多对多例子

学习笔记,转自:http://www.blogjava.net/wujun/archive/2006/04/08/39956.html

?

1.建表

hibernate多对多例证?create???table??student
hibernate多对多例证(sid??varchar?(?32?)??not???null???primary???key?,
hibernate多对多例证?sname??varchar?(?16?),
hibernate多对多例证?sage??varchar?(?16?),
hibernate多对多例证)
hibernate多对多例证
hibernate多对多例证?create???table??course
hibernate多对多例证(cid??varchar?(?32?)??not???null???primary???key?,
hibernate多对多例证cname??varchar?(?16?)
hibernate多对多例证)
hibernate多对多例证
hibernate多对多例证?create???table??student_course_link
hibernate多对多例证(sid??varchar?(?32?)??not???null?,
hibernate多对多例证cid??varchar?(?32?)??not???null?,
hibernate多对多例证?primary???key?(sid,cid)
hibernate多对多例证)
hibernate多对多例证

2.写VO
StudentVO

hibernate多对多例证package?com.test;
hibernate多对多例证import?java.util.Set;
hibernate多对多例证public?class?Student
hibernate多对多例证{
hibernate多对多例证????private?String?sid;
hibernate多对多例证????private?String?sname;
hibernate多对多例证????private?String?sage;
hibernate多对多例证
hibernate多对多例证????private?Set?course;
hibernate多对多例证????public?Student()
hibernate多对多例证????{
hibernate多对多例证????}
hibernate多对多例证???//写上get?set

Course vo

hibernate多对多例证package?com.test;
hibernate多对多例证
hibernate多对多例证import?java.util.Set;
hibernate多对多例证
hibernate多对多例证public?class?Course
hibernate多对多例证{
hibernate多对多例证????private?String?cid;
hibernate多对多例证????private?String?cname;
hibernate多对多例证????private?Set?student;
hibernate多对多例证???//写上get?set


写配置文件
Student.hbm.xml

hibernate多对多例证<?xml?version="1.0"?>
hibernate多对多例证<!DOCTYPE?hibernate-mapping
hibernate多对多例证????PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD//EN"
hibernate多对多例证????"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
hibernate多对多例证
hibernate多对多例证<hibernate-mapping>
hibernate多对多例证
hibernate多对多例证????<class?name="com.test.Student"?table="student"?>
hibernate多对多例证
hibernate多对多例证????????<id?name="sid"?type="string"?unsaved-value="null"?>
hibernate多对多例证????????????<column?name="sid"?sql-type="char(32)"?not-null="true"/>
hibernate多对多例证????????????<generator?class="uuid.hex"/>
hibernate多对多例证????????</id>
hibernate多对多例证
hibernate多对多例证????????<property?name="sname">
hibernate多对多例证????????????<column?name="sname"?sql-type="varchar(16)"?not-null="true"/>
hibernate多对多例证????????</property>
hibernate多对多例证
hibernate多对多例证????????<property?name="sage">
hibernate多对多例证????????????<column?name="sage"?sql-type="varchar(16)"?not-null="true"/>
hibernate多对多例证????????</property>
hibernate多对多例证
hibernate多对多例证????????<set?name="course"?table="student_course_link"?cascade="all"?outer-join="false">
hibernate多对多例证????????????<key?column="sid"/>
hibernate多对多例证????????????<many-to-many?class="com.test.Course"?column="cid"/>
hibernate多对多例证????????</set>
hibernate多对多例证???
hibernate多对多例证????</class>
hibernate多对多例证
hibernate多对多例证</hibernate-mapping>


Course.hbm.xml

hibernate多对多例证<?xml?version="1.0"?>
hibernate多对多例证<!DOCTYPE?hibernate-mapping
hibernate多对多例证????PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD//EN"
hibernate多对多例证????"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
hibernate多对多例证
hibernate多对多例证<hibernate-mapping>
hibernate多对多例证
hibernate多对多例证????<class?name="com.test.Course"?table="course"?>
hibernate多对多例证
hibernate多对多例证????????<id?name="cid"?type="string"?unsaved-value="null"?>
hibernate多对多例证????????????<column?name="cid"?sql-type="char(32)"?not-null="true"/>
hibernate多对多例证????????????<generator?class="uuid.hex"/>
hibernate多对多例证????????</id>
hibernate多对多例证
hibernate多对多例证????????<property?name="cname">
hibernate多对多例证????????????<column?name="cname"?sql-type="varchar(16)"?not-null="true"/>
hibernate多对多例证????????</property>
hibernate多对多例证
hibernate多对多例证????????<set?name="student"?table="student_course_link"?lazy="false"?cascade="all">
hibernate多对多例证????????????<key?column="cid"/>
hibernate多对多例证????????????<many-to-many?class="com.test.Student"?column="sid"/>
hibernate多对多例证????????</set>
hibernate多对多例证???
hibernate多对多例证????</class>
hibernate多对多例证
hibernate多对多例证</hibernate-mapping>


接着把下面的hibernate.properties文件拷到classes目录下。。这里用的是mysql

hibernate多对多例证hibernate.query.substitutions?true?1,?false?0,?yes?'Y',?no?'N'
hibernate多对多例证##?MySQL
hibernate多对多例证hibernate.dialect?net.sf.hibernate.dialect.MySQLDialect
hibernate多对多例证hibernate.connection.driver_class?org.gjt.mm.mysql.Driver
hibernate多对多例证hibernate.connection.url?jdbc:mysql://localhost:3306/wjcms
hibernate多对多例证hibernate.connection.username?root
hibernate多对多例证hibernate.connection.password?wujun
hibernate多对多例证hibernate.connection.pool_size?1
hibernate多对多例证hibernate.proxool.pool_alias?pool1
hibernate多对多例证hibernate.show_sql?true
hibernate多对多例证hibernate.jdbc.batch_size?0
hibernate多对多例证hibernate.max_fetch_depth?1
hibernate多对多例证hibernate.cache.use_query_cache?true?

写测试类了..

hibernate多对多例证package?com.test;
hibernate多对多例证
hibernate多对多例证import?net.sf.hibernate.Session;
hibernate多对多例证import?net.sf.hibernate.SessionFactory;
hibernate多对多例证import?net.sf.hibernate.cfg.Configuration;
hibernate多对多例证import?net.sf.hibernate.*;
hibernate多对多例证import?java.util.Set;
hibernate多对多例证import?java.util.HashSet;
hibernate多对多例证import?java.sql.*;
hibernate多对多例证import?java.util.List;
hibernate多对多例证import?java.util.Iterator;
hibernate多对多例证
hibernate多对多例证public?class?TestManyToMany
hibernate多对多例证{
hibernate多对多例证????SessionFactory?sf;
hibernate多对多例证????Session?session;
hibernate多对多例证????public?TestManyToMany()
hibernate多对多例证????{
hibernate多对多例证????????try
hibernate多对多例证????????{
hibernate多对多例证????????????Configuration?cfg?=?new?Configuration();
hibernate多对多例证????????????sf?=?cfg.addClass(Student.class).addClass(Course.class).buildSessionFactory();
hibernate多对多例证????????}
hibernate多对多例证????????catch(HibernateException?ex)
hibernate多对多例证????????{
hibernate多对多例证????????????ex.printStackTrace();
hibernate多对多例证????????}
hibernate多对多例证????}
hibernate多对多例证????public?void?doCreate()
hibernate多对多例证????{
hibernate多对多例证????????try
hibernate多对多例证????????{
hibernate多对多例证????????????session?=?sf.openSession();
hibernate多对多例证
hibernate多对多例证????????????Student?student?=?new?Student();
hibernate多对多例证????????????student.setSname("小王");
hibernate多对多例证????????????student.setSage("22");
hibernate多对多例证
hibernate多对多例证????????????Set?courseSet?=?new?HashSet();
hibernate多对多例证????????????Course?course?=?null;
hibernate多对多例证????????????for(int?i=0;i<2;i++)
hibernate多对多例证????????????{
hibernate多对多例证????????????????course?=?new?Course();
hibernate多对多例证????????????????if(i==0)
hibernate多对多例证????????????????????course.setCname("c++");
hibernate多对多例证????????????????else?if(i==1)
hibernate多对多例证????????????????????course.setCname("java");
hibernate多对多例证????????????????courseSet.add(course);
hibernate多对多例证????????????}
hibernate多对多例证????????????student.setCourse(courseSet);
hibernate多对多例证????????????
hibernate多对多例证????????????session.save(student);
hibernate多对多例证????????????session.flush();
hibernate多对多例证????????????session.connection().commit();
hibernate多对多例证
hibernate多对多例证????????}
hibernate多对多例证????????catch(HibernateException?ex)
hibernate多对多例证????????{
hibernate多对多例证????????????ex.printStackTrace();
hibernate多对多例证????????}
hibernate多对多例证????????catch(SQLException?ex1)
hibernate多对多例证????????{
hibernate多对多例证????????????ex1.printStackTrace();
hibernate多对多例证????????}
hibernate多对多例证????????finally
hibernate多对多例证????????{
hibernate多对多例证????????????????try{
hibernate多对多例证????????????????????session.close();
hibernate多对多例证????????????????}
hibernate多对多例证????????????????catch(HibernateException?ex2){
hibernate多对多例证????????????????}
hibernate多对多例证????????}
hibernate多对多例证
hibernate多对多例证????}
hibernate多对多例证????public?void?doQuery()
hibernate多对多例证????{
hibernate多对多例证????????try{
hibernate多对多例证????????????session?=?sf.openSession();
hibernate多对多例证????????????Query?q?=?session.createQuery("select?s?from?Student?as?s");
hibernate多对多例证????????????List?l?=?q.list();
hibernate多对多例证????????????Student?s?=?null;
hibernate多对多例证????????????Course?course?=?null;
hibernate多对多例证????????????for(int?i=0;i<l.size();i++)
hibernate多对多例证????????????{
hibernate多对多例证????????????????s?=?(Student)l.get(i);
hibernate多对多例证????????????????System.out.println("姓名:?"+s.getSname());
hibernate多对多例证????????????????System.out.println("年龄:?"+s.getSage());
hibernate多对多例证????????????????System.out.println("所选的课程:");
hibernate多对多例证????????????????Iterator?it?=?s.getCourse().iterator();
hibernate多对多例证????????????????while(it.hasNext())
hibernate多对多例证????????????????{
hibernate多对多例证????????????????????course?=?(Course)it.next();
hibernate多对多例证????????????????????System.out.println("课程名:?"+course.getCname());
hibernate多对多例证????????????????}
hibernate多对多例证
hibernate多对多例证
hibernate多对多例证????????????}
hibernate多对多例证
hibernate多对多例证????????}
hibernate多对多例证????????catch(HibernateException?ex){
hibernate多对多例证????????????ex.printStackTrace();
hibernate多对多例证????????}
hibernate多对多例证????????finally{
hibernate多对多例证????????????try{
hibernate多对多例证????????????????session.close();
hibernate多对多例证????????????}
hibernate多对多例证????????????catch(HibernateException?ex2){
hibernate多对多例证????????????}
hibernate多对多例证????????}
hibernate多对多例证????}
hibernate多对多例证????public?static?void?main(String[]?args)
hibernate多对多例证????{
hibernate多对多例证????????TestManyToMany?t?=?new?TestManyToMany();
hibernate多对多例证????????//t.doCreate();
hibernate多对多例证????????t.doQuery();
hibernate多对多例证????}
hibernate多对多例证}
hibernate多对多例证

?

好。。可以了。。

热点排行