gpt4 book ai didi

java - hibernate 异常: identifier of an instance of X was altered when trying to merge object

转载 作者:行者123 更新时间:2023-12-01 09:55:05 24 4
gpt4 key购买 nike

我正在编写一个 Spring 应用程序。在我的应用程序中,我有一个用于更新 Shop 对象的 REST API。 Shop 对象具有对默认货币对象的引用。我的其余客户端发送带有以下 JSON 有效负载的 post 请求。

{ 
currency: {id: "EUR", name: "Euro", symbol: "€"},
description : "Bla bla bla"
id : "SHOP-00006"
isActive : null
location : "the moon"
name : "Delicious Foo"
numberOfProducts : 0
profilePicture : null
}

我的服务如下所示:

@Transactional
public List<ShopData> updateShop(User user, ShopData shopData) {
Shop shop = shopRepository.getById(shopData.getId());
mapper.map(shopData, shop);
entityManager.merge(shop);
return getUserShops(user);
}

其中 ShopData 是保存从用户接收的数据的 DTO。并且mapper.map将DTO复制到域对象。

此方法抛出以下异常:

org.hibernate.HibernateException: identifier of an instance of com.companyname.application.domain.common.Currency was altered from USD to EUR at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:80) at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:192) at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:152) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102) at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:61) at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1227) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1293) at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573) at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)

最后相关的域类是:

@Entity
@Table(name = "shop")
public class Shop extends AnEntity {

@Column(name = "name", nullable = false)
protected String name;

@Column(name = "description", nullable = false)
protected String description;

@Column(name = "location")
protected String location;

@JoinColumn(name = "owner_id", nullable = false)
@ManyToOne(fetch = FetchType.LAZY)
protected User owner;

@JoinColumn(name = "currency_id")
@ManyToOne(fetch = FetchType.EAGER)
protected Currency currency;

@Column(name = "profile_picture")
protected String profilePicture;

@Column(name = "is_active")
protected Boolean isActive;
// getters and setters
}

还有

@Entity
@Table(name = "currency")
public class Currency extends AnEntity {

@Column(name = "name", length = 124)
protected String name;

@Column(name = "symbol", length = 3)
protected String symbol;
// setters and getters
}

似乎 hibernate 正在尝试合并我的货币对象,即使我没有在关系上指定任何级联值。为什么 hibernate 尝试这样做,而不是将货币引用更改为新的货币引用?如果我在代码中犯了错误,该怎么办?

(编辑)

我的表结构如下:

currency shop

最佳答案

看起来您正在更改实体的ID,但您不应该这样做。如果您使用托管实体引用货币实例,则应该替换整个实体。 Hibernate 无法猜测为什么对象的 ID 属性发生了变化,我认为这是不允许的。

这里我看到两个选项:

  • 在映射器中重新加载您在 shop 实例中设置的 currency 实体,并使用 setter 而不是从 JSON 映射来重新设置它
  • 使用不同类型的货币引用,例如只有 ID 字段而不是实际的对象引用

如果您不希望保留 Currency,请在声明中添加 CascadeType:

@JoinColumn(name = "currency_id")
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
protected Currency currency;

关于java - hibernate 异常: identifier of an instance of X was altered when trying to merge object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37300780/

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