One-to-man in Hibernate

xiaoxiao2021-03-06  41

With an example to illustrate the configuration of One-to-Many in Hibernate, including a Parent (one) and a child (MANY). The code is as follows: Since Struts Hibernate is used, Parent inherits the Actionform class.

Parent.hbm.xml

Set the sub-key, so that Hibernate can be used to control the primary key of the query result and the sub-table out of the sub-table. This field must be present in the sub-table and the type is consistent with the primary key.

Parent.java

Package com.ln.hb;

Import java.util.hashset;

Import org.apache.struts.Action.actionform;

Public class parent extends an actionform {

PRIVATE STRING PNAME;

Private java.util.set child = new hashset ();

Private int PID;

Public Parent () {

}

Public int getPid () {

Return PID;

}

Public void setpid (int pid) {

THIS.PID = PID;

}

Public string getpname () {

Return PNAME;

}

Public void setpname (string pname) {

This.Pname = PNAME;

}

Public java.util.set getChild () {

Return Child;

}

Public void setchild (java.util.set child) {

THIS.CHILD = CHILD;

}

}

Child.hbm.xml

Set the parent table, so that Hibernate can be controlled with the foreign key value of the parent table based on the sub-table outside of the query results, and the desired parent results are found. This field must exist in the parent table and the type is consistent with the primary key.

Child.java

Package com.ln.hb;

Public class child {

PRIVATE STRING CNAME;

PRIVATE STRING PID;

PRIVATE PARENT

PRIVATE STRING CID;

Public child () {

}

Public string getCID () {

Return CID;

}

Public void setcid (string CID) {

THIS.CID = CID;

}

Public string getcname () {

Return CNAME;

}

Public void setcname (String cname) {

THIS.CNAME = CNAME;

}

Public string getpid () {

Return PID;

}

Public void setpid (string pid) {

THIS.PID = PID;

}

Public Parent getParent () {

Return Parent;

}

Public void setparent (parent parent) {

THIS.PARENT = Parent;

}

}

TestAction.java

Package com.ln.struts.Action;

Import java.util.iterator;

Import java.util.set;

Import javax.servlet.http.httpservletRequest;

Import javax.servlet.http.httpservletResponse;

Import net.sf.hibernate.query;

Import net.sf.hibernate.Session;

Import Net.sf.hibActory;

Import net.sf.hibernate.transaction;

Import Net.sf.hibiBernate.cfg.configuration;

Import org.apache.struts.Action.action;

Import org.apache.struts.Action.actionform;

Import org.apache.struts.Action.actionForward;

Import org.apache.struts.Action.Actionmapping; import com.ln.hb.child;

Import com.ln.hb.parent;

Public class test degree extends action {

Public ActionForward Execute (ActionMapping Mapping, Actionform Form,

HttpservletRequest Request, httpservletResponse response) throws exception {

Parent P = (PARENT) FORM;

Child C = new child ();

SessionFactory sf = new configuration (). Configure ()

.BUILDSessionFactory ();

Session session = sf.opensession ();

Transaction tx = session.begintransaction ();

System.out.println ("Let's Go!");

Query Query = session

.createQuery ("Select P from Parent AS P WHERE PID =: PID");

Query.setString ("PID", "1");

Iteerator it = query.Itemate (); it.hasnext ();) {

PARENT P1 = (PARENT) IT.NEXT ();

P.SETPNAME (p1.getpname (). TOSTRING ());

System.out.println (p.getpname ());

SET SET = p1.getchild ();

ITERATOR IC = SET.ITERATOR (); Ic.hasNext ();)

{

C = (child) ic.next ();

System.out.println (c.getcname ());

}

}

TX.comMit ();

session.close ();

Return mapping.findforward ("main");

}

}

note:

1. The type of variable declared in the class is consistent with the type of type in the database, and the declared fields also have corresponding fields in the database.

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

New Post(0)