Hibernate和Lambdaj问题与分组

时间:2015-04-22 13:17:09

标签: java hibernate lambdaj

我想使用Lambdaj从查询结果集执行树分组。结果来自Hibernate。

我可以成功进行聚合,但代码在分组时失败:

List<MyClass> list = myDao.findByProperty(value);

Integer intAvg = avg(list, on(MyClass.class).getIntProperty()); //OK

Group<MyClass> group = group(list, on(MyClass.class).getGroupingStringProperty()); //FAIL

例外是

ch.lambdaj.util.IntrospectionException: java.lang.NoSuchMethodException: org.example.entityMyClass.-2147483644()
    at ch.lambdaj.util.IntrospectionUtil.getPlainPropertyValue(IntrospectionUtil.java:61)
    at ch.lambdaj.util.IntrospectionUtil.getBooleanPropertyValue(IntrospectionUtil.java:53)
    at ch.lambdaj.util.IntrospectionUtil.getPropertyValue(IntrospectionUtil.java:45)
    at ch.lambdaj.group.StringGroupCondition.getGroupValue(StringGroupCondition.java:41)
    at ch.lambdaj.group.GroupImpl.addItem(GroupImpl.java:28)
    at ch.lambdaj.group.Groups.group(Groups.java:58)
    at ch.lambdaj.group.Groups.group(Groups.java:46)
    at ch.lambdaj.Lambda.group(Lambda.java:1237)
    at org.example.package.Class.method(Class.java:128)
    ...............
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:190)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: org.example.entityMyClass.-2147483644()
    at java.lang.Class.getMethod(Class.java:1665)
    at ch.lambdaj.util.IntrospectionUtil.getPlainPropertyValue(IntrospectionUtil.java:59)
    ... 91 more

我试着做一些功课,但现在还不成功。

Lambdaj使用cglib执行代理。常春藤文件提供cglib-nodep.jar包。我们的架构项目功能(因为依赖于Hibernate)cglib标准版本和asm-all。请注意,由于公司政策,我们的Ivy依赖文件不允许传递依赖,依赖性由开发团队根据项目的实际需求决定(因此,如果Hibernate需要某个版本的cglib,我们将这种依赖放入Ivy并将所有内容标记为不可传递的。)

所以在我的项目中我有<dependency org="com.googlecode.lambdaj" name="lambdaj" rev="2.3.3"/>(默认传递为true)和我的项目所依赖的架构项目

    <dependency org="org.ow2.asm"                      name="asm-all"                         rev="4.0"            transitive="false"/>
    <dependency org="org.hibernate"                    name="hibernate-core"                  rev="4.1.6.Final"    transitive="false"/>
    <dependency org="org.hibernate"                    name="hibernate-ehcache"               rev="4.1.6.Final"    transitive="false"/>
    <dependency org="org.hibernate"                    name="hibernate-validator"             rev="4.3.0.Final"    transitive="false"/>
    <dependency org="org.hibernate.javax.persistence"  name="hibernate-jpa-2.0-api"           rev="1.0.1.Final"    transitive="false"/>

(以及更多)

问题是:我在代码中做错了吗?这是Hibernate的问题吗?

1 个答案:

答案 0 :(得分:1)

琐碎的错误

必须调用API而不是

Group<MyClass> group = group(list, on(MyClass.class).getGroupingStringProperty());

而是

Group<MyClass> group = group(list, by(on(MyClass.class).getGroupingStringProperty()));

注意group ... by语法