2011-01-25 71 views
3

JPA映射問題,我有以下映射複合鍵

@Entity 
@Table(name = "auctions") 
public class Auction{ 
. 
. 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "auction") 
    private List<AuctionParamValue> auctionParamValueList; 
. 
. 
} 


@Entity 
@Table(name = "auction_param_values") 
public class AuctionParamValue { 

    @EmbeddedId 
    protected AuctionParamValuePK auctionParamValuePK; 

    @JoinColumn(name = "auction_param_id", referencedColumnName = "auction_param_id",updatable=false,insertable=false) 
    @ManyToOne 
     private AuctionParam auctionParam; 

    @JoinColumn(name = "auction_id", referencedColumnName = "auction_id",updatable=false,insertable=false) 
    @ManyToOne @MapsId("auctionId") 
     private Auction auction; 
} 

@Embeddable 
public class AuctionParamValuePK { 
    @Id 
    @Basic(optional = false) 
    @Column(name = "auction_id") 
    private long auctionId; 

    @Id 
    @Basic(optional = false) 
    @Column(name = "auction_param_id") 
    private int auctionParamId; 
} 

@Entity 
@Table(name = "auction_params")  
public class AuctionParam { 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "auctionParam") 
    private List<AuctionTypeParam> auctionTypeParamList; 

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "auctionParam") 
    private List<AuctionParamValue> auctionParamValueList; 
} 

}

當我試圖堅持拍賣我得到以下錯誤

Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'auction_param_id' cannot be null 
Error Code: 1048 
Call: INSERT INTO auction_param_values (auction_param_val, create_ts, last_updt_ts, auction_param_id, auction_id) VALUES (?, ?, ?, ?, ?) 
    bind => [500, 2011-01-25 20:11:01.22, 2011-01-25 20:11:01.22, null, null] 
Query: InsertObjectQuery(com.eaportal.domain.AuctionParamValue[auctionParamValuePK=null]) 
Jan 25, 2011 8:11:01 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet dispatcher threw exception 
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'auction_param_id' cannot be null 
+0

`mappedBy`引用的`auction`和`auctionParam`屬性在哪裏? – axtavt 2011-01-25 15:02:24

+0

他們沒有映射。我應該如何映射它們? – Sourabh 2011-01-25 15:09:39

回答

5

Auction - AuctionParamValue關係你需要這樣做:

@Entity @Table(name = "auctions") 
public class Auction { 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "auction") 
    private List<AuctionParamValue> auctionParamValueList; 
    ... 
} 

@Entity @Table(name = "auction_param_values") 
public class AuctionParamValue { 
    @EmbeddedId 
    protected AuctionParamValuePK auctionParamValuePK; 

    @ManyToOne @MapsId("auctionId") 
    private Auction auction; 

    ... 
} 

關於AuctionParam我不確定你想表達什麼樣的關係。

您可以在JPA 2.0 specification的第2.4.1.3節中找到衍生身份的映射(即其中包含外鍵的複合鍵)的完整示例。