Java Hibernate a one-to-many self-association problem _java

Source: Internet
Author: User

Hibernate a one-to-many self-correlation problem

This is difficult to describe clearly, can only refer to the CSDN I asked the post:
Http://topic.csdn.net/u/20080711/16/7494bf10-48ca-4b2e-8a01-303e647f5516.html
method, get a PO in the form and then Session.save (PO) as follows:


Program code
tx = Session.begintransaction ();
Session.save (catalog);
Tx.commit ();




b method, also in the form to obtain a PO, and another to obtain a po_id, as follows:


Program code
public void Addchildcatalog (int id, Catalog c11) {
Catalog C1 = This.getcatalog (ID);
Log.info ("C11.getid ()::" +c11.getid ());
C1.getchildcatalogs (). Add (C11);
C11.setparentcatalog (C1);

Session session = Hibernateutil.getsession ();
Transaction tx = Session.begintransaction ();

try {
Session.save (C1);
Session.save (C11);
Tx.commit ();
catch (Hibernateexception e) {
if (TX!= null) {
Hibernateutil.rollbacktransaction (TX);
}
finally {
if (session!= null) {
Hibernateutil.closesession (session);
}
}
}




The first feature, referred to as a function, I was to save a top-level menu, without any association.
The second function, the B function, is to do the correlation save based on that parent ID.

Now the problem is: I enter the function page, use a function, normal, and then use a function, normal, very normal ... It's okay to use n times.
Then I use B function, keep adding subclasses, also all normal ... It's normal to use n times.

Note: When I use the B function, everything becomes abnormal, and then use a function, which is equivalent to using the B function, or doing the last B function operation! The addition is a subcategory.

Another problem: I can only continue to extend the subclass down, unable to join the existing sibling category. Like what

Program code
Level 1 Id:1 c_id=null
Level 2 Id:2 c_id=1
Level 3 Id:3 c_id=2

If the association form in the database is such, then I can only according to 3 ID, add 4 level, can not be joined below 2 levels of Class 3 classes, such as Id:4 c_id=2; so I can't join, the console has only select operation, but I can join Id:4 c_id=3.

The funniest thing is, when I turn the browser off, re-opened into the page, a function is normal, join the same id:4 c_id=2; OK,
Then I went down the classification, and there are the above problems, the most I do not understand is that the browser what the relationship Ah! (My IE6, Khan)

Also attached is the hbm.xml of the link


Program code
<!--self-->
<many-to-one name= "ParentCatalog"
class= "Com.blur.myHibernate.po.article.Catalog" >
<column name= "catalog_id" ></column>
</many-to-one>

<set name= "Childcatalogs" cascade= "Save-update"
Inverse= "true" lazy= "false" >
<key>
<column name= "catalog_id" ></column>
</key>
<one-to-many
class= "Com.blur.myHibernate.po.article.Catalog"/>
</set>


Solution: Formbean In the submission of the form, the domain database in the next time is still retained, struts Formbean default scope for the session, manually set to request, just fine ...

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.