【问题标题】:JPA mapping issue with composite key复合键的 JPA 映射问题
【发布时间】:2011-06-15 06:05:03
【问题描述】:

我有一个下面的映射

@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

【问题讨论】:

  • auctionauctionParammappedBy 引用的属性在哪里?
  • 他们没有映射。我应该如何映射它们?

标签: jpa nhibernate-mapping mapping eclipselink jpa-2.0


【解决方案1】:

对于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 节中找到一整套派生身份的映射示例(即其中包含外键的复合键)。

【讨论】:

  • 感谢您的参考链接。我现在正在浏览它。我试过@MapsId,但它给了我同样的错误:(
  • Sourabh: 等等,auctionParamIdnull,因为你没有给它赋值,你还期待什么其他行为。另请注意,您不能将@GeneratedValue 与派生身份一起使用。
  • 我知道了,我应该自己填充auctionParamId 但是auctionId 呢,​​当我坚持“拍卖”时,我应该得到最后插入的拍卖的auctionId 吗?
  • now error Can't add or update a child row: a foreign key constraint failed (portaldemo.auction_param_values, CONSTRAINT auction_param_values_auction_id_fk FOREIGN KEY (auction_id) REFERENCES auctions ( auction_id)) 插入到拍卖参数值 (auction_param_val、create_ts、last_updt_ts、auction_param_id、auction_id) 值 (?, ?, ?, ?, ?) bind => [2011-01-12 04:00:00, 2011-01- 25 22:30:52.493, 2011-01-25 22:30:52.493, 1, 0] 查询:InsertObjectQuery(com.eaportal.domain.AuctionParamValue[auctionParamValuePK=com.eaportal.domain.AuctionParamValuePK[auctionId=0,auctionParamId= 1])
  • 当我坚持拍卖命令对象时,我不确定如何让auction_id 成为上述查询中最后插入的id :(
猜你喜欢
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 2019-04-22
  • 2011-03-20
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 2020-10-04
相关资源
最近更新 更多