2016-03-18 24 views
7

Mam następujący setup Wiosna w mojej aplikacji Boot JPA:WZP mapowanie @ManyToOne między niezabudowany i EmbeddedId

zanurzalna

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

DDLs 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) 
); 

Kiedy idę do uruchomienia aplikacji, pojawia się następujący błąd:

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.foobar.entity.LogSearchHistoryAttr.logSearchHistory in com.foobar.entity.LogSearchHistory.logSearchHistoryAttrs

Nie jestem pewien, dlaczego ja dostaję ten błąd - mapowanie wygląda poprawnie (dla mnie). Co jest nie tak z tym mapowaniem, które mam? Dzięki!

+0

gdzie jest 'logSearchHistory' w' LogSearchHistoryAttr'? – Ramanlfc

+0

@Ramanlfc - znajduje się w obiekcie EmbeddedId. Czy należy go wyciągnąć i umieścić w "logSearchHistoryAttr"? – Ascalonian

+0

Ręcznie dodaj parametr targetEntity do adnotacji OneToMany. – Schaka

Odpowiedz

9

Przeniesiono atrybut mappedBy do osadzalnego klucza podstawowego, więc pole nie ma już nazwy logSearchHistory, ale raczej . Zmień mapowane przez wpis:

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

Dotyczy: JPA/Hibernate OneToMany Mapping, using a composite PrimaryKey.

Należy również dokonać serializacji klasy klucza głównego LogSearchHistoryAttrPK.

+0

Czy można opuścić mapowanie '@ ManyToOne'? Pytam, ponieważ pojawia się błąd:> BŁĄD: kolumna logsearchh8_.id nie istnieje – Ascalonian

+0

Jest nadal w moim teście. –

2

Na OneToMany części:

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