Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 5
Fetching order details for a customer is pretty easy once the owning side has been designed. In the previous section, you
saw that the Order entity was designed as the owning side, with a many-to-one relationship. The inverse of many-to-one is a one-to-many relationship.
The Customer entity in Listing 8 encapsulates the one-to-many relationship by being mapped to the owning side attribute customer.
@Entity(name = "CUSTOMER")
public class Customer {
@Id //signifies the primary key
@Column(name = "CUST_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long custId;
@Column(name = "FIRST_NAME", length = 50)
private String firstName;
@Column(name = "LAST_NAME", nullable = false,length = 50)
private String lastName;
@Column(name = "STREET")
private String street;
@OneToMany(mappedBy="customer",targetEntity=Order.class,
fetch=FetchType.EAGER)
private Collection orders;
...........................
// The other attributes and getters and setters goes here
}
The @OneToMany annotation in Listing 8 introduces a new attribute: fetch. The default fetch type for one-to-many relationship is LAZY. FetchType.LAZY is a hint to the JPA runtime, indicating that you want to defer loading of the field until you access it. This is called
lazy loading. Lazy loading is completely transparent; data is loaded from the database in objects silently when you attempt to read the
field for the first time. The other possible fetch type is FetchType.EAGER. Whenever you retrieve an entity from a query or from the EntityManager, you are guaranteed that all of its eager fields are populated with data store data. In order to override the default fetch
type, EAGER fetching has been specified with fetch=FetchType.EAGER. The code in Listing 9 fetches the order details for a particular Customer.
........
EntityManager em = entityManagerFactory.createEntityManager();
Customer customer = em.find(Customer.class, 100);
System.out.println("Order details for customer 100 : " + customer.getOrders());
em.close();
entityManagerFactory.close();
.........
There is one last leg of relationship mapping left to consider. An order can consist of one or more products, whereas a product can be associated with zero or more orders. This is a many-to-many relationship, as illustrated in Figure 4.
To define a many-to-many relationship, the sample application will introduce a join table named ORDER_DETAIL that holds the
association between the Order and Product tables, as shown in Listing 10.
@Entity(name = "ORDERS")
public class Order {
@Id
@Column(name = "ORDER_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;
@Column(name = "CUST_ID")
private long custId;
@Column(name = "TOTAL_PRICE", precision = 2)
private double totPrice;
@OneToOne(optional=false,cascade=CascadeType.ALL, mappedBy="order",
targetEntity=Invoice.class)
private Invoice invoice;
@ManyToOne(optional=false)
@JoinColumn(name="CUST_ID",referencedColumnName="CUST_ID")
private Customer customer;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="ORDER_DETAIL",
joinColumns=
@JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID"),
inverseJoinColumns=
@JoinColumn(name="PROD_ID", referencedColumnName="PROD_ID")
)
private List<Product> productList;
...............
The other attributes and getters and setters goes here
}
The @JoinTable annotation is used to specify a table in the database that will associate order IDs with product IDs. The entity that specifies
the @JoinTable is the owner of the relationship; in this case, the Order entity is the owner of the relationship with the Product entity.
Listing 11 demonstrates how to fetch the product details for a particular Order.
..........
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
Order order = em.find(Order.class, 111);
System.out.println("Product : " + order.getProductList());
em.close();
entityManagerFactory.close();
..........
Archived Discussions (Read only)