JPA -- mapping one-way one to many relationships -- 11

Keywords: Attribute Java

By adding the set collection to the Customer class. That is, there is one party to maintain the relationship. Use the annotation @ onetoman, and then in the Order class

There is no need to add a foreign key, but the resulting data table and one-way many to one table structure are the same. There are no redundant columns in the Customer table, but in the

The Order table adds a foreign key column. The difference is that the maintenance of adding, deleting, modifying and querying the foreign key column is maintained by one party.

public class Customer {

	private Integer id;
	private String lastName;
	private String email;
	private int age;
	private Date creatTime;
	private Date brith;

	private Set<Order> orders = new HashSet<Order>();

	@GeneratedValue(strategy =GenerationType.AUTO)
	public Integer getId() {
		return id;
	public void setId(Integer id) { = id;
	public String getLastName() {
		return lastName;
	public void setLastName(String lastName) {
		this.lastName = lastName;
	public String getEmail() {
		return email;
	public void setEmail(String email) { = email;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;
	public Date getCreatTime() {
		return creatTime;
	public void setCreatTime(Date creatTime) {
		this.creatTime = creatTime;
	public Date getBrith() {
		return brith;
	public void setBrith(Date brith) {
		this.brith = brith;

	 * Mapping one-to-many relationships
	 * @OneToMany: One side of mapping one
	 * @JoinColumn: Map the foreign key. The name attribute specifies the name of the foreign key.
	 * You can use the fetch property of @ onetoman to modify the default load policy
	 * You can modify the default delete policy through @ onetoman's cascade attribute
	public Set<Order> getOrders() {
		return orders;
	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	public String toString() {
		return "Customer [id=" + id + ", lastName=" + lastName + ", email="
				+ email + ", age=" + age + ", creatTime=" + creatTime
				+ ", brith=" + brith + "]";


Additions and deletions

	 * When a one-way 1-n association relationship is saved, there must be more UPDATE statements
	 * Because one end of n will not insert a foreign key column at the same time
	public void testOneToManyPersist() {
		Customer customer = new Customer();
		customer.setBrith(new Date());
		customer.setCreatTime(new Date());

		Order order1 = new Order();
		Order order2 = new Order();

		//Set association relationship


	 *  By default, the lazy load policy is used for the associated multiple parties
	 *  You can use the fetch property of @ onetoman to modify the default load policy

	public void testOneToManyFind() {
		Customer customer = entityManager.find(Customer.class, 9);

	 * By default, if one end of 1 is deleted, the foreign key at the end of the associated n will be left blank and then deleted
	 * The default deletion policy can be modified through the cascade attribute of @ onetoman. Cascade deletion is implemented.
	public void testOneToManyRemove(){
		Customer customer = entityManager.find(Customer.class, 8);


Posted by JamieinNH on Wed, 06 Nov 2019 12:07:15 -0800