java JPA 在 Embeddable 和 EmbeddedId 之间映射@ManyToOne
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36084173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA mapping @ManyToOne between Embeddable and EmbeddedId
提问by Ascalonian
I have the following setup in my Spring Boot JPA application:
我在 Spring Boot JPA 应用程序中有以下设置:
Embeddable
可嵌入
@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;
...
}
EmbeddedId
嵌入式 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;
...
}
Database DDLs
数据库 DDL
CREATE TABLE log_search_history (
id serial NOT NULL,
...
CONSTRAINT log_search_history_pk PRIMARY KEY (id)
);
CREATE TABLE log_search_history_attr (
search_history_id INTEGER NOT NULL,
attr CHARACTER VARYING(50) NOT NULL,
value CHARACTER VARYING(100),
CONSTRAINT log_search_history_attr_pk PRIMARY KEY (search_history_id, attr),
CONSTRAINT log_search_history_attr_fk1 FOREIGN KEY (search_history_id) REFERENCES
log_search_history (id)
);
When I go to start the application, I get the following error:
当我去启动应用程序时,出现以下错误:
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.foobar.entity.LogSearchHistoryAttr.logSearchHistory in com.foobar.entity.LogSearchHistory.logSearchHistoryAttrs
引起:org.hibernate.AnnotationException:mappedBy 引用一个未知的目标实体属性:com.foobar.entity.LogSearchHistory.logSearchHistoryAttrs 中的 com.foobar.entity.LogSearchHistoryAttr.logSearchHistory
I am not sure why I am getting this error - the mapping looks correct (to me). What is wrong with this mapping that I have? Thanks!
我不确定为什么会收到此错误 - 映射看起来正确(对我而言)。我的这个映射有什么问题?谢谢!
回答by K.Nicholas
You moved the mappedBy
attribute into an embeddable primary key, so the field is no longer named logSearchHistory
but rather primaryKey.logSearchHistory
. Change your mapped by entry:
您将mappedBy
属性移动到可嵌入的主键中,因此该字段不再命名logSearchHistory
,而是primaryKey.logSearchHistory
. 更改您的映射条目:
@OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER)
private List<LogSearchHistoryAttr> logSearchHistoryAttrs;
Reference: JPA / Hibernate OneToMany Mapping, using a composite PrimaryKey.
参考:JPA / Hibernate OneToMany Mapping,使用复合 PrimaryKey。
You also need to make the primary key class LogSearchHistoryAttrPK
serializable.
您还需要使主键类可LogSearchHistoryAttrPK
序列化。
回答by lukaszwrzaszcz
At OneToManypart:
在一对多部分:
@OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER)
private List<LogSearchHistoryAttr> logSearchHistoryAttrs;