Using PostgreSQL inet type with Ebean

135 views
Skip to first unread message

Yorick Holkamp

unread,
Feb 14, 2019, 6:40:07 AM2/14/19
to Ebean ORM
For a project where we're looking to use Ebean we have a number of PostgreSQL tables that have columns of the 'inet' type and POJOs with InetAddress properties. While at first glance it looks like Ebean can map these, a closer inspection reveals that the default behavior is to map InetAddress in Java to a varchar(50) by default, even for SQL servers that have a native type available. Since we're dealing with existing tables, that's not an option for us.

The default behavior results in a "column "address" is of type inet but expression is of type character varying   Hint: You will need to rewrite or cast the expression." error.

ServerConfig has the 'addCustomMapping' method, but requires a DbType enum value to be included, which seemingly makes it impossible to add a custom mapping to achieve this InetAddress<->inet mapping. Is there a workaround available to still be able to handle this or would this require an extension of the Ebean code?

Thanks!

Rob Bygrave

unread,
Feb 14, 2019, 3:28:31 PM2/14/19
to ebean@googlegroups
I think the question becomes is the support of Postgres InetAddress a DDL only issue or if it needs specific treatment at the JDBC bind (to PreparedStatement) and read (from ResultSet).  

Do you have a link to an example of using InetAddress in JDBC ?   Specifically, the PreparedStatement setParameter() and ResultSet .getXXX()


Notes: If you are looking at Ebean ScalarTypeXXX code then:
- DataReader ... is a light abstraction over ResultSet
- DataBind ... is a light abstraction wrapping PreparedStatement

--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Bygrave

unread,
Feb 14, 2019, 3:46:58 PM2/14/19
to ebean@googlegroups
Need to confirm, if the below is approximately correct:

-- Reading ResultSet  (just treat as a string)

String ipStr = resultSet.getString(x);

if (ipStr == null) {
   return null;
} else {
  return InetAddress.getByName(ipStr);
}



-- Binding PreparedStatement, use PGobject

PGobject pgObj = new PGobject();
pgObj.setType("inet");
pgObj.setValue(inetAddressValue.getHostAddress());

preparedStatement.setObject(index, pgObj);


Given the above is the way to go ... we'd then likely create a  ScalarTypeInetAddressPostgres to do that (so not just a DDL change).

Rob Bygrave

unread,
Feb 17, 2019, 4:47:09 PM2/17/19
to ebean@googlegroups


Need to check testing of migration model.

Yorick Holkamp

unread,
Feb 18, 2019, 6:33:31 AM2/18/19
to eb...@googlegroups.com
Thanks for the quick turnaround. The PR looks great and definitely beats my workarounds via a custom Ebean plugin. 

You received this message because you are subscribed to a topic in the Google Groups "Ebean ORM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ebean/FkeBkyx0Oj4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ebean+un...@googlegroups.com.

Rob Bygrave

unread,
Feb 24, 2019, 4:00:39 PM2/24/19
to ebean@googlegroups
Released in 11.35.1

You can use either java.net.InetAddress or io.ebean.types.Inet ... with the Inet type not performing any DNS lookup or address validation (and supporting address ranges that Postgres INET type supports).


People who are already using  java.net.InetAddress with Postgres and mapping that to varchar can turn it off via:

ebean.databaseInetAddressVarchar = true 
or
serverConfig.getPlatformConfig().setDatabaseInetAddressVarchar(true)

... and complain a bit maybe. The thinking here is that there won't be many people mapping InetAddress and if there were they would have been hitting us up to map that to Postgres INET before now.  So making Postgres INET the default seems reasonable.  Hopefully this doesn't negatively impact too many but if it does then let me know.


Cheers, Rob.
Reply all
Reply to author
Forward
0 new messages