Hi All,
I have two table USER and ADDRESS. A user may have more than one address.
I have two address for one user
Code:
@Entity
@Table(name = "USER", uniqueConstraints = { @UniqueConstraint(columnNames = { "user_id" }) })
public class UserDO {
@Id
@Column(name = "user_id", columnDefinition = "VARCHAR(255) NOT NULL DEFAULT ''", insertable = true)
private String userId;
@OneToMany(cascade= CascadeType.ALL, mappedBy="userDO", fetch = FetchType.LAZY)
private Set<AddressDO> userAddress;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Set<AddressDO> getUserAdrress() {
return this.userAddress;
}
public void setUserAdrress(Set<AddressDO> userAddress) {
this.userAddress = userAddress;
}
}
Code:
@Entity
@Table(name = "ADDRESS",uniqueConstraints = {
@UniqueConstraint(columnNames={"address_id"})}
)
public class AddressDO {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "address_id", insertable = true)
private long address_id;
@ManyToOne
@org.hibernate.annotations.Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE })
@JoinColumn(name="user_id")
private UserDO userDO;
public long getAddress_id() {
return address_id;
}
public void setAddress_id(long address_id) {
this.address_id = address_id;
}
public UserDO getUserDO() {
return userDO;
}
public void setUserDO(UserDO userDO) {
this.userDO = userDO;
}
}
This is working fine for insert operation in session.save(userDo);
But for session.update(userDo); instead of updating the address table it is inserting again.
For delete also the USER table got deleted but not from ADDRESS table
Any Idea?