Java Hibernate: one-to-many Association issues, javahibernate Association

Source: Internet
Author: User

Java Hibernate: one-to-many Association issues, javahibernate Association

Hibernate one-to-multiple self-Association Problems

This is hard to describe clearly. I can only reference the post I asked in CSDN:
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 ();




Method B is also used to obtain a PO in the form and a PO_ID, as shown below:


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 the feature, is used to save A top-level menu and has no association.
The second feature, function B, is used to perform associated storage based on the parent ID.

The problem is: I enter the function page and use the function. It is normal, and then use the function. It is normal and normal... It's okay to use it N times.
Then I use the B function and add sub-classes without stopping. It is also normal... It is normal to use it N times.

Note: After I use the B function, everything becomes abnormal. Using the function is equivalent to the last B function operation, just like using the B function! A subcategory is added.

In addition, the problem is that I can only keep extending the sub-classes down and cannot add existing same-level classes. For example

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 like this, I can only add Level 4 Based on the id of Level 3, not Level 3 under Level 2, such as id: 4 c_id = 2; in this case, I cannot join. The console only has select operations, but I can add id: 4 c_id = 3.

The most funny thing is that when I turn off the browser and re-open the page, the function is normal and the same level id is added: 4 c_id = 2; OK,
Then I extended down the category and encountered the above problem. What I don't understand most is what it has to do with browsers! (My IE6, Khan)

In addition, the association between hbm. xml is attached.


Program code
<! -- Self -->
<Role-to-one name = "parentCatalog"
Class = "com. blur. myHibernate. po. article. Catalog">
<Column name = "catalog_id"> </column>
</Role-to-one>

<Set name = "childCatalogs" cascade = "save-update"
Inverse = "true" lazy = "false">
<Key>
<Column name = "catalog_id"> </column>
</Key>
<One-to-least
Class = "com. blur. myHibernate. po. article. Catalog"/>
</Set>


Solution: When formBean submits a form, the database in the domain is retained for the next time. By default, struts formBean sets scope as session and request manually...

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.