在EclipseLink和PostgreSQL中使用UUID [英] Using UUID with EclipseLink and PostgreSQL

查看:144
本文介绍了在EclipseLink和PostgreSQL中使用UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PostgreSQL uuid类型作为对象的主键. 为此,我创建了一个转换器(实现Converter接口). 波纹管是相关代码:

I want to use the PostgreSQL uuid type for objects' primary keys. For that I've created a converter (implementing the Converter interface). Bellow is the relevant code:

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

        field.setSqlType(Types.OTHER);
        field.setTypeName("uuid");
        field.setColumnDefinition("UUID");
    }

然后,我用波纹管注释为相关实体X注释:

Then I've annotated the relevant entity X with the bellow annotations:

@Converter(name="uuidConverter",converterCalss=UUIDConverter.class)
@Convert("uuidConverter")
@Id
public UUID getId()
{
    return id;
}

问题是我有另一个具有以下定义的类(Y):

The problem is that I have another class (Y) which has the following definition:

@ManyToOne(targetEntity = X.class)
@JoinColumn(name = "x_id")
public X getX();

尽管EclipseLink可以按预期创建表,但在尝试插入类型为Y的对象时,它会向数据库发送一个字符串. Postgres返回以下错误消息:

Although EclipseLink created the tables as expected it sends a string to the database when trying to insert objects of type Y. Postgres returns the following error message:

column "id" is of type uuid but expression is of type character varying at character

任何解决方案/解决方法将不胜感激.

Any solutions / work around will be appreciated.

推荐答案

我在使用EclipseLink JPA + Postgresql + UUID作为主键时遇到了相同的问题.

I had the same issue with EclipseLink JPA + Postgresql + UUID as primary key.

为了解决这个问题,我合并了Github和下面的链接中的代码: https://forums.oracle.com/forums/thread.jspa?messageID=4584157

To solve it, I've merged codes from Github and below link: https://forums.oracle.com/forums/thread.jspa?messageID=4584157

下面的UUIDConverter代码对我有用,尽管代码肯定不是最好的.

The below code for UUIDConverter worked for me, though the code surely isn't the best.

public void initialize(DatabaseMapping ARGMapping, Session ARGSession)
{
    final DatabaseField Field;
    if (ARGMapping instanceof DirectCollectionMapping)
    {
        Field = ((DirectCollectionMapping) ARGMapping).getDirectField();
    }
    else
    {
        Field = ARGMapping.getField();
    }
    Field.setSqlType(Types.OTHER);
    Field.setTypeName("uuid");
    Field.setColumnDefinition("UUID");

    for (DatabaseMapping m : ARGMapping.getDescriptor().getMappings())
    {
        assert OneToOneMapping.class.isAssignableFrom(ManyToOneMapping.class);
        if (m instanceof OneToOneMapping)
        {
            for (DatabaseField field : ((OneToOneMapping) m).getForeignKeyFields())
            {
                field.setSqlType(Types.OTHER);
                field.setColumnDefinition("UUID");
                field.setTypeName("uuid");
            }
        }
    }
}

这篇关于在EclipseLink和PostgreSQL中使用UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆