One to many collection java

Java Tutorial/JPA/One To Many Mapping

import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Department
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy=»department») private Collection students; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String deptName) < this.name = deptName; >public Collection getStudents() < return students; >public void setStudent(Collection students) < this.students = students; >public String toString()

One To Many Cascade.All

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Student
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @ManyToOne(cascade=CascadeType.ALL) private Department department; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String name)
public Department getDepartment() < return department; >public void setDepartment(Department department) < this.department = department; >public String toString()

One To Many Join Table Setup

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Phone
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name=»NUM») private String number; private String type; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getNumber() < return number; >public void setNumber(String phoneNo) < this.number = phoneNo; >public String getType() < return type; >public void setType(String phoneType) < this.type = phoneType; >public String toString()

Читайте также:  Php ini open basedir disable

One To Many List Collection

import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Student
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @ManyToOne private Department department; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String name) < this.name = name; >public Department getDepartment() < return department; >public void setDepartment(Department department) < this.department = department; >public String toString()

One To Many Map Collection

import java.util.HashMap; import java.util.Map; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MapKey; import javax.persistence.OneToMany; @Entity public class Department
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy=»department») @MapKey(name=»name») private Map students; public Department() < students = new HashMap(); > public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String deptName) < this.name = deptName; >public void addStudent(Student student) < if (!getStudents().containsKey(student.getName())) < getStudents().put(student.getName(), student); if (student.getDepartment() != null) < student.getDepartment().getStudents().remove(student.getName()); >student.setDepartment(this); > > public Map getStudents() < return students; >public String toString()

One To Many With Mapped By To remove Mapping Table

import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Department
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy=»department»,cascade=CascadeType.ALL) private Set students; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String deptName) < this.name = deptName; >public Set getStudents() < return students; >public void setStudent(Set students) < this.students = students; >public String toString()

One To Many With Order By Setting

import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OrderBy; @Entity public class Department
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy=»department») @OrderBy(«name ASC») private List students; public Department() < students = new ArrayList(); > public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String deptName) < this.name = deptName; >public void addStudent(Student student) < if (!getStudents().contains(student)) < getStudents().add(student); if (student.getDepartment() != null) < student.getDepartment().getStudents().remove(student); >student.setDepartment(this); > > public Collection getStudents() < return students; >public String toString()

Читайте также:  Расширение html соответствует следующему типу файла

Set target Entity For One To Many Mapping

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Student
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @ManyToOne private Department department; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String name)
public Department getDepartment() < return department; >public void setDepartment(Department department) < this.department = department; >public String toString()

Use Collection In One To Many Mapping

import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Department
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy=»department») private Collection students; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String deptName) < this.name = deptName; >public Collection getStudents() < return students; >public void setStudent(Collection students) < this.students = students; >public String toString()

Источник

One to many collection java

Specifies a many-valued association with one-to-many multiplicity. If the collection is defined using generics to specify the element type, the associated target entity type need not be specified; otherwise the target entity class must be specified. If the relationship is bidirectional, the mappedBy element must be used to specify the relationship field or property of the entity that is the owner of the relationship. The OneToMany annotation may be used within an embeddable class contained within an entity class to specify a relationship to a collection of entities. If the relationship is bidirectional, the mappedBy element must be used to specify the relationship field or property of the entity that is the owner of the relationship. When the collection is a java.util.Map , the cascade element and the orphanRemoval element apply to the map value.

Example 1: One-to-Many association using generics // In Customer class: @OneToMany(cascade=ALL, mappedBy=»customer») public Set getOrders() < return orders; >In Order class: @ManyToOne @JoinColumn(name=»CUST_ID», nullable=false) public Customer getCustomer() < return customer; >Example 2: One-to-Many association without using generics // In Customer class: @OneToMany(targetEntity=com.acme.Order.class, cascade=ALL, mappedBy=»customer») public Set getOrders() < return orders; >// In Order class: @ManyToOne @JoinColumn(name=»CUST_ID», nullable=false) public Customer getCustomer() < return customer; >Example 3: Unidirectional One-to-Many association using a foreign key mapping // In Customer class: @OneToMany(orphanRemoval=true) @JoinColumn(name=»CUST_ID») // join column is in table for Order public Set getOrders()

Optional Element Summary

(Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.

Element Detail

targetEntity

(Optional) The entity class that is the target of the association. Optional only if the collection property is defined using Java generics. Must be specified otherwise. Defaults to the parameterized type of the collection when defined using generics.

cascade

(Optional) The operations that must be cascaded to the target of the association. Defaults to no operations being cascaded. When the target collection is a java.util.Map , the cascade element applies to the map value.

fetch

(Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistence provider runtime that the associated entities must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.

mappedBy

orphanRemoval

public abstract boolean orphanRemoval

(Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.

Copyright © 1996-2015, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.

Источник

Оцените статью