Hibernate-----Many-to-one association mappings (map File mode)

Source: Internet
Author: User

N-1 Associative mapping Principle: Add a foreign key to one end of a multi-point

many (Maintenance side: User )

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
To

One (Non-maintenance segment: Group )

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

public void Testmanytoone () {       Session session= null;       try {           session= hibernateutils.getsession ();           Session.begintransaction ();           Group Group = new Group ();           Group.setname ("group1");           If the group object is not saved first, the transientobjectexception exception is thrown when the cache is cleaned, because the object of the persistent State cannot refer to the object of the transient state           //If the Cascade attribute is used in user It is not necessary to save the group, because Hibernate saves the user-maintained association object Group           Session.save (group) when saving the user;            User user = new user ();           User.setname ("user1");           User.setgroup (group);           Session.save (user);            Session.gettransaction (). commit ();       } catch (Exception e) {           e.printstacktrace ();           Session.gettransaction (). rollback ();       } finally {           hibernateutils.closesession (session);       }    }


Hibernate-----Many-to-one association mappings (map File mode)

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.