Hibernate + PostgreSQL + Network Address Type (inet, cdir)

前端 未结 2 1022
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 13:44

I have started my example project with Hibernate 4.2.7 and PostgreSQL 9.3.1 and everything is going well.

At the moment I would like to use INET PostgreSQL type but

2条回答
  •  半阙折子戏
    2020-12-09 14:03

    I faced similar problem recently with Hibernate 3 & Postgres 9. Since there was no inbuilt mapping provided for String Array in postgres to Java String[] by hibernate, I end up implementing a custom mapping class. You can try following below steps.

    1. Create Custom class "PgInet" which implements "org.hibernate.usertype.UserType" (this UserType class is per my hibernate 3 knowledge. Not sure if this has been changed in Hibernate 4).
    2. Below method implementation would be critical.

      • equals
      • nullSafeGet
      • nullSafeSet
      • returnedClass return InetAddress.class;
      • sqlTypes return new int[] {java.sql.Types.}

    Once above is done, we just need to set type of property in HBM.xml to this class i.e. PgInet.

    For custom class implementation try referring to existing type implementation class. You should be able to locate class files. Use below link as reference.

    http://grepcode.com/file/repo1.maven.org/maven2/hibernate/hibernate/2.1.8/net/sf/hibernate/type/ArrayType.java

    Hope this helps.

    Thanks.

提交回复
热议问题