JPA多对多中间表的记录自动删除
今天我在项目里对前几天写的一个代码做测试(我的实体的关系是:一个用户实体,一个权限实体,这两个实体的关系是对多对,关系维护端是用户),结果发现查询用户表时,会自动把权限的中间表的记录删除。
我的用户实体的映射是这样的:
@ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) private List<Role> roles;我写的测试是:
@Test public void find(){ User user = this.userservice.findById("201158080111"); System.out.println(user.getRoles()); }结果在测试运行时,它会自动把中间表的记录删除,控制台打印的信息是:
[main]-[INFO] 2013-10-04- 11:02:14.354 jdbc.sqlonly select user0_.id as id1_1_0_, user0_.birthday as birthday2_1_0_, user0_.birthplace as birthpla3_1_0_, user0_.disable as disable4_1_0_, user0_.email as email5_1_0_, user0_.ip as ip6_1_0_, user0_.name as name7_1_0_, user0_.password as password8_1_0_, user0_.phone as phone9_1_0_, user0_.regtime as regtime10_1_0_, user0_.salt as salt11_1_0_ from user user0_ where user0_.id='201158080111' [main]-[INFO] 2013-10-04- 11:02:14.355 jdbc.sqltiming select user0_.id as id1_1_0_, user0_.birthday as birthday2_1_0_, user0_.birthplace as birthpla3_1_0_, user0_.disable as disable4_1_0_, user0_.email as email5_1_0_, user0_.ip as ip6_1_0_, user0_.name as name7_1_0_, user0_.password as password8_1_0_, user0_.phone as phone9_1_0_, user0_.regtime as regtime10_1_0_, user0_.salt as salt11_1_0_ from user user0_ where user0_.id='201158080111' {executed in 1 msec} [main]-[INFO] 2013-10-04- 11:02:14.405 jdbc.audit 1. Connection.prepareStatement(select roles0_.user_id as user_id1_1_1_, roles0_.role_id as role_id2_2_1_, role1_.id as id1_0_0_, role1_.permissions as permissi2_0_0_, role1_.role as role3_0_0_ from user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.id where roles0_.user_id=?) returned net.sf.log4jdbc.PreparedStatementSpy@4dbfa7d0 [main]-[INFO] 2013-10-04- 11:02:14.406 jdbc.sqlonly select roles0_.user_id as user_id1_1_1_, roles0_.role_id as role_id2_2_1_, role1_.id as id1_0_0_, role1_.permissions as permissi2_0_0_, role1_.role as role3_0_0_ from user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.id where roles0_.user_id='201158080111' [main]-[INFO] 2013-10-04- 11:02:14.407 jdbc.sqltiming select roles0_.user_id as user_id1_1_1_, roles0_.role_id as role_id2_2_1_, role1_.id as id1_0_0_, role1_.permissions as permissi2_0_0_, role1_.role as role3_0_0_ from user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.id where roles0_.user_id='201158080111' {executed in 1 msec} [main]-[INFO] 2013-10-04- 11:02:14.473 jdbc.audit 1. Connection.prepareStatement(delete from user_role where user_id=?) returned net.sf.log4jdbc.PreparedStatementSpy@74eb350 [main]-[INFO] 2013-10-04- 11:02:14.474 jdbc.sqlonly delete from user_role where user_id='201158080111' [main]-[INFO] 2013-10-04- 11:02:14.474 jdbc.sqltiming delete from user_role where user_id='201158080111' {executed in 0 msec}望有哪位大神可以帮我解决。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不要使用多对多,使用一对多和多对一解决这个中间表
这个咋解决呢?
我也碰到这样的问题
我的意思是 userservice.findById 这里面是不是多写了删除代码?
回复
肯定没有啊,有的话,我还出来问你们。。。。。。。
findbyid没有删除吗?
findbyid没有删除吗?
用很笨的方法解决了,不过为什么springside不会出现这种情况?