Reputation: 31
Suppose i have a class Address which is dependent upon User class. Address is Component type. Can Address class contain reference to anyother entity say Country.
if yes?
Are all 4 relations possible with Address and country
one-to-one
many-to-one
many-to-many
one-to-many
If possible please explain with some JPA annotation
Upvotes: 0
Views: 375
Reputation: 14164
[Updated] Components are "stand-alone values" or "value objects". There is partial support for outbound relationships, and inbound relationships with the mappedBy
specification, but 'component' is not a first-class 'entity'.
Many persistence features -- querying, caching, locking, bulk loading & session-control features are all primarily designed at the 'entity' level. Beyond a certain point, there will be limitations with what you can do with a 'component'.. whether you will encounter such limitations depends on how deep you go.
But actually modelling your scenario, the utility of a Country
table with foreign-keys to it is limited. So probably most correct design is to keep Address.Country denormalized and support it with an auto-complete.
The reason this is the preferred approach:
When countries do change their names or borders -- it is typically not in a "relationally normalized" way. For example, Yugoslavia -> Croatia, Serbia, Montenegro. Sudan -> Sudan, South Sudan. Disputed borders shift & countries break up, but actually being able to just rename one well-identified country is not common.
Compare with people: People have stable identity & can change their name/address, but countries don't have this in a very useful way.
Upvotes: 3