JDO + PostgreSQL 数组

发布于 2024-12-04 02:36:38 字数 673 浏览 0 评论 0原文

我已经实现了 store_mapping 扩展,但它当前使用 ObjectAsStringMapping。因此,我可以从数据库读取数组值,但任何插入或更新都会导致底层 postgresql 驱动程序错误“INTEGER[]”不是“VARCHAR”。

有没有办法在JDO中实现PGSQL数组?它看起来非常灵活,具有所有扩展点。任何有关我必须实施的扩展点的提示都将受到赞赏,提前致谢!

编辑:

在我发现我可以接受 63 个可能的值之后,我使用 postgres int8 作为位字段作为数组的“替换”。

示例类是:

@PersistenceCapable(detachable="true", table="campaigns")
public class Campaign  implements Serializable {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Long id;

    public List<Integer> regions;
}

我想我必须实现一些从 List 到 java.sql.Array 的映射,但仍然不知道如何做到这一点。我可以编写扩展并覆盖默认行为,但它应该是什么扩展点?

I've implemented store_mapping extension but it currently uses ObjectAsStringMapping. As a result I can read array values from database but any insert or update causes underlying postgresql driver error "INTEGER[]" is not "VARCHAR".

Is there any way to implement PGSQL arrays in JDO? It looks quite flexible with all that extension points. Any hints on extension points I have to implement are appreciated, thanks in advance!

Edit:

I'm using postgres int8 as a bit field as a "replacement" for arrays after I figured out that I'll be okay with 63 possible values.

Sample class would be:

@PersistenceCapable(detachable="true", table="campaigns")
public class Campaign  implements Serializable {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Long id;

    public List<Integer> regions;
}

And I think I have to implement some mapping from List to java.sql.Array but still didn't figure out how to do that. I could write extension and override default behavior but what extension-point should it be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2024-12-11 02:36:38

看起来您需要构建自定义字段策略来处理映射。

然后关键是将本例中的表示形式转换为 PostgreSQL 数组表示形式,即逗号分隔值(用 " 转义带有任何特殊字符的文本,但可以用于所有值,双引号通过以下方式转义)然后将字符串括在 {} 之间,因此 ARRAY[1,2,3]::int[] 变为。 '{1,2,3}''{"1","2","3"}'

Looks like you need to build a custom field strategy to handle the mapping.

The key then is to transform the representation in this case to PostgreSQL array representation, namely a comma separated value (with " escaping text with any special characters but can be used on all values, double quotes are escaped by doubling them). The string is then bracketed betweed { and }. So ARRAY[1,2,3]::int[] becomes '{1,2,3}' or '{"1","2","3"}'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文