Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA在Embedded和EmbeddedId之间的映射@ManyToOne_Java_Spring_Hibernate_Jpa_Spring Boot - Fatal编程技术网

Java JPA在Embedded和EmbeddedId之间的映射@ManyToOne

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

我的Spring Boot JPA应用程序中有以下设置:

可嵌入的

@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;