org.hibernate.PropertyAccessException: IllegalArgumentException(已解决)

在做课设的时候:

出现异常:org.hibernate.PropertyAccessException: IllegalArgumentException,搞了好半天。原来是Restrictions.eq()中的第二个参数出问题了,下面是另一个人写的。和我的错误时一样的

 

看看Exception信息:

  1. org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.lssrc.pams.model.User.userId
  2.     ......
  3. Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
  4.     ......

我是用Criteria的方式,按用户查找任务信息。下面是我的代码:

User.java

  1. @Entity
  2. @Table(name = "pams_user")
  3. public class User extends BasePOJO {
  4.     private static final long serialVersionUID = 1L;
  5.     private String userId;
  6.     private String userName;
  7.     // getter, setter
  8. }

Task.java

  1. @Entity
  2. @Table(name = "pams_task")
  3. public class Task extends BasePOJO {
  4.     private static final long serialVersionUID = 1L;
  5.     private String taskId;
  6.     private String taskEndTime;
  7.     private User user;
  8.     // getter, setter
  9. }

TaskDAOImpl.java

  1. @SuppressWarnings("unchecked")
  2. public List<Task> findByUser(User user, int beginIndex, int count) {
  3.     try {
  4.         session = getSessionFactory().getCurrentSession();
  5.         transaction = session.beginTransaction();
  6.         tasks = session.createCriteria(Task.class).add(Restrictions.eq("userId", user.getUserId())).list();
  7.         transaction.commit();
  8.     } catch (Exception e) {
  9.         transaction.rollback();
  10.         e.printStackTrace();
  11.         return null;
  12.     }
  13.     return tasks;
  14. }

反复测试之后,其实是下面这句话的原因:

  1. tasks = session.createCriteria(Task.class).add(Restrictions.eq("userId", user.getUserId())).list();

原因在于Restrictions.eq("userId", user.getUserId())这个方法中,匹配的并不是像sql写的那样,某个字段等于某个值,在这里,而是要看Task.java中user对象的类型。因为在Task.java中user是User类型的,所以,这个地方并不需要getUserId()。要修改成:

  1. tasks = session.createCriteria(Task.class).add(Restrictions.eq("userId", user)).list();

修改之后,又会出现新的错误:

  1. org.hibernate.QueryException: could not resolve property: userId of: com.lssrc.pams.model.Task 

这个问题,还是Restrictions.eq("userId", user)的问题,userId,应该改成user。因为在Task.java中,User对象的名字是user。再测试时,终于通过了。。。
Hibernate学的还是不够精细。做事还需谨慎认真。切记!

org.mvel2.PropertyAccessException: [Error: null pointer: xsetz.indexOf('.')] [Near : {... xsetz.indexOf('.') > 0 ....}] ^ [Line: 1, Column: 1] at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:450) at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:163) at org.mvel2.optimizers.dynamic.DynamicOptimizer.optimizeAccessor(DynamicOptimizer.java:80) at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159) at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115) at org.mvel2.ast.BinaryOperation.getReducedValueAccelerated(BinaryOperation.java:125) at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85) at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123) at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119) at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113) at org.mvel2.MVEL.executeExpression(MVEL.java:929) at com.creating.services.mdruleexecute.MdruleMain.lambda$runMdResult$0(MdruleMain.java:85) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NullPointerException: null at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:383) ... 14 common frames omitted
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值