gpt4 book ai didi

java - 如何使用 H2、JPA 和 Hibernate 映射 JSON 列

转载 作者:IT老高 更新时间:2023-10-28 13:50:02 26 4
gpt4 key购买 nike

我在应用程序 MySQL 5.7 中使用并且我有 JSON 列。当我尝试运行我的集成测试时,因为 H2 数据库无法创建表,所以它不起作用。这是错误:

2016-09-21 16:35:29.729 ERROR 10981 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: create table payment_transaction (id bigint generated by default as identity, creation_date timestamp not null, payload json, period integer, public_id varchar(255) not null, state varchar(255) not null, subscription_id_zuora varchar(255), type varchar(255) not null, user_id bigint not null, primary key (id))
2016-09-21 16:35:29.730 ERROR 10981 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : Unknown data type: "JSON"; SQL statement:

这是实体类。

@Table(name = "payment_transaction")
public class PaymentTransaction extends DomainObject implements Serializable {

@Convert(converter = JpaPayloadConverter.class)
@Column(name = "payload", insertable = true, updatable = true, nullable = true, columnDefinition = "json")
private Payload payload;

public Payload getPayload() {
return payload;
}

public void setPayload(Payload payload) {
this.payload = payload;
}
}

还有子类:

public class Payload implements Serializable {

private Long userId;
private SubscriptionType type;
private String paymentId;
private List<String> ratePlanId;
private Integer period;

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public SubscriptionType getType() {
return type;
}

public void setType(SubscriptionType type) {
this.type = type;
}

public String getPaymentId() {
return paymentId;
}

public void setPaymentId(String paymentId) {
this.paymentId = paymentId;
}

public List<String> getRatePlanId() {
return ratePlanId;
}

public void setRatePlanId(List<String> ratePlanId) {
this.ratePlanId = ratePlanId;
}

public Integer getPeriod() {
return period;
}

public void setPeriod(Integer period) {
this.period = period;
}

}

这个转换器用于插入数据库:

public class JpaPayloadConverter implements AttributeConverter<Payload, String> {

// ObjectMapper is thread safe
private final static ObjectMapper objectMapper = new ObjectMapper();

private Logger log = LoggerFactory.getLogger(getClass());

@Override
public String convertToDatabaseColumn(Payload attribute) {
String jsonString = "";
try {
log.debug("Start convertToDatabaseColumn");

// convert list of POJO to json
jsonString = objectMapper.writeValueAsString(attribute);
log.debug("convertToDatabaseColumn" + jsonString);

} catch (JsonProcessingException ex) {
log.error(ex.getMessage());
}
return jsonString;
}

@Override
public Payload convertToEntityAttribute(String dbData) {

Payload payload = new Payload();
try {
log.debug("Start convertToEntityAttribute");

// convert json to list of POJO
payload = objectMapper.readValue(dbData, Payload.class);
log.debug("JsonDocumentsConverter.convertToDatabaseColumn" + payload);

} catch (IOException ex) {
log.error(ex.getMessage());
}
return payload;

}
}

最佳答案

我刚刚在使用 JSONB 列类型时遇到了这个问题 - JSON 类型的二进制版本,它不映射到 TEXT.

为了以后引用,你可以在H2中使用CREATE DOMAIN定义一个自定义类型,如下:

CREATE domain IF NOT EXISTS jsonb AS other;

这似乎对我有用,让我能够成功地针对实体测试我的代码。

来源:https://objectpartners.com/2015/05/26/grails-postgresql-9-4-and-jsonb/

关于java - 如何使用 H2、JPA 和 Hibernate 映射 JSON 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39620317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com