gpt4 book ai didi

java - 持久化对象时出现 NonUniqueObjectException

转载 作者:行者123 更新时间:2023-12-01 18:57:39 25 4
gpt4 key购买 nike

大家好,我在尝试将对象数组写入数据库时​​遇到了 Hibernate 问题。重要的是我有一个从 Web 服务查询构建的对象。这个对象“响应”最多可以包含十个“未付费项目”,当我尝试保留这些项目时,就会出现问题。

实体:

@Entity
@Table(name="TABLE_NAME")
public class AccountDetailsRROutput implements Serializable {
private static final long serialVersionUID = 1L;

@Id

private String payeename;

private String typeunpd;

private BigDecimal unpdamt;

@Column(name="TRANSACTION_ID")
private long transactionId;

public AccountDetailsRROutput() {
super();
}

// plus all get/sets
}



//================================================================
// Populate the output for the repeating rows table
// which can contain a maximum of 10 unpaid items
//===============================================================
AccountDetailsRROutput outputRRTable[] = new AccountDetailsRROutput[response.getLineItems().length];

LOGGER.debug(METHOD_NAME, "Loop through the line items");
for (int i = 0; i < response.getLineItems().length; i++) {

//================================================================
// Ensure that we have an item so we don't write an empty row
//================================================================
if (response.getLineItems()[i].getTypeunpd() == null || response.getLineItems()[i].getTypeunpd() == "") {
LOGGER.debug(METHOD_NAME, "No unpaid item entry so break out of the the loop");
break;
}
else {
LOGGER.debug(METHOD_NAME, "We've got an unpaid item so add the details to the DB");

outputRRTable[i] = new AccountDetailsRROutput();

outputRRTable[i].setTransactionId(iTransactionID);
outputRRTable[i].setTypeunpd(response.getLineItems()[i].getTypeunpd());
outputRRTable[i].setPayeename(response.getLineItems()[i].getPayeeName());
outputRRTable[i].setUnpdAmt(response.getLineItems()[i].getUnpdAmt());

//================================================================
// Persist the output list DB object
//================================================================
LOGGER.debug(METHOD_NAME, "Persist repeating rows table DB object for line item: " + (i+1));
em_i.persist(outputRRTable[i]);
}
}
LOGGER.debug(METHOD_NAME, "Finished persisting repeating rows table DB object");
em_i.flush();

当我尝试此操作时,出现以下错误:

org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与 session 关联:

我可以通过将 emi.persist 更改为 emi.merge 来解决这个问题,但它只是将一个元素写入数据库。该表中可以有重复记录,不存在pk。

最佳答案

您可能有不止一件商品具有相同的收款人姓名。尝试定义另一个 id(例如由 payeenametransactionID 组成的 id)。

关于java - 持久化对象时出现 NonUniqueObjectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13378268/

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