Work.java
Package com.javamodel.hibernate;
Import java.util.hashset;
Import java.util.set;
Public class work {
Private string id = NULL;
Private string title = NULL;
Private set authors = new hashset ();
Public work () {}
/ **
* @Return
* /
Public set getauthors () {
Return authors;
}
/ **
* @Return
* /
Public string getId () {
Return ID;
}
/ **
* @Return
* /
Public string gettitle () {
Return Title;
}
/ **
* @Param Set
* /
Public void setauthors (set set) {
Authors = set;
}
/ **
* @Param String
* /
Public void setid (String String) {
ID = String;
}
/ **
* @Param String
* /
Public void setTitle (String String) {
Title = String;
}
}
Work.hbm.xml
XML Version = "1.0"?>
id>
set>
clas>
hibernate-maping>
Author.java plus
Private set works = new hashset (); // Get, SET
AuThor.hbm.xml plus
set>
code>
EXAMPLE.JAVA
Package com.javamodel.hibernate;
Import java.io.ioException;
Import Java.io.InputStream;
Import java.util.properties;
Import org.apache.log4j.logger;
Import net.sf.hibernate.hibernateException;
Import net.sf.hibelnate.mappingexception;
Import net.sf.hibernate.Session;
Import Net.sf.hibActory;
Import net.sf.hibernate.transaction;
Import Net.sf.hibiBernate.cfg.configuration;
Public class example {
Private static logger logger = logger.getlogger (example.class);
Private static sessionFactory _SESSIONS = NULL;
Private static property pops = new profment ();
STATIC {
Try {
InputStream Stream = Example.class.getResourceceasstream ("Hibernate.Properties");
Try {
POPS.LOAD (stream);
} catch (ioException e1) {
E1.PrintStackTrace ();
}
Configuration CFG = New Configuration ();
Cfg.addclass (person.class);
cfg.addclass (author.class);
Cfg.addclass (publication.class);
cfg.addclass (work.class);
Cfg.SetProperties (POPS);
_SESSIONS = cfg.buildsessionFactory ();
} catCH (mappingexception e) {
E.PrintStackTrace ();
} catch (hibernateException e) {
E.PrintStackTrace ();
}
}
Public static void main (string [] args) throws hibernateException {
Person Person1 = New Person ();
Person1.setname ("Hengfeido1");
Person1.setemail ("smallduzi@sohu.com");
Person Person2 = New Person ();
Person2.setname ("hengfeido2");
Person2.setemail ("smallduzi@sohu.com");
Person Person3 = New Person ();
Person3.setname ("Hengfeido3");
Person3.setemail ("smallduzi@sohu.com"); Person Person4 = New Person ();
Person4.setname ("Hengfeido4");
Person4.setemail ("smallduzi@sohu.com");
Publication Publication1 = New Publication ();
Publication1.setbookName ("AAA");
Publication1.SetDataTime ("20031224");
Publication Publication2 = New Publication ();
Publication2.setbookName ("BBB");
Publication2.SetDataTime ("20031225");
Publication Publication3 = New Publication ();
Publication3.setbookName ("CCC");
Publication3.SetDataTime ("20031226");
Publication Publication4 = New Publication ();
Publication4.SetbookName ("DDD");
Publication4.SetDataTime ("20031227");
Publication Publication5 = New Publication ();
Publication5.setbookName ("eee");
Publication5.SetDataTime ("20031228");
Publication Publication6 = New Publication ();
Publication6.setbookName ("FFF");
Publication6.SetDataTime ("20031229");
Publication Publication7 = new publication ();
Publication7.setbookName ("GGG");
Publication7.SetDataTime ("20031230");
Publication Publication8 = new publication ();
Publication8.setbookName ("HHH");
Publication8.SetDataTime ("20031231");
Author author1 = new author ();
Author1.setalias ("smallduzi");
Author1.setPerson (Person1);
Author1.getpublications (). add (publication1);
Author1.getpublications (). add (publication2);
Author author2 = new author ();
Author2.setAlias ("ADU");
Author2.setPerson (Person2);
Author2.getpublications (). add (publication3);
Author2.getpublications (). add (publication4);
Author author3 = new author ();
Author3.setalias ("dududu"); author3.setPerson (Person3);
Author3.getpublications (). Add (publication5);
Author3.getpublications (). add (publication6);
Author author4 = new author ();
Author4.setalias ("xiaoduzi");
Author4.setPerson (Person4);
Author4.getpublications (). add (publication7);
Author4.getpublications (). add (publication8);
Publication1.SetAuthor (Author1);
Publication2.SetAuthor (Author1);
Publication3.SetAuthor (Author2);
Publication4.SetAuthor (Author2);
Publication5.SetAuthor (Author3);
Publication6.SetAuthor (Author3);
Publication7.SetAuthor (Author4);
Publication8.SetAuthor (Author4);
Workwork1 = new work ();
Work1.settitle ("111111");
Work1.getauthors (). Add (author);
Work1.getauthors (). Add (author2);
Workwork2 = new work ();
Work2.settitle ("222222");
Work2.getauthors (). Add (author3);
Work2.getauthors (). Add (author4);
Workwork3 = new work ();
Work3.Settitle ("333333");
Work3.getauthors (). Add (author);
Work3.getauthors (). Add (author2);
Workwork4 = new work ();
Work4.settitle ("444444");
Work4.getauthors (). Add (author3);
Work4.getauthors (). Add (author4);
Author1.getWorks (). Add (work1);
Author1.getWorks (). Add (work3);
Author2.getWorks (). add (work2);
Author2.getWorks (). Add (work4);
// Write a point here. I hope everyone can understand
Session session = _SESSIONS.OPENSESSION ();
Transaction TX = NULL;
Try {
TX = session.begintransaction ();
Session.save (Author1);
Session.save (Author2);
Session.save (Author3);
Session.save (Author4);
Session.save (Work1);
Session.save (Work2);
Session.save (Work3);
Session.save (Work4);
TX.comMit ();
System.out.println ("--- over ---");
} catch (hibernateException he) {
IF (tx! = null) tx.rollback ();
Throw He;
}
Finally {session.close ();
}
}
}