openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From efenderbosch <eric.fenderbo...@segmint.com>
Subject How to use Postgres inet data type with OpenJPA [2.2.2]?
Date Wed, 17 Jul 2013 18:57:19 GMT
I need to record IP addresses in a Postgres (9.0) table with OpenJPA (2.2.2).

I've got it working using a native query:

    EntityManager entityManager =
entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    int rows = entityManager.createNativeQuery("insert into click (ip)
values (?::inet)") //
        .setParameter(1, InetAddress.getLocalHost().getHostAddress()) //
        .executeUpdate();
    entityManager.getTransaction().commit();
    entityManager.close();

But I'd prefer to figure out a way for OpenJPA to handle it w/o a native
query.

I searched and found some other suggestions like annotating the column like
this:

    @Column(length = -1, columnDefinition = "inet")

But that doesn't seem to do the cast. I get this exception:

    ERROR: column "ip" is of type inet but expression is of type character
varying
    Hint: You will need to rewrite or cast the expression.
    Position: 32 {prepstmnt 1376458920 INSERT INTO Click (ip) VALUES (?)
[params=?]} [code=0, state=42804]

Can I annotate the field w/ @Strategy and implement a custom FieldStrategy
or ValueHandler? This seems like the right solution, but the documentation
is pretty light and I can't find a simple example to start from.

https://openjpa.apache.org/builds/2.2.2/apache-openjpa/docs/ref_guide_mapping_custom.html

Any direction on fixing the annotations or implementing a FieldStrategy or
ValueHandler would be appreciated.



--
View this message in context: http://openjpa.208410.n2.nabble.com/How-to-use-Postgres-inet-data-type-with-OpenJPA-2-2-2-tp7584454.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message