java - 在 hibernate 中更新枚举值

标签 java hibernate enums

我编写了一个 hql 查询,我想在其中更新数据库中的枚举值,但我不知道如何更新枚举值。这是我的模型

@Entity
public class ImageInfo implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;

@Column(name = "ACTIVE")
private Integer active;

@Column(name = "NOTIFICATION_NUM")
private Integer groupNum;

@Column(name = "PUBLISHED_TIME")
@Temporal(TemporalType.TIMESTAMP)
private Date publishedTime;

@Column(name = "POSTED_TIME")
private Timestamp postedTime;

@Enumerated(EnumType.STRING)
private PublishIndicator publishIndicator;

}

您可以看到有一个名为“publishIndicator”的枚举字段。我想在 hql 查询中更新此字段。这是我的查询

session.createQuery("update ImageInfo img set img.active = 1 
and img.publishIndicator = :publish where   img.active = 0  and  
img.publishedTime <:publishedTime ").setParameter("publishedTime", 
date).setParameter("publish", PublishIndicator.PUBLISH).executeUpdate();

但我收到错误

“java.lang.IllegalArgumentException:要遍历的节点不能为空!”

我知道我在更新枚举值时做错了。我该如何解决它?

最佳答案

使用您的(完全限定的)PublishIndicator 枚举。 而不是 PublishIndicator.PUBLISH 值,所以像这样:(假设您的 PublishIndicator 枚举位于 com.image 包中),将其用作 com.image.PublishIndicator.PUBLISH

关于java - 在 hibernate 中更新枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43285282/

相关文章:

c# - 如果我需要一个 bool 来将选项也设置为 null,那么我最好使用可空 bool 还是自定义枚举?

VB.Net Enum ToString 返回未知数字

java - 一些 Tomcat webapps 打不开

java - public 修饰符对 Java Thread 的 run() 的好处

java - 使用 CXF 和 Webphere 8.5 的 JAX WS(org.apache.axis2.jaxws.client.proxy.JAXWSProxyHandler 与 org.apache.cxf.frontend.ClientProxy 不兼容)

java - 组织.hibernate.LazyInitializationException : failed to lazily initialize a collection of role: no session or session was closed

java - hibernatetemplate 清除ehcache?

java - 确保经过身份验证的用户只能更新自己的记录

c++ - 如何转换枚举?

java - 将 Java Swing 应用程序转换为 Eclipse 插件