开发者

JPA multi-tier relationship

开发者 https://www.devze.com 2023-03-20 20:34 出处:网络
I have a problem where I am stuck :(. I have to build a relationship of a relationship( strange !). For example, I have a relation whereone entity A can have many B (OneToMany)

I have a problem where I am stuck :(. I have to build a relationship of a relationship( strange !). For example, I have a relation where one entity A can have many B (OneToMany)

A ----> B

Now I need another relationship where an entity C will have an OneToOne relationship with the composite relationship between A and B. Like,

C--> (A--->B)  , not C-->A-->B  

Not that C will have a relation with A and A has with B,it should be like like C--> D (A --> B).

The generated joined tables does not have any primary key in JPA and now I am thinking How can I map this relationship. Please be noted that the normal A-->B is also needed as this can not be embeddable and it should be persistent.

Thanks.

Example: Location and Event are my two entities and I want to relate CleanPlan on it. Means one CleanPlan is related to event-1 of location-1. so I am 开发者_StackOverflow社区specifying that have a cleanplan on location-1 in event-1. Does that make sense?

JPA multi-tier relationship


I would say this

JPA multi-tier relationship

or this, depending on Event-Location cardinality.

JPA multi-tier relationship


There are two aspects of this problem in JPA. 1) A mapping table is craeted. 2) A mapping row is created instead of mapping table.

I think the 2nd option will make your life easier. But that is only possible if Event and Location has an unidirectional relation. Then on ManyToOne side, put "mappedBy". And that will generate a mapping table on ManyToOne table. And you can refer that column on CleanPlan table.

0

精彩评论

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