JPA Learning Notes (8)--mapping a one-to-many association relationship

Source: Internet
Author: User

One-to-many correlation relationship

This article has many and many to one is the same, therefore will not write very detailed. If you don't understand, you can refer to the JPA study notes (7)--mapping many-to-one relationships

Order entity Class

 PackageCom.jpa.helloworld2;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.FetchType;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;ImportJavax.persistence.Id;ImportJavax.persistence.JoinColumn;ImportJavax.persistence.ManyToOne;Importjavax.persistence.Table;ImportJavax.persistence.TableGenerator;@Table(name="T_order")@Entity Public  class Order {    @Column(name="ID")@GeneratedValue    @Id    PrivateInteger ID;@Column(name="Order_name")PrivateString Ordername; PublicIntegergetId() {returnId } Public void setId(Integer ID) { This. id = ID; } PublicStringGetordername() {returnOrdername; } Public void Setordername(String ordername) { This. ordername = Ordername; }@Override     PublicStringtoString() {return "Order [id="+ ID +", ordername="+ Ordername +"]"; }}

User entity class

 PackageCom.jpa.helloworld2;ImportJava.util.ArrayList;ImportJava.util.List;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;ImportJavax.persistence.Id;ImportJavax.persistence.JoinColumn;ImportJavax.persistence.OneToMany;Importjavax.persistence.Table;@Table(name="T_user")@Entity Public  class User {    @Column(name="ID")@GeneratedValue(Strategy=generationtype.auto)@Id    PrivateInteger ID;@Column(name="NAME")PrivateString name;@JoinColumn(name="user_id")@OneToMany    Privatelist<order> orders =NewArraylist<> (); PublicIntegergetId() {returnId } Public void setId(Integer ID) { This. id = ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicList<order>getorders() {returnOrders } Public void setorders(list<order> orders) { This. orders = orders; }@Override     PublicStringtoString() {return "User [id=]+ ID +", name="+ name +", orders="+ orders +"]"; }}

A user can have more than one order, and in user there is a property List<Order> orders , which is a one-to-many association. It's a little different from many to one.

One-to-many use @onetomany annotations, and foreign-key relationships use @joincolumn (name= "user_id").

Note: The @joincolumn (name= "user_id"), although written in the USER entity class, is added to the T_order table in the "user_id" field.

Insert
Order Order1 = New Order ();Order1. Setordername("O1");Order Order2 = New Order ();Order2. Setordername("O2");User user = new user ();User. SetName("Tom");User. GetOrders(). Add(Order1);User. GetOrders(). Add(Order2);Insert Entitymanager. Persist(Order1);Entitymanager. Persist(Order2);Entitymanager. Persist(user);

Here the three objects are inserted in the order in which the results are reversed. This is a bit different from many to one.

Inquire
System.out.println(entityManager.find(User.class,1));

A one-to-many query that uses lazy-loaded loading policies By default, you can use the @OneToMany(fetch=FetchType.EAGER) change policy

Delete
User user2 = entityManager.find(User.class1);entityManager.remove(user2);

In many-to-one, this deletion throws an exception because of a foreign key constraint, but the pair is a bit different.

As you can see, it is to place the user_id field blank. But this is obviously not what we want.

In fact, we can use the Cascade property to change the policy, for example, cascade Delete.

@OneToMany(cascade=CascadeType.REMOVE)

Cascade Property There are other values, we Baidu or read the document.

Modify
User u = entityManager.find(User.class2);u.getOrders().get(0).setOrderName("orderName");

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JPA Learning Notes (8)--mapping a one-to-many association relationship

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.