Hibernate annotation配置方式的MappedBy使用详解说是使用详解,其实是我自己做了一些简单的试验.如有说错,请指正. Annotation方式的MappedBy其实就是xml方式的inverse http:///questions/10082434/mappedby-and-inverse-attributes-are-same 写道 In case of one-one or one-many or many to one, the key will be stored in one of the entites. It does not make any sense to store it in 2 places. It essential tells hibernate that the key is at the other end of the relationship. So if there are entities A and B which have a one to one relationship, and you see the mappedBy attribute in A, then the foreign key (which points to A) is stored in B. 但是对于他们的行为 我一直不是很了解 hibernate里的doc解释也不是很清楚: 写道 inverse 映射属性究竟表示什么呢?对于你和 Java 来说,一个双向关联仅仅是在两端简单地正确设置引用。然而,Hibernate 并没有足够的信息去正确地执行 INSERT 和 UPDATE 语句(以避免违反数据库约束),所以它需要一些帮助来正确的处理双向关联。把关联的一端设置为 inverse 将告诉 Hibernate 忽略关联的这一端,把这端看成是另外一端的一个镜象(mirror) 其实简单点说 就是MappedBy确定"维护关系"的一方,也仅仅是"维护关系"而已.其他的操作不影响. 实验环境:hibernate 3.6.10.Final 实体类:User(多方) Classroom(一方) Many-to-One的测试
Java代码:
Java代码 :
从多方插入就不再累述了. 这里就试着从不维护关系的一方来插入: Java代码:
这样只有一条插入语句: Hibernate: insert into Classroom (cname) values (?) 为了更好地说明问题,我把以上一方的配置进行修改 加入了 cascade=CascadeType.ALL 同样执行以上代码 显示插入了两条: Hibernate: insert into Classroom (cname) values (?) Hibernate: insert into User (cid, uname, upass) values (?, ?, ?) 但是查看数据库就会发现:
虽然插入了..但是外键却没有值 一方不维护关系就可以明显的看出来, 但是一方的其他不影响关系的更新操作是没有问题的: Java代码:
执行以上的代码: 显示: Hibernate: select classroom0_.cid as cid1_0_, classroom0_.cname as cname1_0_ from Classroom classroom0_ where classroom0_.cid=? Hibernate: select users0_.cid as cid1_1_, users0_.uid as uid1_, users0_.uid as uid0_0_, users0_.cid as cid0_0_, users0_.uname as uname0_0_, users0_.upass as upass0_0_ from User users0_ where users0_.cid=? cc Hibernate: update User set cid=?, uname=?, upass=? where uid=? 数据在数据库里也被正常更新 这样一番实验坐下来就可以很清楚的感觉到 mappedBy只是确定了由谁来维护关系而已 其他的操作并不影响.
一般情况下 如果要让两方都可以维护关系 我自己是在OneToMany后写JoinColumn 然后和ManyToOne里的外键列名取一样的。 当然也可以不写JoinColumn 来生成一个第三方表记录关系 但这样做没什么必要 相同的关系会被存两次. |
|
来自: wayne_liberary > 《技术博文》