在OpenJPA中使用inet postgres数据类型(Using inet postgres datatype with OpenJPA)

编程入门 行业动态 更新时间:2024-10-25 06:31:59
在OpenJPA中使用inet postgres数据类型(Using inet postgres datatype with OpenJPA)

我的应用程序使用OpenJPA连接Postgres数据库。 在模式中,我在列中使用inet postgres数据类型。 Java中的这个字段是一个字符串。 我能够正确读取该字段,但是在插入新行时遇到问题。

在互联网上搜索我发现了三种可能的解决方案:

创建本机查询。 此方法有效,但在我的特定情况下,为了插入此行而创建Native查询意味着创建更多由OpenJPA管理的查询,这可能导致许多错误。 因此,在这种情况下,它不是更合适的解决方案。

在这个问题中创建一个PostgresDictionary: 如何在OpenJPA中使用Postgres inet数据类型? 。 我已经实现了这个用户解释的具体方法。 我创建了自定义的PostgresDictionary,我在@Column注释中添加了columnDefinition,并在persistence.xml中添加了该属性。 但我的自定义PostgresDictionary永远不会被调用。 当应用程序创建PostgresDictionary时,不断创建org.apache.openjpa.jdbc.sql.PostgresDictionary而不是自定义的。

实施自定义策略,例如http://webspherepersistence.blogspot.co.at/2009/04/custom-orm-with-openjpa.html 。 但是为了实现策略,我必须从类java.sql.Types( http://docs.oracle.com/javase/6/docs/api/java/sql/Types)中设置列的类型。 html?is-external = true )并且此列中没有inet类型。 我尝试了Types.OTHER,但我仍然有相同的错误,表明该列是一个类型inet,我试图插入的值是varchar(String)。

那么,有没有人知道如何解决我对映射的问题?

My application is using OpenJPA to connect with a Postgres database. In the schema I am using the inet postgres datatype in a column. This field in Java is a String. I am able to read the field correctly, but I am having problems inserting a new row.

Searching on the Internet I have found three possible solutions to do this:

Creating a Native query. This method works, but in my specific case creating a Native query in order to insert this row implies creating more queries that were being managed by OpenJPA which can lead to lots of bugs. So it is not the more suitable solution in this case.

Creating a PostgresDictionary like in this question: How to use Postgres inet data type with OpenJPA?. I have implemented this exactly how this user explains. I have created the custom PostgresDictionary, I have added the columnDefinition in the @Column annotation and I have added the property in the persistence.xml. But my custom PostgresDictionary is never called. When the application created the PostgresDictionary keeps creating the org.apache.openjpa.jdbc.sql.PostgresDictionary instead of the custom one.

Implementing a custom Strategy, like this example http://webspherepersistence.blogspot.co.at/2009/04/custom-orm-with-openjpa.html. But in order to implement the Strategy, I have to set the type of the column from the class java.sql.Types (http://docs.oracle.com/javase/6/docs/api/java/sql/Types.html?is-external=true) and there is no inet type in this column. I tried Types.OTHER, but I still have the same error indicating that the column is a type inet and the value I am trying to insert is varchar (String).

So, does anybody has an idea how to fix the problem I am having with the mapping?

最满意答案

第2点的解决方案无法正常工作,因为openjpa.jdbc.DBDictionary被类org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter的类覆盖了,该类的数据库属性设置为POSTGRESQL。 这显然将DBDictionary设置为org.apache.openjpa.jdbc.sql.PostgresDictionary,与persistence.xml属性中设置的值无关。

从OpenJpaVendorAdapter删除此数据库属性允许我使用我的自定义PostgresDictionary。

The solution in the point 2 was not working because the openjpa.jdbc.DBDictionary was been overiden by the class org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter that had the database property set to POSTGRESQL. Which aparently set the DBDictionary to org.apache.openjpa.jdbc.sql.PostgresDictionary independently of the value set in the persistence.xml property.

Deleting this database property from the OpenJpaVendorAdapter allowed me to use my custom PostgresDictionary.

更多推荐

本文发布于:2023-08-06 16:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1452875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   inet   OpenJPA   datatype   postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!