java - How to use Postgres inet data type with OpenJPA? -
i need record ip addresses in postgres (9.0) table openjpa (2.2.2).
i've got working using native query:
entitymanager entitymanager = entitymanagerfactory.createentitymanager(); entitymanager.gettransaction().begin(); int rows = entitymanager.createnativequery("insert click (ip) values (?::inet)") // .setparameter(1, inetaddress.getlocalhost().gethostaddress()) // .executeupdate(); entitymanager.gettransaction().commit(); entitymanager.close();
but i'd prefer figure out way openjpa handle w/o native query.
i searched , found other suggestions annotating column this:
@column(length = -1, columndefinition = "inet")
but doesn't seem cast. exception:
error: column "ip" of type inet expression of type character varying hint: need rewrite or cast expression. position: 32 {prepstmnt 1376458920 insert click (ip) values (?) [params=?]} [code=0, state=42804]
can annotate field w/ @strategy , implement custom fieldstrategy or valuehandler? seems right solution, documentation pretty light , can't find simple example start from.
https://openjpa.apache.org/builds/2.2.2/apache-openjpa/docs/ref_guide_mapping_custom.html
any direction on fixing annotations or implementing fieldstrategy or valuehandler appreciated.
edit: figured out. since don't have enough rep, can't answer own question.
i created custom dbdictionary correctly cast.
package package.name.goes.here; import org.apache.openjpa.jdbc.schema.column; public class postgresdictionary extends org.apache.openjpa.jdbc.sql.postgresdictionary { @override public string getmarkerforinsertupdate(column col, object val) { string coltype = col.gettypeidentifier().getname(); if (coltype != null) { return "?::" + coltype; } return "?"; }
}
then annotate field with:
@column(columndefinition = "inet")
and register custom dictionary in persistence.xml:
<property name="openjpa.jdbc.dbdictionary" value="package.name.goes.here.postgresdictionary"/>
Comments
Post a Comment