【发布时间】:2011-01-27 07:29:47
【问题描述】:
我有一个下面的映射
@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 @MapsId("auctionParamId")
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")
@Nullable
private Long auctionId = null;
@Id
@Basic(optional = false)
@Column(name = "auction_param_id")
@Nullable
private Long auctionParamId = null;
}
@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;
}
}
当我尝试坚持拍卖时(我手动设置了auctionParamId 并期望自动设置auctionId(可能是最后插入的id))
但是我遇到了错误,我不确定为什么查询中的auctionId 在拍卖中是0 而不是最新的id。(我正在使用eclipselink jpa 提供程序)
Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`portaldemo`.`auction_param_values`, CONSTRAINT `auction_param_values_auction_id_fk` FOREIGN KEY (`auction_id`) REFERENCES `auctions` (`auction_id`))
Error Code: 1452
Call: INSERT INTO auction_param_values (auction_param_val, create_ts, last_updt_ts, auction_param_id, auction_id) VALUES (?, ?, ?, ?, ?)
bind => [2011-02-12 04:00:00, 2011-01-27 12:02:00.28, 2011-01-27 12:17:43.25, 2, 0]
Query: InsertObjectQuery(com.eaportal.domain.AuctionParamValue[auctionParamValuePK=com.eaportal.domain.AuctionParamValuePK[auctionId=0, auctionParamId=2]])
这里 [auctionId=0 总是以 0 的形式出现,而不是最后插入的 id :(
这个映射有什么问题?
【问题讨论】:
标签: orm jpa eclipselink