我有一个问题更新枚举字段在我的postgres数据库从枚举在我的java实体使用hibernate导致标题中的错误。
我的数据库看起来像这样
\dT fulfillment_status
List of data types
Schema | Name | Description
--------+--------------------+-------------
public | fulfillment_status |
(1 row)
这将设置特定列fulfillment_status i的类型。e.
Column | Type | Collation | Nullable | Default
----------------------------+--------------------------------+-----------+----------+--------------------------------------
fulfillment_status | fulfillment_status | | not null |
java实体类的相关部分
@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();
这将生成错误
Hibernate:
update
payments
set
fulfillment_status=?
2018-05-02 08:54:11 WARN SqlExceptionHelper:129 - SQL Error: 0, SQLState: 42804
2018-05-02 08:54:11 ERROR SqlExceptionHelper:131 - ERROR: column "fulfillment_status" is of type fulfillment_status but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 40
这里的问题似乎是如何使用CriteriaUpdate将java枚举类型Map到postgres枚举类型。..希望不会创建额外的Map类。
1条答案
按热度按时间wfsdck301#
这解决了我的问题: