
É bem comum esbarrarmos com algumas colunas de tabelas de banco de dados que só aceitem valores dentro de um conjunto estipulado. Por exemplo, coluna sexo da tabela pessoa apenas aceita os valores ‘MASCULINO’ e ‘FEMININO’, e em nosso sistema que faz uso do Hibernate, comumente nossa classe Pessoa teria um atributo sexo do tipo String.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Pessoa implements Serializable { | |
@Column(name="sexo") | |
private String sexoString; | |
} |
Mas o atributo ser String possibilita que ele receba outros valores além de ‘MASCULINO’ e ‘FEMININO’, podendo falhar em casos semelhantes de corresponder às regras de negócio. Então, o correto seria esse atributo ser um enum com esse valores fixos e não uma String, mas como fazer isso se estamos trabalhando com o JPA?
O primeiro passo é criar uma classe que implemente a interface org.hibernate.usertype.UserType do Hibernate para criarmos o nosso próprio tipo, um tipo genérico de enum. Essa classe também deverá implementar a interface org.hibernate.usertype.ParameterizedType para que possamos passar via annotation para ela o nosso enum específico.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("rawtypes") | |
public class EnumType implements UserType, ParameterizedType { | |
private Class enumClass; | |
public EnumType() { | |
super(); | |
} | |
@SuppressWarnings("unchecked") | |
public void setParameterValues(Properties parameters) { | |
String enumClassName = parameters.getProperty("enumClassName"); | |
try { | |
enumClass = (Class) Class.forName(enumClassName); | |
} catch (ClassNotFoundException e) { | |
throw new HibernateException("Enum class not found ", e); | |
} | |
} | |
public int[] sqlTypes() { | |
return new int[] { Types.OTHER }; | |
} | |
public Class returnedClass() { | |
return enumClass; | |
} | |
public boolean equals(Object x, Object y) throws HibernateException { | |
return x == y; | |
} | |
public int hashCode(Object x) throws HibernateException { | |
return x.hashCode(); | |
} | |
@Override | |
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { | |
String pgObject = rs.getString(names[0]); | |
try { | |
return Enum.valueOf(enumClass, pgObject); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { | |
if (value == null) { | |
st.setNull(index, Types.OTHER); | |
} else { | |
st.setObject(index,((Enum) value), Types.OTHER); | |
} | |
} | |
public Object deepCopy(Object value) throws HibernateException { | |
return value; | |
} | |
public boolean isMutable() { | |
return false; | |
} | |
public Serializable disassemble(Object value) throws HibernateException { | |
return (Enum) value; | |
} | |
public Object assemble(Serializable cached, Object owner) | |
throws HibernateException { | |
return cached; | |
} | |
public Object replace(Object original, Object target, Object owner) | |
throws HibernateException { | |
return original; | |
} | |
@SuppressWarnings("unchecked") | |
public Object fromXMLString(String xmlValue) { | |
return Enum.valueOf(enumClass, xmlValue); | |
} | |
public String objectToSQLString(Object value) { | |
return '\'' + ((Enum) value).name() + '\''; | |
} | |
public String toXMLString(Object value) { | |
return ((Enum) value).name(); | |
} | |
} |
Tendo nossa classe, basta agora criarmos nosso enum para o atributo sexo. Lembrando que obrigatoriamente todo valor contido nesse enum deve ser exatamente como os prefixados para a coluna sexo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum SexoEnum { | |
MASCULINO, FEMININO; | |
} |
E por fim, deixaremos de usar String como tipo para nosso atributo da classe Pessoa. Para isso, usaremos a annotation @Type, informando a ela nossa classe que implementa UserType, e passarmos como parâmetro nosso enum específico dessa coluna.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Pessoa implements Serializable { | |
@Column(name="sexo") | |
@Type(type = "seu.pacote.EnumType", | |
parameters = { @org.hibernate.annotations.Parameter(name = "enumClassName", | |
value = "seu.pacote.SexoEnum")}) | |
private String sexoString; | |
} |