JPA @ManyToOne mapping between Embeddable and EmbeddedId

I have the following setup in my Spring Boot JPA application:

Recessed

@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

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

DDL Database

 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 run the application, I get the following error:

Called: org.hibernate.AnnotationException: mappedBy refers to an unknown property of the target entity: com.foobar.entity.LogSearchHistoryAttr.logSearchHistory in com.foobar.entity.LogSearchHistory.logSearchHistoryAttrs

I am not sure why I get this error - the matching looks right (for me). What is wrong with this display that I have? Thanks!

+7
java spring spring-boot hibernate jpa
source share
2 answers

You have moved the mappedBy attribute to the embedded primary key, so the field is no longer called logSearchHistory , but rather primaryKey.logSearchHistory . Change your card to record:

 @OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER) private List<LogSearchHistoryAttr> logSearchHistoryAttrs; 

Link: JPA / Hibernate OneToMany Mapping using the composite PrimaryKey .

You also need to make the primary key class LogSearchHistoryAttrPK serializable.

+10
source share

In the OneToMany section:

 @OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER) private List<LogSearchHistoryAttr> logSearchHistoryAttrs; 
+2
source share

All Articles