Create postgres table mapping with inet type column

I have a table in the postgres database and the column is of type inet, as I do to map this attribute in the entity.

You can use JPA’s @Convert annotation for your entity attribute and write a converter. Please see an example here: https://github.com/cuba-labs/db-type-convert

Example of a converter:

@Converter
public class InetConverter implements AttributeConverter<String, Object> {

  @Override
  public Object convertToDatabaseColumn(String attribute) {
      if (attribute == null)
          return null;
      PGobject pgobject = new PGobject();
      pgobject.setType("inet");
      try {
          pgobject.setValue(attribute);
      } catch (SQLException e) {
          throw new RuntimeException(e);
      }
      return pgobject;
  }

  @Override
  public String convertToEntityAttribute(Object dbData) {
      return dbData == null ? null : dbData.toString();
  }
}

Entity attribute annotation:

@Column(name = "IP_ADDRESS")
@Convert(converter = InetConverter.class)
protected String ip_address;

See the full description in the sample project.

2 Likes

Hi ,
Thanks, it worked.