• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

A couple of question about Jpa

 
Bartender
Posts: 1204
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to study JPA more in depth - untill now, just used it quite superficially - and I'm stuck on a conceptual problem. I hope any expert here could help me.

So, I have fictional OrderMaster and OrderDetail entities, in a many-to-one (bidirectional) relationship. Just for sake of making things a bit more difficult, I want to use a composite primary key , not autogenerated, for each entity. For OrderMaster, I defined a PK <OrderID int , CustomerID String>, while
PK for OrderDetail is <OrderID, CustomerID, IDRow>.
I want to use a shared key, (I hope that's the correct term) so that each row on my DB is:
- for the order master: OrderID, CustomerID + other columns defining the order as a whole
- for the detail: OrderID, CustomerID, IDRow + other columns defining details.

In other words, I don't want to have two explicit, separated columns to map the OrderMaster.

I defined the following classes:





with the following composite keys (I used inheritance)





The first question is if is it correct to use a method like :



to explicitely set the link between entities, or if it should be a JPA provider's task to ensure that corresponding columns are set equal to each other.

Second question: I found (practically) that the following mapping:



behaves exactly like this one:



and I'd like to know if there are any differences between those two mappings.

Thanks id advance !!!

 
Creator of Enthuware JWS+ V6
Posts: 3309
298
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Second question: I found (practically) that the following mapping:


I can only answer your question from the specs (just looking at JPA 2.0 section 2.4.1)

The correct mapping is is the one where you explicitly tell the JPA provider that the "masterKey" attribute of the OrderDetailKey contains the mapping to the OrderMaster. (you can leave out the "masterKey" attribute from the annotation @MapsId("masterKey") when the composite key of the OrderDetail and OrderMaster are the same, but the @MapsId should be there.)

When using the @MapsId annotation, the specs state that:

The embedded id attributes that correspond to the relationship are treated by the provider as “read only”—that is, any updates to them on the part of the application are not propagated to the database.



That the mapping without the @MapsId works can be luck or can be a non-spec-related feature of the JPA implementation you are using. (it might not be portable to another JPA implementation)
 
Claude Moore
Bartender
Posts: 1204
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great answer, thanks man  
 
I do some of my very best work in water. Like this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!