java - JPA and how to think about relationships -


i learning jpa relationships (@onetoone, @manytoone, ...) , talk people how model entities, more confused getting. here's example yesterday:

lets have relationship between person , address. db stores address.id fk in persons table "address_id". relationship onetoone or onetomany? i've heard people argue each , both have reasons why feel way.

one individual argued "is manytoone since address_id not unique each record. same address_id used on , on again in persons table. argues have onetoone, fk in persons table have unique directed in jsr spec".

another individual states "when think person record, have more 1 address? if not, it's onetoone; why complicate matter"

i confused true. on 1 hand, first person able bring jsr spec , talk me through it. on other hand, have developer on 20 years experience (and highly respected) telling me keep simple.

can please clarify me

thank you!

i'll take association person address.

a given person can't have more 1 address, since associated table has foreign key column address.

so, can onetoone or manytoone.

it's decide 1 is, , choice doesn't depend on technical. if don't want 2 different persons ever share same address, it's onetoone. if decide 2 persons can share same address, it's manytoone.

if it's onetoone, should make sure it's indeed onetoone, , 2 persons can't share same address. effective , simple way add unique constraint of address_id column of person table. hibernate doesn't care if set constraint or not.

your "other individual" wrong. if 2 persons can share same address, should manytoone. not onetoone. imagine make bidirectional association: since address can shared several persons, have onetomany association between address , person, inverse of association between person , address. , onetomany's inverse association must manytoone, not onetoone.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -