org.springframework.orm.jpa.JpaSystemException: More than one row with the given identifier

本文介绍了一种在使用Spring ORM JPA时遇到的More than one row with the given identifier was found异常的解决方案。通过调整实体类的主键设置,从单一主键改为复合主键,解决了在向具有联合主键的中间表插入数据时可能出现的重复主键冲突问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

org.springframework.orm.jpa.JpaSystemException: More than one row with the given identifier was found: 42, for class
这个问题说是NHibernate 有AB两个表 A和B一对一,B和A多对一,在没有数据情况下没问题,在有数据是就出现异常more than one row with the given identifier was found。网上好多的都是建议把数据重新清除然后就可以了。
但是我的代码并不是这样的。我的是这样解决的
由于我的业务是向中间表添加数据。由于中间表是联合主键,可能多次添加就会报上面的异常。
我的实体中是这样的添加了重复的主键报错了

@Entity
@Table(name = "usr_firm_role")

public class UsrFirmRole implements Serializable {

	@Id
	@Column(name = "role_id")
	private Integer roleId;

	@Column(name = "firm_id")
	private Integer firmId;

改成这样就可以了

@Entity
@Table(name = "usr_firm_role")
@IdClass(UsrFirmRolePk.class)
public class UsrFirmRole implements Serializable {

	@Id
	@Column(name = "role_id")
	private Integer roleId;

	@Id
	@Column(name = "firm_id")
	private Integer firmId;
@Data
public class UsrFirmRolePk implements Serializable {
    private Integer roleId;

    private Integer firmId;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值