JDO + PostgreSQL 数组
我已经实现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要构建自定义字段策略来处理映射。
然后关键是将本例中的表示形式转换为 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}
. SoARRAY[1,2,3]::int[]
becomes'{1,2,3}'
or'{"1","2","3"}'