Create a database
Create Database student;
Use student;
Create Table studentinfo (
Stuno integer auto_increment not null primary key,
Stuname varchar (50) not null
);
Create Table courseinfo (
Courseno integer auto_increment not null primary key,
Coursename varchar (30) not null
);
Create Table student_course (
Stuno int,
Courseno int,
Foreign key (stuno) References studentinfo (stuno ),
Foreign key (courseno) References courseinfo (courseno)
);
Show databases;
Pojo
Package com. ETP. zsh. pojo;
Import java. util. hashset;
Import java. util. Set;
Public class courseinfo implements java. Io. serializable {
Private integer courseno;
Private string coursename;
Private set studentinfos = new hashset (0 );
Public courseinfo (){
}
Public courseinfo (string coursename ){
This. coursename = coursename;
}
Public courseinfo (string coursename, set studentinfos ){
This. coursename = coursename;
This. studentinfos = studentinfos;
}
// Property accessors
Public integer getcourseno (){
Return this. courseno;
}
Public void setcourseno (integer courseno ){
This. courseno = courseno;
}
Public String getcoursename (){
Return this. coursename;
}
Public void setcoursename (string coursename ){
This. coursename = coursename;
}
Public set getstudentinfos (){
Return this. studentinfos;
}
Public void setstudentinfos (set studentinfos ){
This. studentinfos = studentinfos;
}
}
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class name = "com. ETP. zsh. pojo. courseinfo" table = "courseinfo" catalog = "student">
<ID name = "courseno" type = "Java. Lang. Integer">
<Column name = "courseno"/>
<Generator class = "native"/>
</ID>
<Property name = "coursename" type = "Java. Lang. String">
<Column name = "coursename" length = "30" not-null = "true"/>
</Property>
<Set name = "studentinfos" table = "student_course" catalog = "student">
<Key>
<Column name = "courseno"/>
</Key>
<Your-to-register entity-name = "com. ETP. zsh. pojo. studentinfo">
<Column name = "stuno"/>
</Upload-to-sequence>
</Set>
</Class>
</Hibernate-mapping>
Package com. ETP. zsh. pojo;
Import java. util. hashset;
Import java. util. Set;
Public class studentinfo implements java. Io. serializable {
// Fields
Private integer stuno;
Private string stuname;
Private set courseinfos = new hashset (0 );
// Constructors
Public studentinfo (){
}
Public studentinfo (string stuname ){
This. stuname = stuname;
}
Public studentinfo (string stuname, set courseinfos ){
This. stuname = stuname;
This. courseinfos = courseinfos;
}
// Property accessors
Public integer getstuno (){
Return this. stuno;
}
Public void setstuno (integer stuno ){
This. stuno = stuno;
}
Public String getstuname (){
Return this. stuname;
}
Public void setstuname (string stuname ){
This. stuname = stuname;
}
Public set getcourseinfos (){
Return this. courseinfos;
}
Public void setcourseinfos (set courseinfos ){
This. courseinfos = courseinfos;
}
}
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class name = "com. ETP. zsh. pojo. studentinfo" table = "studentinfo"
Catalog = "student">
<ID name = "stuno" type = "Java. Lang. Integer">
<Column name = "stuno"/>
<Generator class = "native"/>
</ID>
<Property name = "stuname" type = "Java. Lang. String">
<Column name = "stuname" length = "50" not-null = "true"/>
</Property>
<Set name = "courseinfos" table = "student_course" catalog = "student">
<Key>
<Column name = "stuno"/>
</Key>
<Your-to-register entity-name = "com. ETP. zsh. pojo. courseinfo">
<Column name = "courseno"/>
</Upload-to-sequence>
</Set>
</Class>
</Hibernate-mapping>
Hibernatesessionfactory
Package com. ETP. zsh. pojo;
Import org. hibernate. hibernateexception;
Import org. hibernate. Session;
Import org. hibernate. cfg. configuration;
Public class hibernatesessionfactory
{
Private Static string config_file_location = "/hibernate. cfg. xml ";
Private Static final threadlocal <session> threadlocal = new threadlocal <session> ();
Private Static configuration = new configuration ();
Private Static org. hibernate. sessionfactory;
Private Static string configfile = config_file_location;
Static
{
Try
{
Configuration. Configure (configfile );
Sessionfactory = configuration. buildsessionfactory ();
}
Catch (exception E)
{
System. Err. println ("% error creating sessionfactory % ");
E. printstacktrace ();
}
}
Private hibernatesessionfactory ()
{
}
Public static session getsession () throws hibernateexception
{
Session session = (Session) threadlocal. Get ();
If (session = NULL |! Session. isopen ())
{
If (sessionfactory = NULL)
{
Rebuildsessionfactory ();
}
Session = (sessionfactory! = NULL )? Sessionfactory. opensession (): NULL;
Threadlocal. Set (session );
}
Return session;
}
Public static void rebuildsessionfactory ()
{
Try
{
Configuration. Configure (configfile );
Sessionfactory = configuration. buildsessionfactory ();
}
Catch (exception E)
{
System. Err. println ("% error creating sessionfactory % ");
E. printstacktrace ();
}
}
Public static void closesession () throws hibernateexception
{
Session session = (Session) threadlocal. Get ();
Threadlocal. Set (null );
If (session! = NULL)
{
Session. Close ();
}
}
Public static org. hibernate. sessionfactory getsessionfactory ()
{
Return sessionfactory;
}
Public static void setconfigfile (string configfile)
{
Hibernatesessionfactory. configfile = configfile;
Sessionfactory = NULL;
}
Public static configuration getconfiguration ()
{
Return configuration;
}
}
Hibernate. cfg. xml
<? XML version = '1. 0' encoding = 'utf-8'?>
<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
<! -- Generated by myeclipse hibernate tools. -->
<Hibernate-configuration>
<Session-factory>
<Property name = "show_ SQL"> true </property>
<Property name = "myeclipse. Connection. Profile">
Com. MySQL. JDBC. Driver
</Property>
<Property name = "connection. url">
JDBC: mysql: // localhost: 3306/student
</Property>
<Property name = "connection. username"> root </property>
<Property name = "connection. Password"> root </property>
<Property name = "connection. driver_class">
Com. MySQL. JDBC. Driver
</Property>
<Property name = "dialect">
Org. hibernate. dialect. mysqldialect
</Property>
<Mapping Resource = "com/ETP/zsh/pojo/studentinfo. HBM. xml"/>
<Mapping Resource = "com/ETP/zsh/pojo/courseinfo. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>
Test class
Package com. ETP. zsh. pojo;
Import org. hibernate. Session;
Import org. hibernate. transaction;
Public class test
{
// Insert data into the object table
Public void testmanytomany_1 ()
{
Studentinfo S1 = new studentinfo ("Zhang San ");
Studentinfo S2 = new studentinfo ("Li Si ");
Studentinfo S3 = new studentinfo ("Wang Wu ");
Courseinfo C1 = new courseinfo ("physical ");
Courseinfo C2 = new courseinfo ("Mathematics ");
Courseinfo C3 = new courseinfo ("chemistry ");
Session session = NULL;
Transaction TRAN = NULL;
Try
{
Session = hibernatesessionfactory. getsession ();
TRAN = session. begintransaction ();
Session. Save (C1 );
Session. Save (C2 );
Session. Save (C3 );
Session. Save (S1 );
Session. Save (S2 );
Session. Save (S3 );
Tran. Commit ();
}
Catch (exception E)
{
// Todo: handle exception
E. printstacktrace ();
Tran. rollback ();
}
Finally
{
Hibernatesessionfactory. closesession ();
}
}
// Maintain the relationship table from student
Public void testmanytomanay_2 ()
{
Session session = NULL;
Transaction TRAN = NULL;
Try
{
Session = hibernatesessionfactory. getsession ();
TRAN = session. begintransaction ();
Studentinfo ST = (studentinfo) Session. Get (studentinfo. Class, 1 );
Courseinfo cs = (courseinfo) Session. Get (courseinfo. Class, 2 );
St. getcourseinfos (). Add (CS );
Tran. Commit ();
}
Catch (exception E)
{
E. printstacktrace ();
Tran. rollback ();
}
Finally
{
Hibernatesessionfactory. closesession ();
}
}
// Course-based maintenance
Public void testmanytomany_3 ()
{
Session session = NULL;
Transaction TRAN = NULL;
Try
{
Session = hibernatesessionfactory. getsession ();
TRAN = session. begintransaction ();
Studentinfo ST = (studentinfo) Session. Get (studentinfo. Class, 2 );
Courseinfo cs = (courseinfo) Session. Get (courseinfo. Class, 1 );
CS. getstudentinfos (). Add (ST );
Tran. Commit ();
}
Catch (exception E)
{
// Todo: handle exception
E. printstacktrace ();
Tran. rollback ();
}
Finally
{
Hibernatesessionfactory. closesession ();
}
}
// Delete data from the relational table
// The entity table will not change
Public void deletefromrelationtable ()
{
Session session = NULL;
Transaction TRAN = NULL;
Try
{
Session = hibernatesessionfactory. getsession ();
TRAN = session. begintransaction ();
// Delete the records in the relation table with student ID 1 and course number 2 (maintained by the Student)
Studentinfo Stu = (studentinfo) Session. createquery
("From studentinfo s where S. stuno = 1"). uniqueresult ();
Courseinfo cs = (courseinfo) Session. Get (courseinfo. Class, 2 );
Stu. getcourseinfos (). Remove (CS );
// Delete all related records in the Student Association table with the student ID 1
Studentinfo S = (studentinfo) Session. createquery
("From studentinfo s where S. stuno = 1"). uniqueresult ();
S. getcourseinfos (). Clear ();
// Delete all related records in the course Association table with course 2
Courseinfo c = (courseinfo) Session. Get (courseinfo. Class, 2 );
C. getstudentinfos (). Clear ();
Tran. Commit ();
}
Catch (exception E)
{
E. printstacktrace ();
Tran. rollback ();
}
Finally
{
Hibernatesessionfactory. closesession ();
}
}
Public static void main (string [] ARGs)
{
Test T = new test ();
// T. testmanytomany_1 ();
// T. testmanytomany_2 ();
T. deletefromrelationtable ();
}
}