Hibernate object ing: many-to-many

Source: Internet
Author: User

Hibernate object ing: many-to-many

I. project

 

Ii. Business Code

 

PackageCom. cloud. domain;

ImportJava. util. Set;

/**

* Course entity.@ AuthorMyEclipse Persistence Tools

*/

Public classCourse {

// Fields

PrivateInteger id;

PrivateString name;

PrivateSet StuCourse;

PublicInteger getId (){

ReturnId;

}

Public voidSetId (Integer id ){

This. Id = id;

}

PublicString getName (){

ReturnName;

}

Public voidSetName (String name ){

This. Name = name;

}

PublicSet GetStuCourse (){

ReturnStuCourse;

}

Public voidSetStuCourse (Set StuCourse ){

This. StuCourse = stuCourse;

}

}

 

 

 

PackageCom. cloud. domain;

/**

*StucourseEntity.@ AuthorMyEclipse PersistenceTools

*/

 

Public classStucourse {

 

// Fields

 

PrivateInteger id;

PrivateStudent student;

PrivateCourse course;

PrivateInteger grade;

PublicInteger getId (){

ReturnId;

}

Public voidSetId (Integer id ){

This. Id = id;

}

PublicStudent getStudent (){

ReturnStudent;

}

Public voidSetStudent (Studentstudent ){

This. Student = student;

}

PublicCourse getCourse (){

ReturnCourse;

}

Public voidSetCourse (Coursecourse ){

This. Course = course;

}

PublicInteger getGrade (){

ReturnGrade;

}

Public voidSetGrade (Integer grade ){

This. Grade = grade;

}

}

 

 

 

PackageCom. cloud. domain;

ImportJava. util. Set;

/**

* Student entity.@ AuthorMyEclipse Persistence Tools

*/

Public classStudent {

// Fields

PrivateInteger id;

PrivateString name;

PrivateSet StuCourse;

PublicInteger getId (){

ReturnId;

}

Public voidSetId (Integer id ){

This. Id = id;

}

PublicString getName (){

ReturnName;

}

Public voidSetName (String name ){

This. Name = name;

}

PublicSet GetStuCourse (){

ReturnStuCourse;

}

Public voidSetStuCourse (Set StuCourse ){

This. StuCourse = stuCourse;

}

}

Iii. project configuration file

 

Course. hbm. xml

1.0 encoding =UTF-8?>

 

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

Com. cloud. domain>

Course>

Idtype = Java. lang. Integer>

Id/>

Increment/>

 

Nametype = Java. lang. String>

Namelength = 64/>

 

StuCourse>

Course_id/>

Stucourse/>

 

 

 

 

Stucouese. hbm. xml

1.0 encoding =UTF-8?>

 

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

Com. cloud. domain>

Stucourse>

Idtype = Java. lang. Integer>

Id/>

Increment/>

 

Gradetype = Java. lang. Integer>

Gradelength = 3/>

 

Coursecolumn = Course_id/>

Studentcolumn = Student_id/>

 

 

 

Student. hbm. xml

1.0 encoding =UTF-8?>

 

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

Com. cloud. domain>

Student>

Idtype = Java. lang. Integer>

Id/>

Increment/>

 

Nametype = Java. lang. String>

Namelength = 64/>

 

StuCourse>

Student_id/>

Stucourse/>

 

 

 

 

Hibernate. cfg. xml

 

'1. 0' encoding ='Utf-8'?>

 

-// Hibernate/Hibernate Configuration DTD 3.0 // EN

Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>

 

 

Dialect>

Org. hibernate. dialect. MySQLDialect

 

Connection. url>

Jdbc: mysql ://Localhost: 3306/zjgm03

 

Connection. username> root

Connection. password> root

Connection. driver_class>

Com. mysql. jdbc. Driver

 

 

Show_ SQL> true

Hbm2ddl. auto> update

Com/cloud/domain/Student. hbm. xml/>

Com/cloud/domain/Stucourse. hbm. xml/>

Com/cloud/domain/Course. hbm. xml/>

 

 

 

 

 

Iv. Test code

 

Package com. cloud. view;

 

Import org. hibernate. Session;

Import org. hibernate. Transaction;

 

Import com. cloud. domain. Course;

Import com. cloud. domain. Stucourse;

Import com. cloud. domain. Student;

Import com. cloud. util. HibernateUtil;

 

Public class TestMain {

 

/**

* @ Param args

*/

Publicstatic void main (String [] args ){

// TODO Auto-generated method stub

Sessionsession = null;

Transactionts = null;

Try {

Session = HibernateUtil. getCurrentSession ();

Ts = session. beginTransaction ();

// Add a student, a course, and select a course

Studentstu1 = new Student ();

Stu1.setName (James );

Coursecu1 = new Course ();

Cu1.setName (java course );

Stucoursesc = new Stucourse ();

SC. setStudent (stu1 );

SC. setCourse (cu1 );

// Save the order here

Session. save (stu1 );

Session. save (cu1 );

Session. save (SC );

Ts. commit ();

} Catch (Exception e ){

If (ts! = Null)

{

Ts. rollback ();

}

Thrownew RuntimeException ();

} Finally {

If (session! = Null & session. isOpen ()){

Session. close ();

}

}

}

 

}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.