使用JPA,自动增量ID不会反映在组合键中 [英] autoincrement id is not reflecting in composite key using JPA

查看:113
本文介绍了使用JPA,自动增量ID不会反映在组合键中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的映射

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

When I try to persist auction (I am manually setting the auctionParamId and expecting the auctionId to be automaticlly set (may be the last inserted id) )

但是我遇到了错误,我不确定为什么查询中的auctionId是0而不是拍卖中的最新ID.(我使用的是eclipselink jpa provider)

but I am getting below error, I am not sure why the auctionId in the query is going as 0 instead of latest id in the auction.(I am using eclipselink jpa provider)

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:(

Here the [auctionId=0 is always comming as 0 and not the last inserted id :(

此映射有什么问题?

推荐答案

如果您在其他类中引用了负责设置ID的其他属性,则@GeneratedValue将仅设置对其进行注释的属性的值这些.

An @GeneratedValue will only set the value of the attribute it is annotated on, if you have other attributes in other classes that reference the id you are responsible for setting these.

即您将需要首先持久保存并刷新Auction,然后使用其生成ID创建AuctionParamValue.

i.e. you would need to first persist and flush the Auction, and then create the AuctionParamValue using its generate Id.

或者,如果您使用了TABLE或SEQUENCE id生成,则只需要调用persist,而不是flush.通常,我永远不建议使用IDENTITY排序,因为它的值无法预先分配.

Or, if you used TABLE or SEQUENCE id generation then you would just need to call persist, and not the flush. In general I would never recommend IDENTITY sequencing as its values cannot be preallocated.

但是实际上,您不应该拥有所有重复的字段.完全删除@EmbeddedId auctionParamValuePK,只需将@Id添加到两个@ManyToOnes中,然后使用@IdClass即可.即使使用IDENTITY id生成,这也将使事情变得更加简单并且可以正常工作.

But really you should not have the duplicate fields as all. Remove the @EmbeddedId auctionParamValuePK entirely and just add @Id to the two @ManyToOnes, and use an @IdClass instead. This will make things much simplier and will just work, even with IDENTITY id generation.

您还可以替代地删除两个@ManyToOne映射上的insertable/updateable = false,然后将其置于@EmbeddedId属性中,这将从关系中写入外键,但您的对象仍会在内存中损坏

You could also instead remove the insertable/updateable=false on the two @ManyToOne mappings and instead put them on the @EmbeddedId attributes, this will have the foreign key written from the relationships, but your object will still be corrupt in memory.

看, http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing_ManyToOne_Ones_Primary_Tos

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

这篇关于使用JPA,自动增量ID不会反映在组合键中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆