Use of @OneToMany or @manytoone-annotation Relationship Mapping Chapter (Part One)

Source: Internet
Author: User
For example, we use an example to unlock the One-to-many and Many-to-many learning of JPA.

For example, you go to Dangdang to buy books, Dangdang will give you an order.
Through the above business activity description You can analyze that an order can contain one or more order items. Then we will make the order and order item design relationship (1:N) a one-to-many relationship (excluding 0). To get a relationship, we use JPA to implement this relationship (I'll stop talking about building JPA and basic configurations, if I don't understand, see the JPA Primer).

Create Order entity classes first
Java code ImportJava.util.HashSet; ImportJava.util.Set; ImportJavax.persistence.CascadeType; ImportJavax.persistence.Column; Importjavax.persistence.Entity; ImportJavax.persistence.Id; ImportJavax.persistence.OneToMany; Importjavax.persistence.Table; @Entity @Table (name= "orders") Public classOrder { PrivateString OrderID; PrivateFloat amount = 0f; Privateset<orderitem> items = NewHashset<orderitem> (); @Id @Column (length = 12) PublicString Getorderid () { returnOrderID } Public voidSetorderid (String OrderID) { This. OrderID = OrderID; } @Column (nullable = false) PublicFloat Getamount () { returnAmount } Public voidSetamount (Float amount) { This. Amount = Amount; } @OneToMany (cascade = {Cascadetype.refresh, cascadetype.persist,cascadetype.merge, Cascadetype.remove},mapped by = "Order"//Here the relationship is configured, and the relationship maintenance and the maintained side are determined. Mappby indicates that the relationship is maintained and only the relationship side has the right to update the foreign key. And here's the note onetomany The default loading mode is dependent loading. When you see the last word in the set relationship is many, the load defaults to lazy loading PublicSet<orderitem> GetItems () { returnItems } Public voidSetitems (set<orderitem> items) { This. items = items; /** * This method is used to add an order item to the order/* Public voidAddorderitem (OrderItem orderitem) {Orderitem.setorder ( This/////////////maintenance of relationship with relationship maintenance end This. Items.Add (OrderItem); }       }
Entity classes for order items
Java code ImportJavax.persistence.CascadeType; ImportJavax.persistence.Column; Importjavax.persistence.Entity; ImportJavax.persistence.GeneratedValue; ImportJavax.persistence.Id; ImportJavax.persistence.JoinColumn; ImportJavax.persistence.ManyToOne; @Entity Public classOrderItem { PrivateInteger ID; PrivateString ProductName; PrivateFloat sellprice = 0f; PrivateOrder of order; @Id @GeneratedValue PublicInteger GetId () { returnId } Public voidSETID (Integer ID) { This. id = ID; @Column (length = Nullable = false) PublicString Getproductname () { returnProductName; } Public voidSetproductname (String productName) { This. productName = ProductName; } @Column (nullable = false) PublicFloat Getsellprice () { returnSellprice; } Public voidSetsellprice (Float sellprice) { This. Sellprice = Sellprice; } @ManyToOne (cascade = {Cascadetype.merge,cascadetype.refresh}, optional = true) @JoinColumn (name= "order_id")//Here set Joincolum set the name of the foreign key, and OrderItem is the relationship maintenance side PublicOrder GetOrder () { returnOrder } Public voidSetorder (order) { This. order = order; }       }
Here's a look at the test class
Java code ImportJavax.persistence.EntityManager; ImportJavax.persistence.EntityManagerFactory; ImportJavax.persistence.Persistence; ImportOrg.junit.BeforeClass; ImportOrg.junit.Test; ImportCn.itcast.bean.Order; ImportCn.itcast.bean.OrderItem; Public classonetomanytest {@BeforeClass Public Static voidSetupbeforeclass () throwsException {}

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.