Hibernate one-to-many Two-way Association

Source: Internet
Author: User

A category of goods with multiple products, multiple products corresponding to the same commodity category, this relationship is a One-to-many two-way 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; More to a private Category Category; public int getId () {return id;} public void setId (int id) {this.id = ID;} public Category getcategory () {return categ Ory; 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; Collection, 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 relationship:

<?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= "M"/> </property> & Lt;property name= "Mark" type= "java.lang.String" > <column name= "Mark" length= "M"/> </property> <!- -One-to-many, one commodity type has multiple merchandise--> <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 commodities correspond to a category of goods:

<?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= "/> </" Property> <!--More than one, multiple products corresponding to a category--> <many-to-one name= "category" Class= "Com.pojo.Category" 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, 2009 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 (); The public static void Addpro () {Sessions 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 cell phone"); Product 2 Product P1 = new product (); P1.setproductname ("Motorola A1200"); P1.setremark ("Very good cell phone"); One-to-many association set<product> Set = c.getproducts (); Set.add (P); Set.add (p1); Direct preservation of commodity categories, the automatic cascade of merchandise 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 ("Commodity 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 ("Commodity 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 operations 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 operations 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 merchandise under this category the public static void Delete () {Session session = Hibernatemanager.opensession (); Category C = (Category) session.load (Category.class, New Integer (1)); Cascade Deletion of Session.delete (c); try {hibernatemanager.closesession ();} catch (Exception e) {e.printstacktrace (); Hibernatemanager.rollbacktransaction (); } } }

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.