Hibernate按集合排序

时间:2011-12-01 14:43:04

标签: hibernate spring collections

我的“我的应用程序”中有以下类,

import javax.persistence.OrderBy;
    public class Role {
        private Set<RoleAccess> roleAccesses = new HashSet<RoleAccess>(0);
        @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="role")
        @OrderBy("action.actionName") // Error
        public Set<RoleAccess> getRoleAccesses() {
            return this.roleAccesses;
        }
        public void setRoleAccesses(Set<RoleAccess> roleAccesses) {
            this.roleAccesses = roleAccesses;
        }
    }

public class RoleAccess {

private Action action;
    @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="ActionId", nullable=false)
    public Action getAction() {
        return this.action;
    }
    public void setAction(Action action) {
        this.action = action;
    }


    private Role role;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="RoleId", nullable=false)
    public Role getRole() {
        return this.role;
    }
    public void setRole(Role role) {
        this.role = role;
    }

}


public class Action {

private Set<RoleAccess> roleAccesses = new HashSet<RoleAccess>(0);
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="action")
    public Set<RoleAccess> getRoleAccesses() {
        return this.roleAccesses;
    }
    public void setRoleAccesses(Set<RoleAccess> roleAccesses) {
        this.roleAccesses = roleAccesses;
    }

    private String actionName; // with getter/setter

}

我使用了Hibernate 3.5.2,当我调用role.getRoleAccesses()时会导致以下异常

org.hibernate.AnnotationException: property from @OrderBy clause not found: com.omnimd.pms.beans.RoleAccess.action.actionName
    org.hibernate.cfg.annotations.CollectionBinder.buildOrderByClauseFromHql(CollectionBinder.java:946)
    org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:710)
    org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:666)
    org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:619)
    org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
    org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1221)
    org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:383)
    org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)
    org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
    org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)

我尝试了另一种方式: 导入不同版本的@OrderBy

import org.hibernate.annotations.OrderBy;

并将@orderBy用作

@OrderBy(clause = "action.actionName")

然后我得到以下例外

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'action'.
    com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)
    com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
    com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:281)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    org.hibernate.loader.Loader.getResultSet(Loader.java:1849)
    org.hibernate.loader.Loader.doQuery(Loader.java:718)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    org.hibernate.loader.Loader.loadCollection(Loader.java:2062)
    org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
    org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:628)
    org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
    org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1853)
    org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:366)
    org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
    org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:366)
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:242)
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:173)
    javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:271)
    org.apache.jsp.WEB_002dINF.jsp.RoleAddEditForm_jsp._jspx_meth_c_005fforEach_005f0(RoleAddEditForm_jsp.java:410)
    org.apache.jsp.WEB_002dINF.jsp.RoleAddEditForm_jsp._jspx_meth_form_005fform_005f0(RoleAddEditForm_jsp.java:293)
    org.apache.jsp.WEB_002dINF.jsp.RoleAddEditForm_jsp._jspService(RoleAddEditForm_jsp.java:228)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

我正在使用Spring HibernateTemplate进行休眠。

可能是什么解决方案?

2 个答案:

答案 0 :(得分:0)

首先:我在actionName课程中缺少action属性 - 但我认为您刚删除它以使帖子更短。

第二:我不认为可以按属性订购,属于Lazy加载引用(action.actionName)。

您可以尝试将Lazy的获取类型更改为Eger,但我不知道这是否有效。

答案 1 :(得分:0)

你的问题似乎就在这里

 @OrderBy("action.actionName") // Error
    public Set<RoleAccess> getRoleAccesses() {
        return this.roleAccesses;
    }

现在当它尝试应用order by子句时,它不知道要从集合中使用哪个对象,我认为多级顺序将与onetoone映射一起使用,但我可能错了。