Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalArgumentException: argument type mismatch in Hibernate

Out of the blue I started getting “IllegalArgumentException: argument type mismatch” in hibernate. The hibernate entity was working for quite some time and svn logs confirm the code to be intact.

What might be the case?
Here’s part of the exception

Jan 16, 2010 10:47:09 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalArgumentException: argument type mismatch
    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.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
    at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
    at org.hibernate.loader.Loader.doQuery(Loader.java:729)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2220)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:930)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
like image 525
Quintin Par Avatar asked Jan 16 '10 17:01

Quintin Par


2 Answers

Translation: Hibernate provides an argument of wrong type when trying to invoke a setter method.

My first step would be to find out which setter that is (for instance by debugging the application in eclipse, setting an exception break point, and inspecting the stack variables once the breakpoint is reached).

Edit: What is the signature of the setter for the mapped property qs? It should take a Set<Q>.

like image 111
meriton Avatar answered Oct 07 '22 06:10

meriton


The solution is the use of "addScalar" in your query execution.

Suppose your Entity is a user :

@Entity
@Table(name="user")
@DynamicUpdate(value=true)
public class UserEntity implements Serializable{    

@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id",updatable=false)
private Long id;

@Column(name="name",nullable=false,length=20)
private String name;
....
}

Instead of doing :

List<UserEntity> users = sessionFactory.getCurrentSession()
      .createQuery(query)
      .list();

Use addScalar :

List<UserEntity> users = sessionFactory.getCurrentSession()
     .createQuery(query)
     .addScalar("id",LongType.INSTANCE)
     .addScalar("name",StringType.INSTANCE)         
     .list();

Note : prior to LongType.INSTANCE, there was Hibernate.LONG and Hibernate.STRING (they are deprecated now).

like image 34
gezdy Avatar answered Oct 07 '22 05:10

gezdy