EclipseLink JPA Postgres JsonObject到JSONB转换器

时间:2018-07-06 12:33:48

标签: java postgresql jpa eclipselink jsonb

我对EclipseLink和Postgres的“扩展”使用还比较陌生,所以我希望有人可以帮助我。

我有以下型号:

package co.nayo.backend.models;

import co.nayo.backend.models.helpers.JSONConverter;
import co.nayo.backend.models.helpers.UUIDConverter;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.Converter;

import javax.json.JsonObject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.UUID;

@Entity
@Table(name = "subscriptions")
@Converter(name = "uuid_converter", converterClass = UUIDConverter.class)
@Converter(name = "json_converter", converterClass = JSONConverter.class)
public class Subscription {
    @Id
    @Column(name = "subscription_id", updatable = false)
    @Convert("uuid_converter")
    private UUID subscriptionId;

    @Column(name = "title")
    private String title;

    @Column(name = "description")
    private String description;

    @Column(name = "billing_cycle")
    private String billingCycle;

    @Column(name = "amount")
    private double amount;

    @Column(name = "currency")
    private String currency;

    @Column(name = "invoice_title")
    private String invoiceTitle;

    @Column(name = "invoice_description")
    private String invoiceDescription;

    @Column(name = "public")
    private boolean isPublic;

    @Column(name = "permissions")
    @Convert("json_converter")
    private JsonObject permissions;

    @Column(name = "active")
    private boolean active;

    @Column(name = "created_at")
    private Date createdAt;

    // Getters & Setters
}

我需要/想要一个javax.json.JsonObject来映射到permissions postgres数据类型的JSONB字段。 因此,我编写了此Converter来处理转换。

package co.nayo.backend.models.helpers;

import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.DirectCollectionMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;

import javax.json.JsonObject;
import java.sql.Types;

public class JSONConverter implements Converter {
    @Override
    public JsonObject convertObjectValueToDataValue(Object objectValue, Session session) {
        return (JsonObject)objectValue;
    }

    @Override
    public JsonObject convertDataValueToObjectValue(Object dataValue, Session session) {
        return (JsonObject)dataValue;
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public void initialize(DatabaseMapping databaseMapping, Session session) {
        final DatabaseField field;
        if(databaseMapping instanceof DirectCollectionMapping) {
            field = ((DirectCollectionMapping)databaseMapping).getDirectField();
        } else {
            field = databaseMapping.getField();
        }

        field.setSqlType(Types.OTHER);
        field.setTypeName("JsonObject");
        field.setType(JsonObject.class);
        field.setColumnDefinition("jsonb");
    }
}

当我尝试保留新的订阅时,出现以下错误:

  

内部异常:org.postgresql.util.PSQLException:错误:列   “权限”的类型为jsonb,但表达式的类型为hstore
  提示:您将需要重写或强制转换表达式。位置:224   错误代码:0

所以由于某种原因(我还不知道),EclipseLink似乎忽略了JSONB绑定。

当我将field.setColumnDefinition("jsonb");更改为field.setColumnDefinition("hstore");时,一切正常,但是postgres数据类型现在为hstore

任何人都可以朝正确的方向引导我如何使其与JSONB数据类型一起使用吗?

预先感谢和亲切的问候, 鲍勃

1 个答案:

答案 0 :(得分:0)

对我来说,我必须扩展JpaBaseConfiguration才能使转换器正常工作:

@Configuration
public class JpaConfiguration extends JpaBaseConfiguration {
    protected JpaConfiguration(DataSource dataSource, JpaProperties properties,
                               ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider,
                               ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
        super(dataSource, properties, jtaTransactionManagerProvider, transactionManagerCustomizers);
    }

    @Override
    protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
        return new EclipseLinkJpaVendorAdapter();
    }

    @Override
    protected Map<String, Object> getVendorProperties() {
        final HashMap<String, Object> map = new HashMap<>();
        map.put(PersistenceUnitProperties.WEAVING, detectWeavingMode());
        map.put(PersistenceUnitProperties.VALIDATION_MODE, ValidationMode.NONE.toString());
        return map;
    }

    private String detectWeavingMode() {
        return InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? "true" : "static";
    }
}