Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java InetAddress wrapper for PostgreSQL inet data type for Hibernate
//Java bean wrapping InetAddress to enable Hibernate support for PostgreSQL inet type.
import java.io.Serializable;
import java.net.InetAddress;
@SuppressWarnings("serial")
public class PgInet implements Serializable {
private InetAddress address;
public PgInet() {}
public PgInet(InetAddress address) {
this.address = address;
}
public InetAddress getAddress() {
return address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof PgInet)) {
return false;
}
PgInet other = (PgInet) obj;
if (address == null) {
if (other.address != null) {
return false;
}
} else if (!address.equals(other.address)) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PgInet [address=");
builder.append(address);
builder.append("]");
return builder.toString();
}
}
// Hibernate UserType extension wrapping the bean for Hibernate integration.
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;
import org.postgresql.util.PGobject;
public class PgInetType implements UserType {
public PgInetType() {}
@Override
public Object assemble(Serializable cached, Object owner) {
return deepCopy(cached);
}
@Override
public Object deepCopy(Object value) {
if (value != null) {
return new PgInet(((PgInet) value).getAddress());
}
return null;
}
@Override
public Serializable disassemble(Object value) {
return (value != null) ? (Serializable) deepCopy(value) : null;
}
@Override
public boolean equals(Object x, Object y) {
return x == y || ( x != null && y != null && x.equals( y ) );
}
@Override
public int hashCode(Object x) {
return (x != null) ? x.hashCode() : 0;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names,
SessionImplementor session, Object owner) throws SQLException {
PgInet address = null;
String ipStr = rs.getString(names[0]);
if (ipStr != null) {
try {
address = new PgInet(InetAddress.getByName(ipStr));
} catch (UnknownHostException e) {
throw new HibernateException(e);
}
}
return address;
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index,
SessionImplementor session) throws SQLException {
if (value == null) {
st.setNull(index, Types.VARCHAR);
}
else {
PGobject pgObj = new PGobject();
pgObj.setType("inet");
pgObj.setValue(((PgInet) value).getAddress().getHostAddress());
st.setObject(index, pgObj);
}
}
@Override
public Object replace(Object original, Object target, Object owner) {
return deepCopy(original);
}
@SuppressWarnings("rawtypes")
@Override
public Class returnedClass() {
return PgInet.class;
}
@Override
public int[] sqlTypes() {
return new int[] {Types.OTHER};
}
}
// Type definition annotation for using the custom type
@TypeDefs(value={
@TypeDef(name="pgInet", typeClass=PgInetType.class)
})
// Field annotation for using the custom type via the above type definition
@Column(name="IP_ADDRESS", nullable=false)
@Type(type="pgInet")
private PgInet ipAddress;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment