Hibernate Annotation @mappedBy含义
2010-08-27 10:24
309 查看
原帖请见:http://revolver77.spaces.live.com/blog/cns!583C0A9A80FC1137!209.entry
EJB3使用心得(1)-mappedBy的含义
1、关于mappedBy的含义:a) 只有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性;
b) mappedBy标签一定是定义在the owned side(被拥有方的),他指向the owning side(拥有方);
c) mappedBy的含义,应该理解为,拥有方能够自动维护跟被拥有方的关系;
当然,如果从被拥有方,通过手工强行来维护拥有方的关系也是可以做到的。
例如:
Person.java @OneToOne(optional = true, cascade = CascadeType.ALL, mappedBy = "person") public IDCard getIdcard() { return idcard; } IDCard.java @OneToOne(optional = false, cascade = CascadeType.ALL) @JoinColumn(name = "Person_ID", referencedColumnName = "personid",unique= true) public Person getPerson() { return person; } OneToOneDAOBean.java /*上面例子中IDCard为拥有方,这里强行使用Person作为主控方,手工维护两个实体的关系*/ public void insertPerson(String name, boolean sex,short age, Date birthday,String cardID) { Person person = new Person(); person.setName(name); person.setSex(sex); person.setAge(Short.valueOf(age)); person.setBirthday(birthday); IDCard idcard = new IDCard(cardID); idcard.setPerson(person); person.setIdcard(idcard); em.persist(person); } /*其实既然IDCard为拥有方,那么将IDCard视为主控方的话,他自动维护了两个实体的关系*/ public void insertPerson1(String name, boolean sex,short age, Date birthday,String cardID) { Person person = new Person(); person.setName(name); person.setSex(sex); person.setAge(Short.valueOf(age)); person.setBirthday(birthday); IDCard idcard = new IDCard(cardID); idcard.setPerson(person); //person.setIdcard(idcard); //em.persist(person); em.persist(idcard); } |
e) 标准的关系映射,定义在ejb-3_0-fr-spec-persistence.pdf 的2.1.8 Relationship Mapping Defaults 章节中:
The following rules apply to bidirectional relationships: • The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation. The mappedBy element designates the property or field in the entity that is the owner of the relationship. • The many side of one-to-many / many-to-one bidirectional relationships must be the owning side, hence the mappedBy element cannot be specified on the ManyToOne annotation. • For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key. • For many-to-many bidirectional relationships either side may be the owning side. |
相关文章推荐
- Hibernate Annotation @mappedBy含义在使用onetoone 时候【外键双向关联】(十三)
- 谈 Hibernate Annotation @mappedBy含义
- Hibernate Annotation @mappedBy含义
- Hibernate_基于Annotation的使用_ManyToOne_Demo(mappedBy="person")
- Hibernate Annotation mappedBy注解理解
- Hibernate_基于Annotation的使用_OneToOne_Demo(不维护关系那方(mappedBy="person"))
- 转Hibernate Annotation mappedBy注解理解
- nested exception is org.hibernate.AnnotationException: Unknown mappedBy in
- Understanding mappedBy annotation in Hibernate
- org.hibernate.AnnotationException: Associations marked as mappedBy must not define database mappings
- org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
- hibernate基于注解的维护权反转:@OneToMany(mappedBy=)
- Can someone please explain mappedBy in hibernate?
- Caused by: org.hibernate.AnnotationException: No identifier specified for entity
- hibernate 单向外键关联,一对一双向外间关联凡是双向关联必设mappedBy!!!
- hibernate mappedBy
- 码农小汪-Hibernate学习9-hibernate双向关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable
- Hibernate之mappedBy
- Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: table is not Mapped
- annotation @OneToMany(mappedBy="")