Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » IllegalArgument Exception using a Enum as Query Parameter
IllegalArgument Exception using a Enum as Query Parameter [message #1016532] Wed, 06 March 2013 19:20 Go to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
Hi all.

I have a Small entity, that haves an ENUM as Status Column. This enum is Integer on the database (PostgreSQL), and is mapped as an Ordinal Enumerated on JPA.

package br.com.h4.controleatividades.domain.model;

import java.io.Serializable;
import java.util.Date;

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

@Entity
@Table(name="atividade")
@SequenceGenerator(name="atividade_seq", sequenceName="atividade_seq", initialValue=1, allocationSize=1)
public class Atividade implements Serializable {

	private static final long serialVersionUID = 5213347022288962081L;

	@Id
	@GeneratedValue(generator="atividade_seq", strategy=GenerationType.AUTO)
	@Column(name="atividade_id")
	private Integer id;
	
	@Column(name="nome", length=80, nullable=false)
	private String nome;
	
	@Column(name="prioridade", nullable=false)
	private Integer prioridade = 0;
	
	@Column(name="data_limite", nullable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date dataLimite;
	
	@ManyToOne(fetch=FetchType.EAGER, optional=false, cascade={CascadeType.PERSIST, CascadeType.MERGE})
	@JoinColumn(name="responsavel_id", nullable=false)
	private Responsavel responsavel;
	
	@Column(name="status", nullable=false)
	@Enumerated(EnumType.ORDINAL)
	private Status status = Status.ENCERRADA;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Integer getPrioridade() {
		return prioridade;
	}

	public void setPrioridade(Integer prioridade) {
		this.prioridade = prioridade;
	}

	public Date getDataLimite() {
		return dataLimite;
	}

	public void setDataLimite(Date dataLimite) {
		this.dataLimite = dataLimite;
	}

	public Responsavel getResponsavel() {
		return responsavel;
	}

	public void setResponsavel(Responsavel responsavel) {
		this.responsavel = responsavel;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Atividade other = (Atividade) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}



When I try to set a parameter on this property, I get this error:

java.lang.IllegalArgumentException: You have attempted to set a value of type class br.com.h4.controleatividades.domain.model.Status for parameter status with expected type of class br.com.h4.controleatividades.domain.model.Status from query string SELECT a FROM Atividade a WHERE a.status = :status ORDER BY a.prioridade DESC.
	at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:827)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:546)


This is the query code:

String jpql = "SELECT a FROM Atividade a WHERE a.status = :status ORDER BY a.prioridade DESC";
		TypedQuery<Atividade> q = getEntityManager().createQuery(jpql, Atividade.class);
		q.setParameter("status", Status.ABERTA);


Im using Eclipselink 2.4.1 in a StandAlone application.
Re: IllegalArgument Exception using a Enum as Query Parameter [message #1016831 is a reply to message #1016532] Thu, 07 March 2013 20:26 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

The error seems to indicate you somehow have 2 status classes on your classpath. Are you using Spring or OSGi?
Check your class loader usage.


James : Wiki : Book : Blog : Twitter
Re: IllegalArgument Exception using a Enum as Query Parameter [message #1016833 is a reply to message #1016831] Thu, 07 March 2013 20:28 Go to previous messageGo to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
Im using Spring 3.1.2 and I'm not using OSGi. How can this occour? It's a very small project.

[Updated on: Fri, 08 March 2013 19:35]

Report message to a moderator

Re: IllegalArgument Exception using a Enum as Query Parameter [message #1021078 is a reply to message #1016833] Tue, 19 March 2013 13:48 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Spring does use its own classloaders, so you may have something misconfigured in Spring. Can you try removing Spring from your test to confirm the issue is with Spring?


James : Wiki : Book : Blog : Twitter
Previous Topic:JOIN Syntax Worked in toplink essentials
Next Topic:Cached Map Update NPE on element remove
Goto Forum:
  


Current Time: Thu Sep 18 05:56:58 GMT 2014

Powered by FUDForum. Page generated in 0.44410 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software