Java JPA在Embedded和EmbeddedId之间的映射@ManyToOne
我的Spring Boot JPA应用程序中有以下设置: 可嵌入的Java JPA在Embedded和EmbeddedId之间的映射@ManyToOne,java,spring,hibernate,jpa,spring-boot,Java,Spring,Hibernate,Jpa,Spring Boot,我的Spring Boot JPA应用程序中有以下设置: 可嵌入的 @Embeddable public class LogSearchHistoryAttrPK { @Column(name = "SEARCH_HISTORY_ID") private Integer searchHistoryId; @Column(name = "ATTR", length = 50) private String attr; @ManyToOne @Jo
@Embeddable
public class LogSearchHistoryAttrPK {
@Column(name = "SEARCH_HISTORY_ID")
private Integer searchHistoryId;
@Column(name = "ATTR", length = 50)
private String attr;
@ManyToOne
@JoinColumn(name = "ID")
private LogSearchHistory logSearchHistory;
...
}
嵌入式ID
@Repository
@Transactional
@Entity
@Table(name = "LOG_SEARCH_HISTORY_ATTR")
public class LogSearchHistoryAttr implements Serializable {
@EmbeddedId
private LogSearchHistoryAttrPK primaryKey;
@Column(name = "VALUE", length = 100)
private String value;
...
}
OneToMany
@Repository
@Transactional
@Entity
@Table(name = "LOG_SEARCH_HISTORY")
public class LogSearchHistory implements Serializable {
@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer id;
@OneToMany(mappedBy = "logSearchHistory", fetch = FetchType.EAGER)
private List<LogSearchHistoryAttr> logSearchHistoryAttrs;
...
}
当我启动应用程序时,出现以下错误:
导致原因:org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性:com.foobar.entity.LogSearchHistoryAttr.logSearchHistory在com.foobar.entity.logSearchHistoryAttrs中
我不确定为什么会出现这个错误-映射看起来(在我看来)是正确的。我的映射有什么问题?谢谢 您将
mappedBy
属性移动到可嵌入的主键中,因此该字段不再命名为logSearchHistory
,而是命名为primaryKey.logSearchHistory
。更改您的“按条目映射”选项:
@OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER)
private List<LogSearchHistoryAttr> logSearchHistoryAttrs;
@OneToMany(mappedBy=“primaryKey.logSearchHistory”,fetch=FetchType.EAGER)
私有列表logSearchHistoryAttrs;
参考文献:
您还需要使主键类
LogSearchHistoryAttrPK
可序列化。AtOneToMany部分:
@OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER)
private List<LogSearchHistoryAttr> logSearchHistoryAttrs;
@OneToMany(mappedBy=“primaryKey.logSearchHistory”,fetch=FetchType.EAGER)
私有列表logSearchHistoryAttrs;
其中是LogSearchHistoryAttr
中的logSearchHistory
?@Ramanlfc-它位于EmbeddedId对象中。是否应将其从那里拉出并放入logSearchHistoryAttr
?手动将targetEntity添加到OneToMany批注中。是否可以保留@manytone
映射?我问这个问题是因为我得到了一个错误:>error:column logsearch8_u8;id不存在,它仍然在我的测试中。
@OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER)
private List<LogSearchHistoryAttr> logSearchHistoryAttrs;