1.建表
create
table
student
(sid
varchar
(
32
)
not
null
primary
key
,
sname
varchar
(
16
),
sage
varchar
(
16
),
)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
create
table
course
(cid
varchar
(
32
)
not
null
primary
key
,
cname
varchar
(
16
)
)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
create
table
student_course_link
(sid
varchar
(
32
)
not
null
,
cid
varchar
(
32
)
not
null
,
primary
key
(sid,cid)
)
2.写VO
StudentVO
package
com.test;
import
java.util.Set;
public
class
Student
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private String sid;
private String sname;
private String sage;
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
private Set course;
public Student()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
//写上get set
Course vo
package
com.test;
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
import
java.util.Set;
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
public
class
Course
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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"
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
hibernate-mapping
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
class
name
="com.test.Student"
table
="student"
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
id
name
="sid"
type
="string"
unsaved-value
="null"
>
<
column
name
="sid"
sql-type
="char(32)"
not-null
="true"
/>
<
generator
class
="uuid.hex"
/>
</
id
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
property
name
="sname"
>
<
column
name
="sname"
sql-type
="varchar(16)"
not-null
="true"
/>
</
property
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
property
name
="sage"
>
<
column
name
="sage"
sql-type
="varchar(16)"
not-null
="true"
/>
</
property
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
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
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
</
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"
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
hibernate-mapping
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
class
name
="com.test.Course"
table
="course"
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
id
name
="cid"
type
="string"
unsaved-value
="null"
>
<
column
name
="cid"
sql-type
="char(32)"
not-null
="true"
/>
<
generator
class
="uuid.hex"
/>
</
id
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
property
name
="cname"
>
<
column
name
="cname"
sql-type
="varchar(16)"
not-null
="true"
/>
</
property
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
<
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
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
</
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;
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
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;
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
public
class
TestManyToMany
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
SessionFactory sf;
Session session;
public TestManyToMany()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Configuration cfg = new Configuration();
sf = cfg.addClass(Student.class).addClass(Course.class).buildSessionFactory();
}
catch(HibernateException ex)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ex.printStackTrace();
}
}
public void doCreate()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
session = sf.openSession();
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
Student student = new Student();
student.setSname("小王");
student.setSage("22");
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
Set courseSet = new HashSet();
Course course = null;
for(int i=0;i<2;i++)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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();
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
}
catch(HibernateException ex)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ex.printStackTrace();
}
catch(SQLException ex1)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ex1.printStackTrace();
}
finally
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
session.close();
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(HibernateException ex2)
{
}
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
}
public void doQuery()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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++)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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())
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
course = (Course)it.next();
System.out.println("课程名: "+course.getCname());
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(HibernateException ex)
{
ex.printStackTrace();
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
finally
{
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
session.close();
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(HibernateException ex2)
{
}
}
}
public static void main(String[] args)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TestManyToMany t = new TestManyToMany();
//t.doCreate();
t.doQuery();
}
}