Java IllegalArgumentException:Hibernate 中的参数类型不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2078263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:18:53  来源:igfitidea点击:

IllegalArgumentException: argument type mismatch in Hibernate

javahibernateorm

提问by Quintin Par

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.

突然间,我开始在 hibernate 中收到“IllegalArgumentException:参数类型不匹配”。休眠实体已经工作了很长一段时间,svn 日志确认代码完好无损。

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.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)

采纳答案by meriton

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

翻译:当尝试调用 setter 方法时,Hibernate 提供了错误类型的参数。

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).

我的第一步是找出是哪个 setter(例如,通过在 eclipse 中调试应用程序,设置异常断点,并在到达断点后检查堆栈变量)。

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

编辑:映射属性的 setter 的签名是什么qs?它应该需要一个Set<Q>.

回答by Kaleb Brasee

I've heard of this happening due to underlying database field changes (such as date to timestamp). It might be worth reverting the database changes if you're able and testing it, or checking the .hbm or annotations as Sands suggested.

我听说这是由于底层数据库字段更改(例如日期到时间戳)而发生的。如果您能够并测试它,或者按照 Sands 的建议检查 .hbm 或注释,那么恢复数据库更改可能是值得的。

回答by Pascal Thivent

So, you modified an hibernate mapping file without modifying the Entity? I guess that the qsproperty was already there then. But is it a java.util.Set(as you used a <set>to map your collection)?

所以,你修改了一个休眠映射文件而不修改实体?我猜当时该qs物业已经在那里了。但它是一个java.util.Set(如你使用<set>映射您的收藏)?

回答by gezdy

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

解决方案是在查询执行中使用“ addScalar”。

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 :

使用 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).

注意:在 LongType.INSTANCE 之前,有 Hibernate.LONG 和 Hibernate.STRING(现在已弃用)。

回答by zawhtut

Sometimes it happened when the types are not compatible. For MySQL query following conversion of casting must be made for compatibility.

有时当类型不兼容时会发生这种情况。对于 MySQL 查询,必须进行转换后的兼容性。

select CAST(sum(hours) AS SIGNED) ...

Hope this will help the people run into the similar problem with JPA(JPA-Hibernate for exact).

希望这会帮助人们遇到与 JPA 类似的问题(确切地说是 JPA-Hibernate)。