Java EE V7.0学习笔记:JPA

jpa并不难,但是比较繁琐,唯一令人疑惑的地方可能是Bidirectional Relationships关系。如下官方文档中的四句话可能是JPA唯一的难点。


BidirectionalRelationships



Theinverse side of a bidirectional relationship must refer to its owningside by using the mappedBy element of the @OneToOne, @OneToMany, or@ManyToMany annotation. The mappedBy element designates the propertyor field in the entity that is the owner of the relationship.


Themany side of many-to-one bidirectional relationships must not definethe mappedBy element. The many side isalways the owning side of the relationship.



Forone-to-one bidirectional relationships, the owning sidecorresponds(相一致,相符合) tothe side that contains the corresponding foreign key.


correspond: to be the same as or match something


Formany-to-many bidirectional relationships, either side may be theowning side.


其实本来可以简单一点,不过上面的写法确实很绕口。

mappedBy

1>只有OneToOneOneToManyManyToMany上才有mappedBy属性,ManyToOne不存在该属性;

2>mappedBy标签一定是定义在被拥有方的,他指向拥有方;

3>mappedBy的含义,应该理解为,拥有方能够自动维护跟被拥有方的关系,当然,如果从被拥有方,通过手工强行来维护拥有方的关系也是可以做到的;

4>mappedByjoinColumn/JoinTable总是处于互斥的一方,可以理解为正是由于拥有方的关联被拥有方的字段存在,拥有方才拥有了被拥有方。mappedBy这方定义JoinColumn/JoinTable总是失效的,不会建立对应的字段或者表


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值