gpt4 book ai didi

复合键的 JPA 映射问题

转载 作者:行者123 更新时间:2023-12-04 08:32:48 25 4
gpt4 key购买 nike

我有一个下面的映射

@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

最佳答案

对于 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 节。

关于复合键的 JPA 映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4794803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com