我的问题很简单:
Auser
有address
,address
由city
,state
和组成country
。
所以为此我有这个结构:
public class User {
..
//bi-directional many-to-one association to UserAddress
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="id_user_address")
private UserAddress userAddress;
..
}
请注意,CascadeType
没有,CascadeType.ALL
因为如果用户被删除,它不会影响cities, states and countries
创建的。
public class UserAddress {
..
//bi-directional many-to-one association to AddressCity
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.})
@JoinColumn(name="id_city")
private AddressCity addressCity;
//bi-directional many-to-one association to AddressCountry
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="id_country")
private AddressCountry addressCountry;
//bi-directional many-to-one association to AddressState
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="id_state")
private AddressState addressState;
..
}
我的问题是,如果我尝试注册一个具有相同功能的用户,city
例如它会给我一个例外:
严重:原因:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:键“city_UNIQUE”的重复条目“Campinas”
我怎样才能避免这种constraint
,保持CASCADE
?
有谁知道如何在我的 JSF 项目中显示 JPA 所做的查询?