我已经完成了我的代码,就像谷歌中的示例一样。我试图建立一对一的关系。但我得到错误:AnnotationException引用的属性不是(一个|多个)ToOne
问题:出了什么问题?
@Entity
@Table(name = "filesInfo")
@Inheritance(strategy= InheritanceType.JOINED)
public class FileInfo {
@Id
@SequenceGenerator(name = "file_info_sequence", sequenceName = "sq_file_info")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "file_info_sequence")
@Column(name = "id")
private long fileID;
@JsonIgnore
@OneToOne(mappedBy="fileInfo", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private FileContent fileContent;
//......
}
@Entity
@Table(name="file_content")
public class FileContent{
@Id
@Column(name="id", unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="fileInfo"))
private long fileID;
@JsonIgnore
@PrimaryKeyJoinColumn
private FileInfo fileInfo;
//....
}
错误: 的 java.lang.IllegalStateException :无法加载ApplicationContext
由引起:org.springframework.beans.factory.BeanCreationException:创建名为'sessionFactory'的bean在URL [file:src / test / resources / applicationContextTest.xml]中定义时出错:调用init方法失败;嵌套异常是org.hibernate.AnnotationException:引用的属性不是(One | Many)ToOne:FileInfo.fileContent的mappedBy中的FileContent.fileInfo
引起: org.hibernate.AnnotationException:引用的属性不是(One | Many)ToOne:FileInfo.fileContent的mappedBy中的FileContent.fileInfo
.......
答案 0 :(得分:3)
答案是将@OneToOne注释添加到FileContent.fileInfo字段。