`
sctom123
  • 浏览: 109779 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

IllegalArgumentException occurred calling getter of *

阅读更多
从别处copy过来的,和我的错一样
java 代码
 
  1. I have a basic model called "Group" which has a name, a reference to its parent (which is also a Group) and a collection of its children (which are Groups).  I've defined the hibernate mapping like so:  
  2.   
  3.   @Id  
  4.   @GeneratedValue(strategy=GenerationType.AUTO)  
  5.   public Long getId()  
  6.   {  
  7.     return id;  
  8.   }  
  9.   
  10.   @ManyToOne  
  11.   @JoinColumn(name="group_fk", nullable=true, insertable=false, updatable=false)  
  12.   public Group getParent()  
  13.   {  
  14.     return parent;  
  15.   }  
  16.   
  17.   @OneToMany(cascade = {CascadeType.ALL}, fetch=FetchType.LAZY)  
  18.   @JoinColumn(name="group_fk")  
  19.   public Collection<Group> getChildren()  
  20.   {  
  21.     return children;  
  22.   }  
  23.   
  24. When I try to save a Group using my DAO I get the following exception:  
  25.   
  26. org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred calling getter of com.trace.model.Group.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.trace.model.Group.id  
  27. Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.trace.model.Group.id  
  28.         at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)  
  29. ....  
  30. ....  
  31. ....  
  32. Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class  
  33.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  34.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  
  35.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  36.         at java.lang.reflect.Method.invoke(Method.java:597)  
  37.         at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)  
  38.   
  39. Any help or hints would be greatly appreciated.  Thanks in advance.  
下面是他的解决方案:
java 代码
 
  1. Alright, I figured it out.  I can't believe I wasted hours on this.    
  2.   
  3. The issue was that my DAO had a method called getChildren( Long ) which took an ID of a group and would return the children of the group with that ID.  The code was:    
  4.   
  5. DetachedCriteria crit = DetachedCriteria.forClass( Group.class );  
  6. crit.add( Restrictions.eq( "parent", id ) );  
  7.   
  8. The getParent() method of Group returns a Group but I was attempting to compare it against a Long.  
简单来说,就是parent和id类型不匹配。但是从错误的堆栈上面,很难看出是这样的错误。主要是经过hibernate封装。抛出是org.hibernate.PropertyAccessException: IllegalArgumentException错误,也就是说本来预期是某某类型,结果发现传入的不是某某类型。但是在写代码的时候编译器并不能捕捉到此错误。因为:
java 代码
  1. SimpleExpression org.hibernate.criterion.Restrictions.eq(String propertyName, Object value)  
后面是一个object,只会程序运行时才能够做这样的检查。也许它应该抛出这样的错。castclass错误,或者说预期什么参数,结果得到什么参数。这样在定位错误的时候就会更加容易一些。
分享到:
评论
1 楼 wendaoke 2008-01-24  
我也遇到这样的情况,但是后来直接写sql,就没有出现这样的问题。

c.add(Expression.in("areaId", areaIdStrs));
c.add(Expression.eq("type", agentType));

去掉了c.add(Expression.in("areaId", areaIdStrs))就可以了,没找哪里有错,后来就直接写了sql,就可以了。

相关推荐

Global site tag (gtag.js) - Google Analytics