EJB3 Entity之间的关系
最近在看EJB3,找到了guide for EJB3 on JBoss,大概看了一下,觉得还不错,写个笔记做个记录。
实体之间的关系,用了好久,但有时用到还是要到网上再查一遍,干脆自己记录下来,免得麻烦。
1.one-to-one
这种关系属于单向关系,两个Entity在一边把关系明确就可以。在Customer Entity里面这样定义:
@OneToOne(cascade = {CascadeType.ALL})@JoinColumn(name = "ADDRESS_ID")public Address getAddress(){ return address;}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="order")public Collection<LineItem> getLineItems(){ return lineItems;}
@ManyToOne@JoinColumn(name = "order_id")public Order getOrder(){ return order;}
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy="customers") public Set<Flight> getFlights() { return flights; }
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)@JoinTable(table = @Table(name = "flight_customer_table"), joinColumns = {@JoinColumn(name = "FLIGHT_ID")}, inverseJoinColumns = {@JoinColumn(name = "CUSTOMER_ID")})public Set<Customer> getCustomers(){ return customers;}