I have the entity classes below. When a user first signs up, only the username and password are supplied, so the list of accounts (think profiles) is empty. Later, when they add an account, the user object is updated in the client, passed to the server, and then entityManager.merge(user) is called. When the user is merged, the account is added 6 times to the database and the address supplied is added three times. I'm not sure why. I would like the account to be added once and only one address to be added. Any ideas on what may be happening?
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="user_accounts")
private List<Account> accounts;
//...getters and setters ...
}
@Entity
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="address")
private Address address;
//...getters and setters...
}
@Entity
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="street")
private String street;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
@Column(name="zip")
private String zip;
//...getters and setters...
}
This is a known issue with using merge where the collections are lists. Unfortunately now real fix yet: HHH-5855
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With