JPA多对多中间表的记录自动删除

发布于 2021-12-04 14:26:27 字数 3237 浏览 937 评论 8

今天我在项目里对前几天写的一个代码做测试(我的实体的关系是:一个用户实体,一个权限实体,这两个实体的关系是对多对,关系维护端是用户),结果发现查询用户表时,会自动把权限的中间表的记录删除。

我的用户实体的映射是这样的:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

输什么也不输骨气 2021-12-06 13:12:30

不要使用多对多,使用一对多和多对一解决这个中间表

心欲静而疯不止 2021-12-06 12:46:08

这个咋解决呢?

左岸枫 2021-12-06 12:04:16

我也碰到这样的问题

像你 2021-12-06 11:29:22

我的意思是 userservice.findById 这里面是不是多写了删除代码?

噩梦成真你也成魔 2021-12-06 10:59:28

回复
肯定没有啊,有的话,我还出来问你们。。。。。。。

倾城泪 2021-12-06 07:47:31

findbyid没有删除吗?

泛泛之交 2021-12-06 07:39:34

findbyid没有删除吗?

断爱 2021-12-05 13:04:11

用很笨的方法解决了,不过为什么springside不会出现这种情况?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文