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>只有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性;
2>mappedBy标签一定是定义在被拥有方的,他指向拥有方;
3>mappedBy的含义,应该理解为,拥有方能够自动维护跟被拥有方的关系,当然,如果从被拥有方,通过手工强行来维护拥有方的关系也是可以做到的;
4>mappedBy跟joinColumn/JoinTable总是处于互斥的一方,可以理解为正是由于拥有方的关联被拥有方的字段存在,拥有方才拥有了被拥有方。mappedBy这方定义JoinColumn/JoinTable总是失效的,不会建立对应的字段或者表