- 背景:
在上一篇文章中实现双向关联时,其中在Customer.java中我们使用了java.util.List<Order>来关联多的Order。其实还有另外一种实现方法:使用java.util.Set来替代java.util.List。
Customer.java(定义Order:private java.util.Set<Order> orders=new HashSet<Order>();)
1 package com.dx.hibernate.onetomany; 2 3 import java.util.HashSet; 4 import java.util.List; 5 import java.util.Set; 6 7 public class Customer { 8 private Integer customerId; 9 private String customerName; 10 private Set<Order> orders = new HashSet<Order>(); 11 12 public Customer() { 13 } 14 15 public Customer(String customerName) { 16 super(); 17 this.customerName = customerName; 18 } 19 20 public Integer getCustomerId() { 21 return customerId; 22 } 23 24 public void setCustomerId(Integer customerId) { 25 this.customerId = customerId; 26 } 27 28 public String getCustomerName() { 29 return customerName; 30 } 31 32 public void setCustomerName(String customerName) { 33 this.customerName = customerName; 34 } 35 36 public Set<Order> getOrders() { 37 return orders; 38 } 39 40 public void setOrders(Set<Order> orders) { 41 this.orders = orders; 42 } 43 }