JPA合并导致外国实体的重复输入 [英] JPA merge results in duplicate entry of foreign entity
问题描述
问题:
有没有人知道怎么合并而没有 EntityManager
试图重新插入外国实体?
Does anyone know how to merge without having EntityManager
trying to re-insert the foreign entity?
情景:
只是设置一个与我的案例非常匹配的场景:我有两个实体
Just to set up a scenario that closely matches my case: I have two entities
@Entity
@Table(name = "login", catalog = "friends", uniqueConstraints =
@UniqueConstraint(columnNames = "username"))
public class Login implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "username", unique = true, nullable = false, length = 50)
private String username;
@Column(name = "password", nullable = false, length = 250)
private String password;
}
@Entity
@Table(name = "friendshiptype", catalog = "friends")
public class FriendshipType implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "username")
private Login login;
@Column(name = "type", unique = true, length = 32)
private String type;
...//other fields go here
}
登录
实体和 FriendshipType
实体分别持久保存到数据库。然后,稍后,我需要将 Login
行与 FriendshipType
行合并。当我调用 entityManager.merge(友谊)
时,它会尝试插入一个新的登录
,这当然会导致以下结果错误
Both the Login
entity and the FriendshipType
entity are persisted to the database separately. Then, later, I need to merge a Login
row with a FriendshipType
row. When I call entityManager.merge(friendship)
, it tries to insert a new Login
which of course results in the following error
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'myUserName1350319637687' for key 'username'
Error Code: 1062
Call: INSERT INTO friends.login (password, username) VALUES (?, ?)
我的问题是,如何在没有enityManager尝试重新插入外来对象的情况下合并两个对象?
My question, again, is how do I merge two objects without having enityManager trying to reinsert the foreign object?
推荐答案
以下是我解决问题的方法。我终于想出合并没有解决的原因是因为login.id是由JPA自动生成的。因为我真的不需要自动生成的id字段,我将其从架构中删除并使用 username
作为 @id
字段:
Here is how I solve the problem. I finally figure the reason the merge is not resolving is because the login.id is auto generated by JPA. So since I really don't need an auto-generated id field, I remove it from the schema and use username
as the @id
field:
@Entity
@Table(name = "login", catalog = "friends", uniqueConstraints =
@UniqueConstraint(columnNames = "username"))
public class Login implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "username", unique = true, nullable = false, length = 50)
private String username;
@Column(name = "password", nullable = false, length = 250)
private String password;
}
我遇到的另一种解决方案,我没有实施,但可能有所帮助别人,他们是否需要拥有自动生成的id字段。
Another solution that occurred to me, which I didn't implement but may help someone else, should they need to have an auto-generated id field.
而不是创建 Login的实例
对于合并,从数据库中获取实例。我的意思是,而不是
Instead of creating an instance of Login
for the merger, get the instance from the database. What I mean is, instead of
Login login = new Login(); login.setUsername(username); login.setPassword(password);
宁愿
Login login = loginDao.getByUsername(username);
这样,就不会生成新的id字段,使实体看起来与众不同。
That way, a new id field is not generated making the entity seem different.
感谢所有人的帮助,特别是@mijer非常耐心。
Thanks and up-votes to everyone for helping, especially to @mijer for being so patient.
这篇关于JPA合并导致外国实体的重复输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!