質問:
EntityManager
外国の実体を再挿入しようとせずにマージする方法を知っている人はいますか?
シナリオ:
私のケースにぴったり一致するシナリオを設定するためだけに:私には2つのエンティティがあります
@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
}
Login
エンティティとエンティティの両方がFriendshipType
別々にデータベースに永続化されます。次に、後で、Login
行を行とマージする必要がありFriendshipType
ます。を呼び出すとentityManager.merge(friendship)
、新しいものを挿入しようとしLogin
ますが、もちろん次のエラーが発生します
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が外部オブジェクトを再挿入しようとせずに2つのオブジェクトをマージするにはどうすればよいですか?