我的项目给我留下了深刻的印象,但突然间我注意到我做错了什么,因为每次创建、合并、删除我都是手动进行的,但我应该让 JPA 为我处理这件事对吗?
我所做的是创建数据库,然后从中创建实体。所以这是我的数据库,所以你们可以理解我的概念问题。
以一种简单的方式: Anuser
有一个address
,只有一个地址。这address
由city
和state
组成country
。我希望能够创建、更新user address
逐级联。
我认为我的由 JPA 生成的映射是错误的(或者我的理解是错误的),让我展示一下:
public class User {
..
//bi-directional many-to-one association to UserAddress
@ManyToOne(cascade = CascadeType.ALL) // should be one to one ?
@JoinColumn(name="id_user_address")
private UserAddress userAddress;
..
}
public class UserAddress {
..
//bi-directional many-to-one association to User
@OneToMany(mappedBy="userAddress", fetch=FetchType.EAGER)
private List<User> users;
//bi-directional many-to-one association to AddressCity
@ManyToOne
@JoinColumn(name="id_city")
private AddressCity addressCity;
//bi-directional many-to-one association to AddressState
@ManyToOne
@JoinColumn(name="id_state")
private AddressState addressState;
//bi-directional many-to-one association to AddressCountry
@ManyToOne
@JoinColumn(name="id_country")
private AddressCountry addressCountry;
..
}
我认为user
有one adress only
,所以它应该是OneToOne
映射?UserAdress
关于country
和state
也是如此city
。