我有两个实体,邮政和图片。 我想使Picture尽可能独立。 Post应该具有图片,但不是强制性的。现在,实体看起来像这样。
@Entity
@Table(name = "pictures")
public class Picture implements Comparable<Picture> {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String file;
//getters and setters
}
@Entity
@Table(name = "posts")
public class Post implements Comparable<Post> {
@Id
@GeneratedValue
private long id;
//some extra fields
private Picture picture;
//getters and setters
}
问题是我不知道我要注释图片字段以建立连接的含义。 我尝试使用@OneToOne和@MapsId进行注释(基于Vlad Mihalcea的指南),但是当我尝试使用空图片字段保存Post实体时,会出现这样的错误
org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.acme.Post.picture]
解决此问题的正确方法是什么?谢谢