Skip to content

Instantly share code, notes, and snippets.

@gunnarmorling
Created January 7, 2020 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gunnarmorling/a3cf63d173898e596e1eafe0c46d96b0 to your computer and use it in GitHub Desktop.
Save gunnarmorling/a3cf63d173898e596e1eafe0c46d96b0 to your computer and use it in GitHub Desktop.
package dev.morling.demos.usertype;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.BigDecimalType;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;
/**
* Use like this:
* @Columns(columns = {
* @Column(name = "quantity_amount"),
* @Column(name = "quantity_metric")
* })
* @Type(type ="dev.morling.demos.quarkus.QuantityUserType")
* public Quantity quantity;
*/
public class QuantityUserType implements CompositeUserType {
@Override
public Class returnedClass() {
return Quantity.class;
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
return Objects.equals(x, y);
}
@Override
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
BigDecimal amount = rs.getBigDecimal(names[0]);
Metric metric = Metric.valueOf(rs.getString(names[1]));
return new Quantity(amount, metric);
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.NUMERIC);
st.setNull(index + 1, Types.VARCHAR);
} else {
st.setBigDecimal(index, ((Quantity) value).amount);
st.setString(index + 1, ((Quantity) value).metric.name());
}
}
@Override
public Object deepCopy(Object value) throws HibernateException {
return value != null ? new Quantity((Quantity) value) : null;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public String[] getPropertyNames() {
return new String[]{ "amount", "metric" };
}
@Override
public Type[] getPropertyTypes() {
return new Type[] { BigDecimalType.INSTANCE, StringType.INSTANCE };
}
@Override
public Object getPropertyValue(Object component, int property) throws HibernateException {
if (property == 0) {
return ((Quantity)component).amount;
}
else {
return ((Quantity)component).metric;
}
}
@Override
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
if (property == 0) {
((Quantity)component).amount = (BigDecimal) value;
}
else {
((Quantity)component).metric = Metric.valueOf((String) value);
}
}
@Override
public Serializable disassemble(Object value, SharedSessionContractImplementor session) throws HibernateException {
throw new UnsupportedOperationException();
}
@Override
public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) {
throw new UnsupportedOperationException();
}
@Override
public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner) {
return original;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment