gpt4 book ai didi

orm - 自动增量 ID 未反射(reflect)在使用 JPA 的复合键中

转载 作者:行者123 更新时间:2023-12-04 06:36:30 24 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 @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;
}

}

当我尝试坚持拍卖时(我手动设置了uctionParamId并期望自动设置auctionId(可能是最后插入的id))

但我遇到了以下错误,我不确定为什么查询中的拍卖 ID 是 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 :(

这个映射有什么问题?

最佳答案

@GeneratedValue 只会设置它所注释的属性的值,如果其他类中有其他属性引用了您负责设置这些 id 的 id。

即,您需要首先持久化并刷新 Auction,然后使用其生成 Id 创建 AuctionParamValue。

或者,如果您使用 TABLE 或 SEQUENCE id 生成,那么您只需要调用persist,而不是flush。一般来说,我永远不会推荐 IDENTITY 排序,因为它的值不能预先分配。

但实际上你不应该有重复的字段。完全删除@EmbeddedIductionParamValuePK,只需将@Id 添加到两个@ManyToOnes,并使用@IdClass 代替。这将使事情变得更简单,并且即使使用 IDENTITY id 生成也能正常工作。

您也可以删除两个 @ManyToOne 映射上的 insertable/updateable=false 并将它们放在 @EmbeddedId 属性上,这将从关系中写入外键,但您的对象仍会在内存中损坏。

看,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships

关于orm - 自动增量 ID 未反射(reflect)在使用 JPA 的复合键中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4813755/

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