从别处copy过来的,和我的错一样
java 代码
- 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:
-
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- public Long getId()
- {
- return id;
- }
-
- @ManyToOne
- @JoinColumn(name="group_fk", nullable=true, insertable=false, updatable=false)
- public Group getParent()
- {
- return parent;
- }
-
- @OneToMany(cascade = {CascadeType.ALL}, fetch=FetchType.LAZY)
- @JoinColumn(name="group_fk")
- public Collection<Group> getChildren()
- {
- return children;
- }
-
- When I try to save a Group using my DAO I get the following exception:
-
- 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
- Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.trace.model.Group.id
- at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
- ....
- ....
- ....
- Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
- 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$BasicGetter.get(BasicPropertyAccessor.java:145)
-
- Any help or hints would be greatly appreciated. Thanks in advance.
下面是他的解决方案:
java 代码
- Alright, I figured it out. I can't believe I wasted hours on this.
-
- 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:
-
- DetachedCriteria crit = DetachedCriteria.forClass( Group.class );
- crit.add( Restrictions.eq( "parent", id ) );
-
- 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 代码
- SimpleExpression org.hibernate.criterion.Restrictions.eq(String propertyName, Object value)
后面是一个object,只会程序运行时才能够做这样的检查。也许它应该抛出这样的错。castclass错误,或者说预期什么参数,结果得到什么参数。这样在定位错误的时候就会更加容易一些。
分享到:
相关推荐
NULL 博文链接:https://listen-raining.iteye.com/blog/2087518
Java源码非法访问异常IllegalArgumentException
链接is unknown to this NavController
java.lang.IllegalArgumentException Cannot format given Object as a Date
NULL 博文链接:https://denghuijia1314521-gmail-com.iteye.com/blog/1341258
NULL 博文链接:https://xienan123.iteye.com/blog/1042124
主要介绍了IDEA 中使用 ECJ 编译出现 java.lang.IllegalArgumentException问题 ,本文内容简短给大家介绍的好,需要的朋友可以参考下
Caused by: java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(ImageIO.java:1388) at com.pleanwar.fiying.FlyingObject.loadImage(FlyingObject.java:52) at ...
NULL 博文链接:https://wxb-j2ee.iteye.com/blog/1183736
Java demo源码 398.java.lang.IllegalArgumentException- 398.java.lang.IllegalArgumentException 复现bug demo 源码
IllegalArgumentException, IllegalAccessException, InvocationTargetException{ String paramType = m.getParameterTypes()[0].getName(); paramType = paramType.substring(paramType.lastIndexOf(".")+1); ...
解决viewpager图片放大和缩小时出现的 java.lang.IllegalArgumentException: pointerIndex out of range问题的解决方法。
U10316025_HW8_14_4 如果输入的值小于或等于 0,则让 Loan 类抛出 IllegalArgumentException
Java学习资料&项目源码&教程等资源
两天因为项目需要整合spring、struts2、mybatis三大框架,但启动的时候总出现这个错误,困扰我好久,折腾了好久终于找到问题根源,下面小编给大家分享下问题所在及解决办法,一起看看吧
### Cause: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName. at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:...
IllegalArgumentException, IllegalAccessException, InstantiationException { byte data[]=Util.readFile(filename); // 执行加密操作 byte encryptedClassData[] = getencryptData(data); // 保存加密后的...
SpringBoot 整合 MyBatis。解决因Mybatis-Spring版本过高引起的“Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required”异常
* @exception IllegalArgumentException * 当 is 为 null 时抛出此异常。 */ public static Document parseForDoc(final InputStream is) throws SAXException, IOException, ...