One-to-many relationship mappings
The Set node information is configured in a one-party configuration file:
The properties of the set node in the *.hbm.xml configuration file:
Lazy: The default is true to use lazy loading, false for instant loading;
Order-by: A party to a set set of multiple entities within a set of the sort method by default is the primary key sort but can be changed;
Cascade: Whether to use cascade operation; Configuration method: The value of Cascade is as follows;
All: Associated operations are performed in all cases, i.e. save-update and delete.
None: Associated operations are not performed in all cases. This is the default value.
Save-update: The associated operation is performed when Save/update/saveorupdate is executed.
Delete: The associated operation is performed when the delete is executed.
All-delete-orphan: When a node becomes an orphan node in an object graph, the node is deleted
Inverse: Invert. True indicates that the other party is responsible for control, and False indicates that it is controlled by itself.
Cascade Delete: (Configure the Set node in one of the configuration files of a party)
The value of Cascade uses all to indicate that all operations are cascaded;
Change the value of the Set node's property inverse to true:
It is possible to implement cascade deletion, that is, when one party is deleted, the entire record of the primary key of the party referenced to one of the parties is deleted.
One-to-many ' a '
Vo
PackageOrg.sunny.shop.vo;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.List;ImportJava.util.Set; Public classCategory {Private intCID; PrivateString CNAME; PrivateDate Dateinsert; PrivateDate dateupdate; //use a collection to associate with more than one party PrivateSet<category2th>Category2thset; /** * The rest is setter, getter method **/}
Category.hbm.xml
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hi Bernate-mapping-3.0.dtd "><hibernate-mapping> <classname= "Org.sunny.shop.vo.Category"Table= "category"> <IDname= "CID"column= "CID"> <Generatorclass= "Identity" /> </ID> < Propertyname= "CNAME" /> < Propertyname= "Dateinsert" /> < Propertyname= "Dateupdate" /> <!--configuring a collection of level two classifications - <Setorder-by= "CSID"name= "Category2thset"Lazy= "false"Cascade= "All"Inverse= "true"> <Keycolumn= "CID"/> <One-to-manyclass= "org.sunny.shop.vo.Category2th"/> </Set> </class></hibernate-mapping>
One-to-many ' more '
Vo
PackageOrg.sunny.shop.vo;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Set;
Public classcategory2th {Private intCSID; PrivateString CSName; //category Add Time PrivateDate Dateinsert; //Category Update Time PrivateDate dateupdate; PrivateCategory category; //using a Vo class with a one-to-many ' a ' association PrivateSet<product>Productset; /** * The rest is setter, getter method **/ }
Category2th.hbm.xml
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "><hibernate-mapping> <classname= "org.sunny.shop.vo.Category2th"Table= "Categorysecond"> <IDname= "CSID"column= "CSID"> <Generatorclass= "Identity" /> </ID> < Propertyname= "CSName"/> < Propertyname= "Dateinsert" /> < Propertyname= "Dateupdate" /> <!--relationship between level two classification and first class classification - <Many-to-onename= "category"Lazy= "false"class= "Org.sunny.shop.vo.Category"> <columnname= "CID"></column> </Many-to-one> </class></hibernate-mapping>
Hibernate learning Note (i): Cascade Delete