Spring Boot 3 在更新枚举数据类型时出现 org.hibernate.query.SemanticException 错误。

huangapple go评论46阅读模式
英文:

Spring boot 3 org.hibernate.query.SemanticException when updating an enum data type

问题

以下是您要翻译的内容:

Repository method:

@Transactional
@Modifying
@Query("UPDATE MyEntityClass e SET e.status = 'ENABLED'")
int setEnabledStatus();

When trying to start the application i got the following exception:

... Caused by: org.hibernate.query.SemanticException: The assignment exppression type [java.lang.String] did not match the assignment path type [...MyStatusEnum] for the path [e.status] [UPDATE MyEntityClass e SET e.status = 'ENABLED']

Has anyone experienced the same issue?
I'm using spring boot 3.0.2

英文:

With the following code,

Entity:

@Column
@Enumerated(value = EnumType.STRING)
private MyStatusEnum status;

public enum MyStatusEnum {
  ENABLED,
  DISABLED
}

Repository method:

@Transactional
@Modifying
@Query("UPDATE MyEntityClass e SET e.status = 'ENABLED'")
int setEnabledStatus();

When trying to start the application i got the following exception:

... Caused by: org.hibernate.query.SemanticException: The assignment exppression type [java.lang.String] did not match the assignment path type [...MyStatusEnum] for the path [e.status] [UPDATE MyEntityClass e SET e.status = 'ENABLED']

Has anyone experienced the same issue?
I'm using spring boot 3.0.2

答案1

得分: 3

你必须使用枚举值而不是字符串:

@Transactional
@Modifying
@Query("UPDATE MyEntityClass e SET e.status = MyStatusEnum.ENABLED")
int setEnabledStatus();

因为 JPA 使用对象。

英文:

You have to use the enum value not a string:

@Transactional
@Modifying
@Query("UPDATE MyEntityClass e SET e.status = MyStatusEnum.ENABLED")
int setEnabledStatus();

because JPA works with Objects

huangapple
  • 本文由 发表于 2023年2月19日 19:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定