本文介绍了如何解决Hibernate异常的原因“调用setter时发生IllegalArgumentException”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决偶然异常的原因当调用setter 时,会发生IllegalArgumentException异常,当我的程序尝试通过数据库中的名称加载用户时,Hibernate会抛出? p>

我将表用户中的列 USER_RV 整数而不是 BigDecimal 或其他整数类型?请注意,相同的应用程序将其他表中的 NUMBER 列映射到 Integer 对象,但Hibernate 将这个异常从这些表格中填入一个对象时抛出此异常。



另请注意,该程序启用 RMI缓存复制 strong>仅用户缓存。这个异常可能与缓存复制有关吗?是否是Ehcache或Hibernate中的错误?

 导致:org.springframework.orm.hibernate3.HibernateSystemException:调用时发生IllegalArgumentException com.db.spgit.abstrack.model.User.rv的setter嵌套异常是org.hibernate.PropertyAccessException:在org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)调用com.db.spgit.abstrack.model.User.rv 
的setter时发生IllegalArgumentException )
在org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
在org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
在org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
在org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1055)
在org。 SpringBegin abstrack.dao.hibernate.User DaoHibernate.loadByName(UserDaoHibernate.java:63)
在com.db.spgit.abstrack.manager.QuoteManager.populateUser(QuoteManager.java:330)
在com.db.spgit.abstrack.manager。 QuoteManager.populateOwnerUK(QuoteManager.java:307)
在com.db.spgit.abstrack.manager.QuoteManager.populateGuids(QuoteManager.java:254)
在com.db.spgit.abstrack.manager。 QuoteManager.addQuoteReturnVwRfqUK(QuoteManager.java:429)
at com.db.spgit.abstrack.manager.QuoteManager $$ FastClassByCGLIB $$ d817accb.invoke(& lt; generate>)
at net.sf .cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
在org.springframework.aop.framework.Cglib2AopProxy $ CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
在org.springframework.aop .framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
在org.springframe work.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
在org.springframework.aop.framework.Cglib2AopProxy $ DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
在com.db. spgit.abstrack.manager.QuoteManager $$ EnhancerByCGLIB $$ cc8d0025.addQuoteReturnVwRfqUK(& lt; generate>)
在com.db.abstrack.ejb.RfqBean.addRfqDraftListUK(RfqBean.java:370)
... 59更多
导致:org.hibernate.PropertyAccessException:在org.hibernate.property.BasicPropertyAccessor调用com.db.spgit.abstrack.model.User.rv
的setter时发生IllegalArgumentException $ BasicSetter.set(BasicPropertyAccessor.java:104)
在org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
在org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues( PojoEntityTuplizer.java:200)
在org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(Abstra ctEntityPersister.java:3514)
在org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:116)
在org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java: 82)
在org.hibernate.event.def.DefaultLoadEventListener.assembleCacheEntry(DefaultLoadEventListener.java:553)
在org.hibernate.event.def.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:508)
在org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:357)
在org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
在org .hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:179)
在org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
在org.hibernate.impl .SessionImpl.fireLoad(SessionImpl.java:878)
在org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
在org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
在org.hibernate.type.ManyToOneType.assemble(ManyToOneType.java:196)
在org.hibernate.cache.StandardQueryCache。 get(StandardQueryCache.java:130)
在org.hibernate.loader.Loader.getResultFromQueryCache(Loader.java:2152)
在org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2117)
在org.hibernate.loader.Loader.list(Loader.java:2087)
在org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
在org .html.bat $ 36.doInHibernate(HibernateTemplate.java:1065)
在org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 77更多
导致:java .lang.IllegalArgumentException:java.lang.ClassCastException @ 6be0d6
at sun.reflect.GeneratedMethodAccessor104.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
在java.lang.reflect.Method.invoke (Method.java:597)
在org.hibernate.property.BasicPropertyAccessor $ BasicSetter.set(BasicPropertyAccessor.java:42)
... 101更多

课程定义用户



<$
@Table(name =TB_USER$ b)
@Table(name =TB_USER
,schema =ABSOL_USER

public class用户实现java.io.Serializable {
private String id;
private String userName;
private整数rv;

public User(){
}

public User(String userName,Integer rv){
this.userName = userName;
this.rv = rv;


@GenericGenerator(name =generator,strategy =guid)@ Id @GeneratedValue(generator =generator)

@Column name =USER_ID,unique = true,nullable = false)
public String getId(){
return this.id;
}

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

@Column(name =USER_NAME,nullable = false,length = 20)
public String getUserName(){
return this.userName;
}

public void setUserName(String userName){
this.userName = userName;
}

@Column(name =USER_RV,precision = 22,scale = 0)
public Integer getRv(){
return this.rv;
}

public void setRv(Integer rv){
this.rv = rv;
}
}

表的定义 TB_USER

  CREATE TABLEABSOL_OWNER。TB_USER(
USER_ID 16)DEFAULT SYS_GUID(),
USER_NAMEVARCHAR2(20 CHAR)NOT NULL ENABLE,
USER_RVNUMBER NOT NULL ENABLE,
PRIMARY KEY(USER_ID)

环境:




  • Hibernate 3.3.2.GA

  • Ehcache 2.2.0

  • WebLogic Server 10.3.2

  • Oracle数据库

  • 缓存用户对象

  • 通过远程密钥无效启用RMI缓存复制


解决方案

事实证明,这个异常是由于Hibernate中的一个错误。我的一个队友发现了一个相关的错误报告, ,这是在使用Hibernate和时发生的。



根据报告,将Hibernate属性 hibernate.cache.use_structured_entries 设置为 true 避免例外。根据Hibernate ,这个属性强制Hibernate以更人性化的格式将数据存储在二级缓存中。显然,结构化缓存条目还确保Hibernate将二级缓存中的对象的属性正确映射到实体中的相应属性。



有一个有趣的,解释了Hibernate如何使用二级缓存。



在3.3.2GA之后,Hibernate的版本是否也会呈现此行为?


How do I cure the cause of the occasional exception IllegalArgumentException occurred while calling setter that Hibernate throws when my program attempts to load a user by name from the database?

Am I incorrectly mapping column USER_RV in table User to class Integer instead of BigDecimal or some other integer type? Note that the same application maps NUMBER columns in other tables to Integer objects, but Hibernate does not throw this exception when populating an object with a row from those tables.

Further note that the program enables RMI cache replication for the User cache only. Might this exception be related to cache replication? Is it a bug in Ehcache or Hibernate?

Caused by: org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred while calling setter of com.db.spgit.abstrack.model.User.rv; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.db.spgit.abstrack.model.User.rv
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1055)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1048)
    at com.db.abstrack.dao.hibernate.AbstrackDaoHibernate.findByCriteria(AbstrackDaoHibernate.java:57)
    at com.db.abstrack.dao.hibernate.UserDaoHibernate.loadByName(UserDaoHibernate.java:63)
    at com.db.spgit.abstrack.manager.QuoteManager.populateUser(QuoteManager.java:330)
    at com.db.spgit.abstrack.manager.QuoteManager.populateOwnerUK(QuoteManager.java:307)
    at com.db.spgit.abstrack.manager.QuoteManager.populateGuids(QuoteManager.java:254)
    at com.db.spgit.abstrack.manager.QuoteManager.addQuoteReturnVwRfqUK(QuoteManager.java:429)
    at com.db.spgit.abstrack.manager.QuoteManager$$FastClassByCGLIB$$d817accb.invoke(&lt;generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
    at com.db.spgit.abstrack.manager.QuoteManager$$EnhancerByCGLIB$$cc8d0025.addQuoteReturnVwRfqUK(&lt;generated>)
    at com.db.abstrack.ejb.RfqBean.addRfqDraftListUK(RfqBean.java:370)
    ... 59 more
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.db.spgit.abstrack.model.User.rv
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:104)
    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:3514)
    at org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:116)
    at org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:82)
    at org.hibernate.event.def.DefaultLoadEventListener.assembleCacheEntry(DefaultLoadEventListener.java:553)
    at org.hibernate.event.def.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:508)
    at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:357)
    at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
    at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:179)
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
    at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
    at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
    at org.hibernate.type.ManyToOneType.assemble(ManyToOneType.java:196)
    at org.hibernate.cache.StandardQueryCache.get(StandardQueryCache.java:130)
    at org.hibernate.loader.Loader.getResultFromQueryCache(Loader.java:2152)
    at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2117)
    at org.hibernate.loader.Loader.list(Loader.java:2087)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    at org.springframework.orm.hibernate3.HibernateTemplate$36.doInHibernate(HibernateTemplate.java:1065)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    ... 77 more
Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException@6be0d6
    at sun.reflect.GeneratedMethodAccessor104.invoke(Unknown Source)
    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)
    ... 101 more

Definition of class User:

@Entity
@Proxy(lazy=false)
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@Table(name="TB_USER"
    ,schema="ABSOL_USER"
)
public class User  implements java.io.Serializable {
     private String id;
     private String userName;
     private Integer rv;

     public User() {
     }

     public User(String userName, Integer rv) {
        this.userName = userName;
        this.rv = rv;
     }

     @GenericGenerator(name="generator", strategy="guid")@Id @GeneratedValue(generator="generator")

     @Column(name="USER_ID", unique=true, nullable=false)
     public String getId() {
         return this.id;
     }

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

     @Column(name="USER_NAME", nullable=false, length=20)
     public String getUserName() {
         return this.userName;
     }

     public void setUserName(String userName) {
         this.userName = userName;
     }

     @Column(name="USER_RV", precision=22, scale=0)
     public Integer getRv() {
         return this.rv;
     }

     public void setRv(Integer rv) {
         this.rv = rv;
     }
}

Definition of table TB_USER:

CREATE TABLE "ABSOL_OWNER"."TB_USER" (
   "USER_ID" RAW(16) DEFAULT SYS_GUID(),
   "USER_NAME" VARCHAR2(20 CHAR) NOT NULL ENABLE,
   "USER_RV" NUMBER NOT NULL ENABLE,
   PRIMARY KEY ("USER_ID")
)

Environment:

  • Hibernate 3.3.2.GA
  • Ehcache 2.2.0
  • WebLogic Server 10.3.2
  • Oracle database
  • Caching User objects
  • Enabled RMI cache replication via remote key invalidation

解决方案

It turns out that this exception is due to a bug in Hibernate. One of my teammates found a related bug report, Re-assembling entity from clustered JBoss Cache may fail with "argument type mismatch", that occurs when using Hibernate and JBoss Cache.

According to the report, setting Hibernate property hibernate.cache.use_structured_entries to true avoids the exception. According to the Hibernate documentation, this property "forces Hibernate to store data in the second-level cache in a more human-friendly format". Apparently, structured cache entries also ensure that Hibernate correctly maps properties of an object in the second-level cache to the corresponding property in an entity.

Javalobby has an interesting article that explains how Hibernate uses the second-level cache.

Do versions of Hibernate after 3.3.2GA also exhibit this behaviour?

这篇关于如何解决Hibernate异常的原因“调用setter时发生IllegalArgumentException”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:54