我的图片表与用户表有两个onetoone关系:
图片实体:
@Entity
@Table(name = "pictures")
public class Picture implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
public int getId() {
return id;
}
@OneToOne
@JoinColumn(name = "customer_id", nullable = true)
private User customer;
public User getCustomer() {
return customer;
}
public void setCustomer(User customer) {
this.customer = customer;
}
@OneToOne
@JoinColumn(name = "photographer_id", nullable = false)
private User photographer;
public User getPhotographer() {
return photographer;
}
public void setPhotographer(User photographer) {
this.photographer = photographer;
}
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private BigDecimal price;
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Picture(String title,
String url,
BigDecimal price)
{
this.title = title;
this.url = url;
this.price = price;
}
public Picture() {
// Empty constructor
}
}
图片模板:
@Override
public void insert(Picture object) {
sessionFactory.getCurrentSession().save(object);
}
如何更新这两个外键 customer_id
以及 photographer_id
?
在实体中,两个外键是一对一的关系 User
我得到的错误是 Column 'photographer_id' cannot be null
1条答案
按热度按时间goqiplq21#
你做了一个非常错误的数据库设计。
数据库实体之间不能建立两个关系,表\实体之间只能建立一个关系。
根据你的设计,你需要
@OneToMany
关系Picture
以及User
,对于每一张/一张图片,你都有很多用户,即使事实上你最终只有两个用户,因为两个用户仍然不是一个/一张,所以它被认为是一样多的。我建议你先重新设计数据库。