• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

About my exam cloud mock 2, @CollectionTable  RSS feed

 
Ranch Hand
Posts: 1732
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Table                                  Entity
CUSTOMER - Primary Key : CUST_ID        Customer
ORDER - Primary Key: ORDER_ID           ORDER
CUST_ORDERS - Primary Key: CUST_ID,
ORDER_ID; other  columns :Order_Date

For each customer, you want to keep the order date for each of the order. You want to keep it as a Map in Customer entiy.

Which of the following options correctly declares this field in Customer entity code?
A. @ElementCollection
  @CollectionTable(name="CUST_ORDERS")
  @Column (name="ORDER_DATE")
  private Map<Order,Date> orders;

B. @Entity
  public class Customer{
  @JoinTable(name=CUST_ORDERS")
  @ManyToMany
  private Set<Order> orders;
 
  }
C.@ElementCollecton
 @JoinedTable(name="CUST_ORDER")
 private Map<Order,Date> orders;

Given answer A.
A declares a correct way to model a many to many relationship when the relationship itself has one
additonal attribute. There is a many to many relationshop between customer and order and each such
relationship has attribute orderDate.
CUST_ORDERS is a join table , but with addtional column for order date that can be captured as
a Map value. The mapping for the key of the map (ie Order) is specified using @CollectionTable,
while mapping for the value of the map (ie Date) is specified using @Column.



I think the correct way is:
@ElementCollection
@CollectionTable(name="CUST_ORDERS")
@MapKeyJoinColumn(name="ORDER_ID")
@Column(name="ORDER_DATE")
Map<Order,Date> orders;

If @MapKeyJoinColumn is not specified, by default, the map key join column is "ORDERS_KEY" (the name of the property or field concat with "_KEY").
But in the CUST_ORDER table, there is no column with name ORDERS_KEY.

 
Creator of Enthuware JWS+ V6
Posts: 3272
296
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need to override the default mapping for the foreign key column of Customer:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!