Hibernate, HQL e enum

Pessoal estou querendo fazer uma consulta passando um enum como parametro, mas sendo que está rolando o seguinte erro abaixo e tentei modificar de várias formas e nada :confused:

[size=18]
java.lang.String cannot be cast to java.lang.Enum[/size][color=red] [/color]

minha Classe Conta

/**
 * 
 */
package com.algaworks.dwjsf.financeiro.dominio;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * @author max davis
 * 
 */
@Entity
@Table(name = "conta")
public class Conta {
	private Long id;
	private Pessoa pessoa;
	private String descricao;
	private BigDecimal valor;
	private TipoConta tipo;
	private Date dataVencimento;
	private Date dataBaixa;

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	/**
	 * @param pessoa
	 *            the pessoa to set
	 */
	public void setPessoa(Pessoa pessoa) {
		this.pessoa = pessoa;
	}

	/**
	 * @return the pessoa
	 */
	@ManyToOne
	@JoinColumn(name = "pessoa_id")
	public Pessoa getPessoa() {
		return pessoa;
	}

	/**
	 * @param descricao
	 *            the descricao to set
	 */
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	/**
	 * @return the descricao
	 */
	@Column(name = "descricao")
	public String getDescricao() {
		return descricao;
	}

	/**
	 * @param valor
	 *            the valor to set
	 */
	public void setValor(BigDecimal valor) {
		this.valor = valor;
	}

	/**
	 * @return the valor
	 */

	@Column(name = "valor")
	public BigDecimal getValor() {
		return valor;
	}

	/**
	 * @param tipo
	 *            the tipo to set
	 */

	public void setTipo(TipoConta tipo) {
		this.tipo = tipo;
	}

	/**
	 * @return the tipo
	 */
	@Enumerated(EnumType.STRING)
	public TipoConta getTipo() {
		return tipo;
	}

	/**
	 * @param dataVencimento
	 *            the dataVencimento to set
	 */
	public void setDataVencimento(Date dataVencimento) {
		this.dataVencimento = dataVencimento;
	}

	/**
	 * @return the dataVencimento
	 */
	@Column(name = "data_vencimento")
	@Temporal(TemporalType.DATE)
	public Date getDataVencimento() {
		return dataVencimento;
	}

	/**
	 * @param dataBaixa
	 *            the dataBaixa to set
	 */
	public void setDataBaixa(Date dataBaixa) {
		this.dataBaixa = dataBaixa;
	}

	/**
	 * @return the dataBaixa
	 */
	@Column(name = "data_baixa")
	@Temporal(TemporalType.DATE)
	public Date getDataBaixa() {
		return dataBaixa;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Conta other = (Conta) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}

minha Consulta no metodo passando o enum com parametro

public List<Conta> consultarPorTipoConta(TipoConta tipos) {
			
		String tipo = ((Enum)tipos).name();
		System.out.print(tipo);		

		Query query = this.getSession().createQuery("select conta from Conta conta where conta.tipo like :tipo"); 
		query.setParameter("tipo", tipo);  

		
		List<Conta> lista = query.list();  		
		
		return lista ;
	}

O erro ocorre porque você está passando explicitamente uma String pro Hibernate comparar com o Enum.

String tipo = ((Enum)tipos).name(); 

Já tentou comparar com a varíavel “tipos” mesmo? O Hibernate consegue reconhecer os Enum, acho que não haverá problema em compará-lo direto.

Quer uma dica? O uso de Criteria é uma boa nesses casos :smiley:

Espero ter ajudado!