One-to-Many

xiaoxiao2021-03-06  35

When One To Many, both ends need to be described.

Publication.java

Package com.javamodel.hibernate;

PUBLIC CLASS publication {

Private string id = NULL;

PRIVATE STRING BOOKNAME = NULL;

PRIVATE STRING DATATIME = NULL;

PRIVATE STRING AuthorId = NULL;

PRIVATE Author = NULL;

Public publication () {}

/ **

* @Return

* /

Public string getbookname () {

Return BookName;

}

/ **

* @Return

* /

Public string getDataTime () {

Return DataTime;

}

/ **

* @Return

* /

Public string getId () {

Return ID;

}

/ **

* @Param String

* /

Public void setBookName (String string) {

Bookname = string;

}

/ **

* @Param String

* /

Public void setDataime (string string) {

Dataime = String;

}

/ **

* @Param String

* /

Public void setid (String String) {

ID = String;

}

/ **

* @Return

* /

Public string getAuthorid () {

Return authorid;

}

/ **

* @Param String

* /

Public void setauthorid (String string) {

Authorid = String;

}

/ **

* @Return

* /

Public author getAuthor () {

Return Author;

}

/ **

* @Param Author

* /

Public void setauthor (author author) {

THIS.AUTHOR = Author;

}

}

Publication.hbm.xml

Author.java plus

Private set publications = new hashset (); // add get, set

AuThor.hbm.xml plus

Here is the main end, you can use Set, List, Bag, etc., refer to the documentation, inverse = "true", default is false. this place

When describing the two corresponding mapping files, there must be one end to: True. YEHS220 also mentioned many times.

->

EXAMPLE.JAVA

Package com.javamodel.hibernate;

Import java.io.ioException;

Import Java.io.InputStream;

Import java.util.properties;

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 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.SetProperties (POPS);

_SESSIONS = cfg.buildsessionFactory ();

} catCH (mappingexception e) {

E.PrintStackTrace ();

} catch (hibernateException e) {

E.PrintStackTrace ();

}

}

Public static void main (string [] args) throws hibernateException {Person Person = New Person ();

Person.setname ("Hengfeido");

Person.setemail ("smallduzi@sohu.com");

Publication Publication1 = New Publication ();

Publication1.setbookName ("aaa");

Publication1.SetDataTime ("20031224");

Publication Publication2 = New Publication ();

Publication2.setbookName ("BBB");

Publication2.SetDataTime ("20031225");

Author author = new author ();

Author.setalias ("smallduzi");

Author.setPerson (Person);

Author.getpublications (). add (publication1);

Author.getpublications (). add (publication2);

Publication1.SetAuthor (Author);

Publication2.SetAuthor (Author);

Session session = _SESSIONS.OPENSESSION ();

Transaction TX = NULL;

Try {

TX = session.begintransaction ();

Session.save (author);

TX.comMit ();

System.out.Println ("over");

} catch (hibernateException he) {

IF (tx! = null) tx.rollback ();

Throw He;

}

Finally {

session.close ();

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-66567.html

New Post(0)