英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论