开发者

Hibernate design dilemma; parent - child relationship with address link

I have an existing entity design. There is an Individual entity. A Guardian or a Minor are stored as Individual. If u r a Minor, u r assigned zero or more guardians. This relationship is stored in a different entity.

There is an additional requirement; to add a flag to show that Guardian Address should be used for the Minor if the relationship is set. One can clear that flag at a later time. If the flag is cleared, the existing address will be used and no changes to guardian's address will overwrite the minor's address.

The relationship between Minor and Guardian is stored as

(minor_individual_id, guardian_individual_id, type_of_the_relationship)

where type_of_the_relationship can have values such "legal" etc..

Is it as simple as adding a new flag to the relationship table ? Once added, the minor's address needs to be changed to the guardian's address. The address information is stored in another entity (Address). Can this be done via Hibernate built-in functionality ? Or should we add a layer to take care of this functionality ? Is there anything in Hibernate that lets me know the flag has been updated and a new set of updates need to happen ?

The address entity contains the individual id and the address for the individual in a single table.

Once the flag is set, it can only be set once per minor since it will cause confusion to have minor having multiple guardians to have address cascade to be turned on for each of its guardians. Thanks.

Database Structure:

Individual ( Table/Entity)

Id

Age ( Depending on Age, you are treated as minor )

GuardianRelationShip ( Table/En开发者_开发问答tity)

Minor_Individual_Id

Guardian_Individual_Id

Address (Table/Entity)

Individual_Id

AddressLine1

City

State

If u r a Minor with Individual id of 2 and a have a Guardian with Individual id of 1. This will look like

Id Age (Individual)
1   40 Guardian
2    5 Minor

Minor Guardian (GuardianRelationShip)
2     1

IndividualId City State (Address)
1            LA    CA
2            NY    NY

Once those are linked, the idea is that the address for the minor "individual id of 2" will be changed to NY/NY.

If there is a req. that says use guardian address for minor and allow for unlinking address, how would you do that ?


You have the AddressEntity. I would create IndividualEntity.address and minor's address could reference the same AddressEntity as one of guardians (BTW, several guardians could live at the same address too!). Afterwards, you could check if minor's address equals to one of guardian's address.

I don't understand which problem the flag solves. Please provide more info, along with some code to illustrate your entities and relations.


Allow an Individual entry without a corresponding Address entry. If you want to limit it to just guardians, only permit that case when the entry for the Individual table doesn't have a corresponding match in the GuardianRelationship.Minor column. If address is null, lookup guardian and use that address.

If you do enforce that rule above, make sure you consider edge cases that change the structure such as an individual no longer having a listed guardian and having no address.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜