There are multiple commodities under one commodity category, and multiple items correspond to the same commodity category, which is a one-to-many bidirectional association.
Product Category:
Package Com.pojo; /** * Product entity. * * @author myeclipse Persistence Tools * * Public class Product implements java.io.Serializable {private static final Lon G Serialversionuid = -7963752710605063544l; private int id; Private String ProductName; Private String remark; Many to a private category category; public int getId () {return ID,} public void setId (int id) {this.id = ID,} public Category getcategory () {return categ Ory The public void Setcategory (category category) {this.category = category;} public Product () {} public String Getproductna Me () {return this.productname;} public void Setproductname (String productname) {this.productname = productname;} publi C String Getremark () {return this.remark;} public void Setremark (String remark) {This.remark = remark;}}
Product Category:
Package Com.pojo; Import Java.util.HashSet; Import Java.util.Set; /** * Category entity. * * @author myeclipse Persistence Tools * * Public class Category implements java.io.Serializable {private static final lo ng serialversionuid = -666185106951167028l; private int id; private String name; Private String Mark; Set, a pair of more private set<product> products = new hashset<product> (); public int getId () {return ID,} public void setId (int id) {this.id = ID,} public set<product> getproducts () {RE Turn products; } public void Setproducts (set<product> products) {this.products = products;} public Category () {} public String G Etname () {return this.name;} public void SetName (String name) {this.name = name;} public String Getmark () {return thi S.mark; public void Setmark (String mark) {This.mark = Mark;}}
XML configuration file, one-to-many relationships:
<?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 "> <!--mapping file autogenerated by MyEclipse persistence Tools---< hibernate-mapping> <class name= "com.pojo.Category" table= "Category" schema= "ZM" > <id name= "id" type= " Java.lang.Integer "> <column name=" ID "precision=" 8 "scale=" 0 "/> <generator class=" increment "/> </id > <property name= "name" type= "java.lang.String" > <column name= "name" length= "/>" </property> & Lt;property name= "Mark" type= "java.lang.String" > <column name= "Mark" length= "/>" </property> <!- -one-to-many, a commodity type with multiple items--<set name= "Products" cascade= "All" outer-join= "true" > <key column= "Producttypeid" foreign-key= "id" ></key> <one-to-many class= "com.pojo.Product"/> </set> </class> </ Hibernate-mapping>
Multiple items correspond to one product Category:
<?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 "> <!--mapping file autogenerated by MyEclipse persistence Tools---< hibernate-mapping> <class name= "com.pojo.Product" table= "Product" schema= "ZM" > <id name= "id" type= " Java.lang.Integer "> <column name=" ID "precision=" 8 "scale=" 0 "/> <generator class=" increment "/> </id > <property name= "ProductName" type= "java.lang.String" > <column name= "ProductName" length= "/>" < /property> <property name= "remark" type= "java.lang.String" > <column name= "Remark" length= "all"/> </ Property> <!--to one, multiple items corresponding to one category--<many-to-one name= "category" Class= "Com.pojo.Category" outer-join= "true "> <column name=" Producttypeid "></column> </many-to-one> </class>
Test class:
Package com.test; Import java.util.List; Import Java.util.Set; Import org.hibernate.Session; Import Com.pojo.Category; Import com.pojo.Product; Import Com.util.HibernateManager; public class Test1 {/** * Beckham Dec, 7:30:06 PM */public static void main (string[] args) {test1.delete ();} p ublic static void AddType () {Session session = Hibernatemanager.opensession (); Category C = new category (); C.setname ("Computer"); C.setmark ("This is the computer category"); try {session.save (c); Hibernatemanager.closesession (); } catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); }} public static void Addpro () {Session session = Hibernatemanager.opensession ();//get type category C = (category) Sessio N.load (Category.class, New Integer (1)); Product 1 Product p = new product (); P.setproductname ("Motorola V8"); P.setremark ("Very good phone"); Product 2 Product P1 = new product (); P1.setproductname ("Motorola A1200"); P1.setremark ("Very good phone"); One-to-many association set<product> Set = c.getproducts (); Set.add (P); Set.add (p1); The product category is stored directly, and the product is automatically cascaded to save Session.save (c); try {hibernatemanager.closesession ();} catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); }} public static void Loadpro () {Session session = Hibernatemanager.opensession (); Product P = (product) session.load (Product.class, New Integer (1)); System.out.println ("category number:" + p.getcategory (). GetId ()); System.out.println ("Category name:" + p.getcategory (). GetName ()); SYSTEM.OUT.PRINTLN ("Category description:" + p.getcategory (). Getmark ()); SYSTEM.OUT.PRINTLN ("Product number" + P.getid ()); SYSTEM.OUT.PRINTLN ("Product Name:" + p.getproductname ()); SYSTEM.OUT.PRINTLN ("Product Description:" + P.getremark ()); try {hibernatemanager.closesession ();} catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); }} @SuppressWarnings ("Unchecked") public static void Loadprolist () {Session session = Hibernatemanager.opensession ();// First get the type list<product> list = Session.createquery ("from Product"). List (); for (Product p:list) {System.out.println ("category number:" + P.GetCategory (). GetId ()); System.out.println ("Category name:" + p.getcategory (). GetName ()); SYSTEM.OUT.PRINTLN ("Category description:" + p.getcategory (). Getmark ()); SYSTEM.OUT.PRINTLN ("Product number" + P.getid ()); SYSTEM.OUT.PRINTLN ("Product Name:" + p.getproductname ()); SYSTEM.OUT.PRINTLN ("Product Description:" + P.getremark ()); System.out.println ("<br/>"); } try {hibernatemanager.closesession ();} catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); }}//Modify commodity type, cascade operation public static void Updatecate () {Session session = Hibernatemanager.opensession (); Category C = (category) Session.load (Category.class, New Integer (1)); Set<product> products = c.getproducts (); for (Product p:products) {if (P.getid () = = 1) {p.setproductname ("Nokia 6300"), P.setremark ("very good"),}} SESSION.UPDA Te (c); try {hibernatemanager.closesession ();} catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); }}//Modify commodity type, cascade operation public static void Updatepro () {Session session = Hibernatemanager.openseSsion (); Product P = (product) session.load (Product.class, New Integer (1)); Category C = p.getcategory (); P.setproductname ("Samsung"); P.setremark ("Very useful mobile phone"); Session.update (c); try {hibernatemanager.closesession ();} catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); }}//delete a category and cascade Delete all goods under the category public static void Delete () {Session session = Hibernatemanager.opensession (); Category C = (category) Session.load (Category.class, New Integer (1)); Cascade Delete Session.delete (c); try {hibernatemanager.closesession ();} catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); } } }