2016-09-21 11 views
9

私はアプリケーションMySQL 5.7で使用しており、JSON列を持っています。 H2データベースがテーブルを作成できないため、統合テストを実行しようとすると機能しません。これはエラーです:H2でJSON列を解決するには

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; 

    } 
} 
+0

問題を解決しましたか?私も同様のことをやろうとしていて、成功しない – nadavgam

答えて

4

H2は、JSONデータ型を持っていません。

JSONは基本的に非常に長い文字列なので、ほとんどのデータベースで使用できるCLOBを使用できます。

行レベルでJSON型を操作するSQL関数が必要な場合のみ、JSON関数がCLOBではなくJSON型で動作することをデータベースが主張する場合にのみ、行レベルでJSON型が必要です。

2

私はH2でTEXT型を使用して問題を解決しました。 テスト用にH2でスキーマを作成し、JSONタイプをTEXTで置き換えるには、個別のデータベーススクリプトを作成する必要があります。

クエリでJson関数を使用すると、H2を使用している間にそれらをテストすることができないため、これはまだ問題です。

関連する問題