开发者

JPA one-to-many relationship question (relations on one entity)

开发者 https://www.devze.com 2022-12-08 16:49 出处:网络
In this JPA example there is a code: @OneToOne(cascade=CascadeType.ALL) private Deity mother; @OneToOne(cascade=CascadeType.ALL)

In this JPA example there is a code:

@OneToOne(cascade=CascadeType.ALL)
private Deity mother;

@OneToOne(cascade=CascadeType.ALL)
private Deity father;

@OneToMany(cascade=CascadeType.ALL)
private Set<Deity> children;

Why relation with father and mother is implemented by @OneToOne annotation and not in @ManyToOne relation? If Child and Parent will be separate classes Parent will have @OneToMany Collection<Child> children and Child have @ManyToOne Parent parent. This (deity) example seems reasonable but I looking for explanation why is that.

Links to JPA specification will be very appreciated开发者_C百科.


I think you are right, it is supposed to be ManyToOne relationship.

It must have been a mistake from the author of this code. Other parts of the code look suspicious also, I wouldn't trust it too much...


If you are looking for good persistence examples, I suggest the Hibernate documentation. Hibernate sticks with JPA as much as possible.

Note: You could switch your code to Hibernate, or stay with what you use and only use their documentation...


I am not sure but the example in site could refer to a person who has 1 mother and 1 father(hence unique one to one relation) and many children (one to many relation).

This example might be just to show that relations.

PS: i really am not sure, but then this is what occured to me..

0

精彩评论

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

关注公众号