我编写了一个 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/