将Eclipselink实体移动到单独的jar中

时间:2015-11-30 21:07:37

标签: java postgresql java-ee jpa eclipselink

我有一个在JBoss上托管的工作Java EE应用程序。它使用EclipseLink来管理Postgres数据库中的数据。我现在将实体类移动到一个单独的jar中,以便它们可以被其他组件共享。执行此操作后,Postgres发出以下错误:

ERROR:  operator does not exist: uuid = bytea at character 209
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

看起来我的转换器类没有被调用。这是我的转换器类的样子:

package model.util;

import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;

import java.sql.Types;
import java.util.UUID;

public class PgUuidConverter
        implements Converter
{
    @Override
    public boolean isMutable ()
    {
        return false;
    }
    @Override
    public Object convertDataValueToObjectValue (Object value, Session session)
    {
        return (UUID) value;
    }
    @Override
    public Object convertObjectValueToDataValue (Object value, Session session)
    {
        return (UUID) value;
    }
    @Override
    public void initialize (DatabaseMapping mapping, Session session)
    {
        mapping.getField ().setSqlType (Types.OTHER);
    }
}

以下是我在我的实体中使用它的方式:

package model;

import model.util.PgUuidConverter;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.Converter;

import java.util.UUID;
import javax.persistence.*;

@Entity
@Table (
    name           = "item",
    schema         = "main"
)
@Converter (
    name           = "uuid",
    converterClass = PgUuidConverter.class
)
public class Item
{
    public Item () {}

    @Id
    @Column (
        name     = "item_id",
        unique   = true,
        nullable = false
    )
    private UUID      itemId;
    @Column (name = "layer_id")
    @Convert ("uuid")
    private UUID      layerId;

    public UUID getItemId ()
    {
        return this.itemId;
    }

    public UUID getLayerId ()
    {
        return this.layerId;
    }

    public void setItemId (UUID itemId)
    {
        this.itemId = itemId;
    }

    public void setLayerId (UUID layerId)
    {
        this.layerId = layerId;
    }
}

我缺少某种配置吗?

1 个答案:

答案 0 :(得分:0)

看起来问题是现在显而易见的问题。我需要将转换注释应用于所有UUID字段。

之前我只将Convert annotation应用于可以为空的UUID字段,因为当UUID字段设置为NULL时,Postgres只给出了类型错误。现在,Postgres为所有没有转换注释的UUID字段抛出异常。