java.lang.illegalargumentexception无法将java.lang.string字段设置为java.lang.String

时间:2016-08-11 11:44:44

标签: java spring hibernate exception junit

我正在删除一个小的spring和hibernate程序,我想从数据库中获取值的总和。为此我写下面的代码

public double getDataFromDb(String num) {

        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        double result=0;

        Query getSumquery=(Query)session.createQuery("select sum(amount) from User_Master where  =:num");
        getSumquery.setParameter("num",num);
        List list =getSumquery.list();
        result=(double) list.get(0);
        tx.commit();

这里我在列表行中遇到异常。从junit testcase运行时,Exception如下所示

org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.abc.process.hbmfiles.Number.num
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:35)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3596)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3312)
    at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:218)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:87)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
    at org.hibernate.loader.Loader.doQuery(Loader.java:673)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)
    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 com.creditprocess.daoimpl.EvaluationDAOImpl.getCrtTotal(EvaluationDAOImpl.java:294)
    at com.creditprocess.managerimpl.EvaluationManagerImpl.saveData(EvaluationManagerImpl.java:264)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy28.saveData(Unknown Source)
    at EvaluationTest.sendDetails(EvaluationTest.java:150)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.caja.creditprocess.hbmfiles.Doi_Master.dni to java.lang.String
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:393)
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:32)
    ... 62 more

这些是我的实体 的 User_Master.java

@Entity
@Table(name="User_Master")
public class User_Master implements Serializable{

@Id
@Column(name="User_master_id")
private String user_master_id;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="num")
private Number num;
}

Number.java

@Entity
@Table(name="Number")
public class Number implements Serializable{

@Id
@Column(name="num")
private String num;
}

2 个答案:

答案 0 :(得分:1)

如果您使用条件语句,那么您必须在其中之后定义一些条件,所以在这里我认为您是您的实体变量(num)参考的任务....

        public double getDataFromDb(String num) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        double result=0;

        Query getSumquery=(Query)session.createQuery("select sum(u.amount) from User_Master u where u.num  =:num");
        getSumquery.setParameter("num",num);
        List list =getSumquery.list();
        result=(double) list.get(0);
        tx.commit();
}

答案 1 :(得分:0)

在使用反射的同时,我也遇到了同样的问题,而我犯的错误不是写:

Arrays.asList(object.getClass().getDeclaredFields()).forEach(field -> {
    field.setAccessible(true);
        try {
            System.out.pringln(String.valueOf(field.get(object)));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    });

我写道:

Arrays.asList(object.getClass().getDeclaredFields()).forEach(field -> {
    field.setAccessible(true);
        try {
            System.out.pringln(String.valueOf(field.get(object.getField())));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    });

意思是说我试图在将其值作为参数的同时获取对象字段。