Hibernate / JPA + Derby - SELECT语句在GROUP BY,ORDER BY或选择列表中有太多项

时间:2010-09-14 09:29:34

标签: java hibernate jpa derby

我使用Hibernate与Derby DB进行JPA DB映射。对于复杂的对象结构,我得到“org.apache.derby.client.am.SqlException:SELECT语句在GROUP BY,ORDER BY或选择列表中有太多项目”:

org.apache.derby.client.am.SqlException: SELECT statement has too many items in GROUP BY, ORDER BY or select list.
        org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
        org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
        org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
        org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
        org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
        org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
        org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
        org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
        org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
        org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
        org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
        org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
        org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:505)
        org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:423)
        org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
        org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1547)
        org.hibernate.loader.Loader.doQuery(Loader.java:673)
        org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
        org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
        org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
        org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
        org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
        org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
        org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
        org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
        org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
        org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
        org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
        org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
        org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:229)
        org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:120)
        org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
        org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
        org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
        org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
        org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:228)
        sun.reflect.GeneratedMethodAccessor376.invoke(Unknown Source)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        java.lang.reflect.Method.invoke(Unknown Source)
        org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
        $Proxy74.merge(Unknown Source)
        sun.reflect.GeneratedMethodAccessor376.invoke(Unknown Source)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        java.lang.reflect.Method.invoke(Unknown Source)
        org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
        $Proxy41.merge(Unknown Source)

有没有人遇到这个问题,如果解决方案是什么?我在考虑

  • 切换到MySQL
  • 懒洋洋地加载一些属性(fetch = FetchType.LAZY)
  • 重新设计数据库架构(现在可能很难,使用Hyperjaxb从XSD架构生成数据库,并且必须更改shemas)

你会选择哪种解决方案?您知道Derby对SQL语句有什么限制吗?

2 个答案:

答案 0 :(得分:2)

尝试使用show_sql=true doc进行调试 检查生成的SQL并尝试调试它。

答案 1 :(得分:2)

我相信Derby在GROUP BY子句中允许大约32,000个列。 ORDER BY和SELECT列表中命名的列有类似的限制。