gpt4 book ai didi

java - 虽然有 Composite Primary Key,但 Hibernate 在只有一个键唯一时报错

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:37 24 4
gpt4 key购买 nike

我遇到了一个 Hibernate 错误,它说找到了具有给定标识符的不止一行,我一直坚持下去。如果能提供任何帮助,我将不胜感激。

  • 我想创建一个表作为 orderLine,其中包含特定销售订单的产品代码、数量等。
  • 一个 SalesOrder 可以包含多个 orderLine。

  • orderLine 表的复合键是productCode + OrderNumber。 ProductCode 是Product 表的主键,OrderNumber 是SalesOrder 表的主键。

  • 在一个 SalesOrder 中,特定产品应该只有一个订单行。
  • 复合键正确生成, hibernate 记录了以下 sql 语句。

    Hibernate:创建表 orderLine(orderNumber varchar(255) 不为空,productCode varchar(255) 不为空,status varchar(255) 不为空,数量整数不为空,totalPrice double 不为空,unitPrice double 不为空,主键(orderNumber,productCode))

  • 当 OrderLine 表包含如下数据时,我成功地向 OrderLine 表插入一条新记录,其中包含 OrderNumber ORD001 & ProductCode BIS1003

enter image description here

  • 当我尝试从 OrderLine 中获取记录后,我立即收到以下错误。

    原因:org.hibernate.HibernateException:找到具有给定标识符的多行:BIS1003,类:com.salesOrder_ws.entity.OrderLine

  • 既然有一个组合键作为主键,为什么当组合键中只有一个键不唯一时,hibernate 会抛出异常?

代码如下。

订单行实体:

  @Entity
@Table(name = "orderLine")
public class OrderLine implements Serializable{

private static final long serialVersionUID = -851110991599534263L;


@AttributeOverrides(value =
{@AttributeOverride(column = @Column(name="productCode"), name = "productCode"),
@AttributeOverride(column = @Column(name="orderNumber"), name = "orderNumber")})
@EmbeddedId
private LineID pk;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "productCode", insertable = false, updatable = false)
private Product product;

private int quantity;

private double unitPrice;

private double totalPrice;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "orderNumber", nullable=false, insertable=false, updatable=false)
private SalesOrder salesOrder;


@Override
public boolean equals(Object obj) {

try {
LineID line = (LineID) obj;
return (this.getSalesOrder().getOrderNumber()
.equals(line.getOrderNumber()) && this.getProduct()
.getCode().equals(line.getProductCode()));
} catch (Exception e) {
return false;
}
}

@Override
public int hashCode() {
return (this.getProduct().getCode() + "" + this.getProduct().getCode()).hashCode();
}
}

销售订单实体

@Entity
@Table(name = "salesOrder")
public class SalesOrder extends BaseEntity{

@Id
private String orderNumber;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "customerCode", nullable = false)
private Customer customer;

private double totalPrice;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "salesOrder", cascade = CascadeType.ALL)
private List<OrderLine> lines;


@Override
public boolean equals(Object obj) {

try {
SalesOrder so = (SalesOrder) obj;
if (this.getOrderNumber().equals(so.getOrderNumber())) {
return true;
}
} catch (Exception e) {
return false;
}
return false;
}

@Override
public int hashCode() {
return this.getOrderNumber().hashCode();
}
}

可嵌入类

@Embeddable
public class LineID implements Serializable{

private static final long serialVersionUID = -4478828739881744452L;
@Basic(optional = false)
private String productCode;
@Basic(optional = false)
private String orderNumber;

@Override
public boolean equals(Object obj) {
try {
LineID l = (LineID) obj;
return this.productCode.equals(l.getProductCode()) && this.orderNumber.equals(l.getOrderNumber());
} catch (Exception e) {
return false;
}
}

@Override
public int hashCode() {
return (this.getOrderNumber() + "" + this.getProductCode()).hashCode();
}
}

更新

Hibernate 生成的 SQL:

Hibernate: create table customer (code varchar(255) not null, status varchar(255) not null, address varchar(255), creditLimit double precision not null, currentCredit double precision not null, name varchar(255), phone1 varchar(255), phone2 varchar(255), primary key (code))
Hibernate: create table orderLine (orderNumber varchar(255), productCode varchar(255), status varchar(255) not null, quantity integer not null, totalPrice double precision not null, unitPrice double precision not null, primary key (orderNumber, productCode))
Hibernate: create table product (code varchar(255) not null, status varchar(255) not null, description varchar(255), price double precision not null, quantity integer not null, primary key (code))
Hibernate: create table salesOrder (orderNumber varchar(255) not null, status varchar(255) not null, totalPrice double precision not null, customerCode varchar(255) not null, primary key (orderNumber))
Hibernate: alter table orderLine add constraint UK_9gf3j9l0n1w7d2h4sso3voc77 unique (productCode)
Hibernate: alter table orderLine add index FK_9gf3j9l0n1w7d2h4sso3voc77 (productCode), add constraint FK_9gf3j9l0n1w7d2h4sso3voc77 foreign key (productCode) references product (code)
Hibernate: alter table orderLine add index FK_ojvge4lucwf2gtihxtmnav3u2 (orderNumber), add constraint FK_ojvge4lucwf2gtihxtmnav3u2 foreign key (orderNumber) references salesOrder (orderNumber)
Hibernate: alter table salesOrder add index FK_4lq8ynumala22y9t17ceawo81 (customerCode), add constraint FK_4lq8ynumala22y9t17ceawo81 foreign key (customerCode) references customer (code)

Hibernate:修改表 orderLine 添加约束 UK_9gf3j9l0n1w7d2h4sso3voc77 unique (productCode)

上面的SQL不是用来生成的。如果我能避免这个独特的约束,问题就解决了。

感谢解决此问题的任何帮助。

最佳答案

我想你可能错过了 @MapsId注释:

@Entity
@Table(name = "orderLine")
public class OrderLine implements Serializable{

private static final long serialVersionUID = -851110991599534263L;

@AttributeOverrides(value =
{@AttributeOverride(column = @Column(name="productCode"), name = "productCode"),
@AttributeOverride(column = @Column(name="orderNumber"), name = "orderNumber")})
@EmbeddedId
private LineID pk;

@ManyToOne(cascade = CascadeType.ALL)
@MapsId("productCode")
private Product product;

private int quantity;

private double unitPrice;

private double totalPrice;

@ManyToOne(fetch = FetchType.EAGER)
@MapsId("orderNumber")
private SalesOrder salesOrder;


@Override
public boolean equals(Object obj) {

try {
LineID line = (LineID) obj;
return (this.getSalesOrder().getOrderNumber()
.equals(line.getOrderNumber()) && this.getProduct()
.getCode().equals(line.getProductCode()));
} catch (Exception e) {
return false;
}
}

@Override
public int hashCode() {
return (this.getProduct().getCode() + "" + this.getProduct().getCode()).hashCode();
}
}

关于java - 虽然有 Composite Primary Key,但 Hibernate 在只有一个键唯一时报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870549/

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