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.

Customer.java

@Table(name="JPA_CUSTOMER")
@Entity
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)
	@Id
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(name="LAST_NAME")
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Temporal(TemporalType.TIME)
	public Date getCreatTime() {
		return creatTime;
	}
	public void setCreatTime(Date creatTime) {
		this.creatTime = creatTime;
	}
	@Temporal(TemporalType.DATE)
	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
	 */
	@JoinColumn(name="CUSTOMER_ID")
    @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.REMOVE})
	public Set<Order> getOrders() {
		return orders;
	}
	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}
	@Override
	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
	 */
	@Test
	public void testOneToManyPersist() {
		Customer customer = new Customer();
		customer.setAge(16);
		customer.setBrith(new Date());
		customer.setCreatTime(new Date());
		customer.setEmail("mm@com");
		customer.setLastName("mm");

		Order order1 = new Order();
		order1.setOrderName("O-mm-1");
		Order order2 = new Order();
		order2.setOrderName("O-mm-2");

		//Set association relationship
		customer.getOrders().add(order1);
		customer.getOrders().add(order2);

		entityManager.persist(customer);
		entityManager.persist(order1);
		entityManager.persist(order2);
	}

	/**
	 *  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
	 */

	@Test
	public void testOneToManyFind() {
		Customer customer = entityManager.find(Customer.class, 9);
		System.out.println(customer.getLastName());

		System.out.println(customer.getOrders().size());
	}
	/**
	 * 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.
	 */
	@Test
	public void testOneToManyRemove(){
		Customer customer = entityManager.find(Customer.class, 8);
		entityManager.remove(customer);
	}

 

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