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)