Hibernate4之多对一单向关联

一个客户可以有多个订单,订单对应一个客户的关系就是多对一。
在这里多对一的关系是@ManyToOne,默认是非延迟加载的。如果要设置懒加载必须设置fetch = FetchType.LAZY,但是既然配置了多对一,肯定是希望加载订单的时候加载出客户信息的。

/**
 * @author Chou
 * @since 2012-8-8
 * ...客户实体
 *
 */

@Entity
@Table(name = "TAB_CUSTOMER")
@SequenceGenerator(name = "seq", sequenceName = "SEQ_CUSTOMER", allocationSize = 1)
public class Customer {
	private Long id;
	private String cname;
	private String address;
	private String phone;
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	......

}


/**
 * @author Chou
 * @since 2012-8-8
 * ...订单实体(多对一关系维护方)
 *
 */

@Entity
@Table(name = "TAB_ORDER")
@SequenceGenerator(name = "seq", sequenceName = "SEQ_ORDER", allocationSize = 1)
public class Order {
	private Long id;
	private String orderno;
	private Double money;
	private Customer customer;
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

        .....
		
	@ManyToOne
	@Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinColumn(name = "CUSTOMERID")
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
}

你可能感兴趣的:(Hibernate,jpa,多对一单向)