2016-03-18 15 views
7

tôi đã thiết lập sau đây trong ứng dụng Spring Boot JPA tôi:JPA lập bản đồ @ManyToOne giữa nhúng và EmbeddedId

nhúng

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

Cơ sở dữ liệu DDLs

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

Khi tôi đi đến khởi động ứng dụng, tôi nhận được lỗi sau:

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

Tôi không chắc chắn lý do tại sao tôi nhận được lỗi này - ánh xạ có vẻ chính xác (với tôi). Điều gì là sai với bản đồ này mà tôi có? Cảm ơn!

+0

nơi là 'logSearchHistory' trong 'LogSearchHistoryAttr'? – Ramanlfc

+0

@Ramanlfc - nó nằm trong đối tượng EmbeddedId. Nó có nên được kéo ra khỏi đó và đưa vào 'logSearchHistoryAttr'? – Ascalonian

+0

Thêm thủ công targetEntity vào chú thích OneToMany của bạn. – Schaka

Trả lời

9

Bạn đã di chuyển thuộc tính mappedBy vào khóa chính có thể nhúng, do đó trường này không còn được đặt tên là logSearchHistory mà là primaryKey.logSearchHistory. Thay đổi ánh xạ của bạn theo mục nhập:

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

Tham chiếu: JPA/Hibernate OneToMany Mapping, using a composite PrimaryKey.

Bạn cũng cần phải tạo lớp khóa chính LogSearchHistoryAttrPK có thể tuần tự hóa.

+0

Bạn có thể rời khỏi bản đồ '@ ManyToOne' được không? Tôi hỏi vì tôi nhận được lỗi:> L ERI: cột logsearchh8_.id không tồn tại – Ascalonian

+0

Nó vẫn còn trong đó trong thử nghiệm của tôi. –

2

Tại OneToMany phần:

@OneToMany(mappedBy = "primaryKey.logSearchHistory", fetch = FetchType.EAGER) 
private List<LogSearchHistoryAttr> logSearchHistoryAttrs; 
Các vấn đề liên quan