我在使用hibernate从我的java实体中的枚举更新postgres数据库中的枚举字段时遇到问题导致标题中的错误。
我的数据库看起来像这样
list files in folder
这设置了特定列fulfillment_status的类型,即
\dT fulfillment_status
List of data types
Schema | Name | Description
--------+--------------------+-------------
public | fulfillment_status |
(1 row)
java实体类的相关部分
Column | Type | Collation | Nullable | Default
----------------------------+--------------------------------+-----------+----------+--------------------------------------
fulfillment_status | fulfillment_status | | not null |
用于更新班级的代码
@Entity
@Table(name = "payments", uniqueConstraints = { @UniqueConstraint(columnNames = {"id"})})
public class Payment {
private FulfillmentStatus fulfillmentStatus;
public enum FulfillmentStatus {
PENDING, FULFILLED, FAILED, ABORTED
}
@Enumerated(EnumType.STRING)
@Column(name = "fulfillment_status", nullable = false, columnDefinition = "enum('PENDING', 'FULFILLED', 'FAILED', 'ABORTED')")
public FulfillmentStatus getFulfillmentStatus() {
return fulfillmentStatus;
}
public void setFulfillmentStatus(FulfillmentStatus fulfillmentStatus) {
this.fulfillmentStatus = fulfillmentStatus;
}
这会产生错误
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaUpdate<Payment> update =
builder.createCriteriaUpdate(Payment.class);
Root root = update.from(Payment.class);
update.set(root.get("fulfillmentStatus"),
Payment.FulfillmentStatus.FULFILLED);
update.where(builder.equal(root.get("requestID"), UUID.fromString(requestID)));
session.createQuery(update).executeUpdate();
session.getTransaction().commit();
session.close();
这里的问题似乎是如何使用CriteriaUpdate将java枚举类型映射到postgres枚举类型...希望无需创建其他映射类。