Galera, estou com um problema que não consigo resolver, já procurei de tudo na net mas ainda não achei solução.
O que acontece é o seguinte, tenho um tipo de dados em um banco postgres chamado flag, esse tipo foi criado como enum.
CREATE TYPE flag AS ENUM('0','1');
Na minha classe de Estado tenho um atributo que é do tipo flag, segue abaixo minha classe mapeada:
@Entity
@Table(name = "ger_tb_estado", uniqueConstraints = {
@UniqueConstraint(columnNames = "cd_uf"),
@UniqueConstraint(columnNames = "nm_estado") })
@TypeDef(name = "flagEnum", typeClass = PgEnumUserType.class, parameters =
{@Parameter(name = "enumClassName", value = "inforcoop.seiam.modelo.tipo.Flag")})
public class GerTbEstado implements java.io.Serializable {
private Integer pkIdEstado;
private String cdUf;
private String nmEstado;
private Flag flRemovido;
private Set<GerTbCidade> gerTbCidades = new HashSet<GerTbCidade>(0);
public GerTbEstado() {
}
public GerTbEstado(String cdUf, String nmEstado) {
this.cdUf = cdUf;
this.nmEstado = nmEstado;
}
public GerTbEstado(String cdUf, String nmEstado,
Set<GerTbCidade> gerTbCidades) {
this.cdUf = cdUf;
this.nmEstado = nmEstado;
this.gerTbCidades = gerTbCidades;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "pk_id_estado", unique = true, nullable = false)
public Integer getPkIdEstado() {
return this.pkIdEstado;
}
public void setPkIdEstado(Integer pkIdEstado) {
this.pkIdEstado = pkIdEstado;
}
@Column(name = "cd_uf", unique = true, nullable = false, length = 2)
public String getCdUf() {
return this.cdUf;
}
public void setCdUf(String cdUf) {
this.cdUf = cdUf;
}
@Column(name = "nm_estado", unique = true, nullable = false, length = 70)
public String getNmEstado() {
return this.nmEstado;
}
public void setNmEstado(String nmEstado) {
this.nmEstado = nmEstado;
}
@Column(name = "fl_removido", nullable = true, columnDefinition = "flag DEFAULT '0'")
@Type(type = "flagEnum")
public Flag getFlRemovido() {
return this.flRemovido;
}
public void setFlRemovido(Flag flRemovido) {
this.flRemovido = flRemovido;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "gerTbEstado")
public Set<GerTbCidade> getGerTbCidades() {
return this.gerTbCidades;
}
public void setGerTbCidades(Set<GerTbCidade> gerTbCidades) {
this.gerTbCidades = gerTbCidades;
}
}
Enum Flag:
public enum Flag implements Serializable{
NAO(0),
SIM(1);
private int value;
Flag(int value) {
this.value = value;
}
// the identifierMethod
public int toInt() {
return value;
}
// the valueOfMethod
public static Flag fromInt(int value) {
switch(value) {
case 0: return NAO;
case 1: return SIM;
default:
return NAO;
}
}
public String toString() {
switch(this) {
case NAO:
return "0";
case SIM:
return "1";
}
return null;
}
}
Classe PgEnumUserType (essa classe está sendo utilizada em @Typedef)
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.EnhancedUserType;
import org.hibernate.usertype.ParameterizedType;
import org.postgresql.util.PGobject;
// This implementation works only with Postgres
public class PgEnumUserType implements EnhancedUserType, ParameterizedType {
// Enum class under observation
@SuppressWarnings("rawtypes")
private Class<Enum> enumClass;
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setParameterValues(Properties parameters) {
String enumClassName = parameters.getProperty("enumClassName");
try {
enumClass = (Class<Enum>) Class.forName(enumClassName);
} catch (ClassNotFoundException cnfe) {
throw new HibernateException("Enum class not found", cnfe);
}
}
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached;
}
public Object deepCopy(Object value) throws HibernateException {
System.out.println(value.toString());
System.out.println(value.getClass().toString());
return value;
}
@SuppressWarnings("rawtypes")
public Serializable disassemble(Object value) throws HibernateException {
return (Enum) value;
}
public boolean equals(Object x, Object y) throws HibernateException {
return x == y;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean isMutable() {
return false;
}
@SuppressWarnings("unchecked")
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Object object = rs.getObject(names[0]);
if (rs.wasNull()) {
return null;
}
// Notice how Object is mapped to PGobject. This makes this implementation Postgres specific
if (object instanceof PGobject) {
PGobject pg = (PGobject) object;
return Enum.valueOf(enumClass, pg.getValue());
}
return null;
}
@SuppressWarnings("rawtypes")
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, 1111);
//st.setNull(index, Types.VARCHAR);
// UPDATE: To support NULL insertion, change to: st.setNull(index, 1111);
} else {
// Notice 1111 which java.sql.Type for Postgres Enum
st.setObject(index, ((Enum)value), 1111);
}
}
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
@SuppressWarnings("rawtypes")
public Class returnedClass() {
return enumClass;
}
public int[] sqlTypes() {
return new int[] { 1111 };
//return new int[] { Types.VARCHAR };
// UPDATE: To support NULL insertion, change to: return new int[] { 1111 };
}
@SuppressWarnings("unchecked")
public Object fromXMLString(String xmlValue) {
return Enum.valueOf(enumClass, xmlValue);
}
@SuppressWarnings("rawtypes")
public String objectToSQLString(Object value) {
return '\'' + ((Enum) value).name() + '\'';
}
@SuppressWarnings("rawtypes")
public String toXMLString(Object value) {
return ((Enum) value).name();
}
@Override
public Object nullSafeGet(ResultSet arg0, String[] arg1,
SessionImplementor arg2, Object arg3) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void nullSafeSet(PreparedStatement arg0, Object arg1, int arg2,
SessionImplementor arg3) throws HibernateException, SQLException {
// TODO Auto-generated method stub
}
}
O que acontece é que na hora de fazer uma consulta, um cadastro ou qualquer coisa na tabela estado, tenho a seguinte exceção como retorno:
No value specified for parameter 1.
org.postgresql.util.PSQLException: No value specified for parameter 1.
org.hibernate.exception.DataException: No value specified for parameter 1.
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:134)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy23.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1962)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2447)
at org.hibernate.loader.Loader.doList(Loader.java:2433)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263)
at org.hibernate.loader.Loader.list(Loader.java:2258)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:122)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1535)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at inforcoop.seiam.dao.generico.DAO.listaCompleta(DAO.java:45)
at inforcoop.seiam.dao.GerTbEstadoDAO.listaCompleta(GerTbEstadoDAO.java:44)
at inforcoop.seiam.util.Teste.main(Teste.java:18)
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:178)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:246)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 15 more
E antes que digam, eu estou passando sim o valor de flRemovido, se alguém puder me ajudar ficarei grato!