SQL查询与MySQL

前端之家收集整理的这篇文章主要介绍了SQL查询与MySQL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Jesey在Java(JAX-RS)中实现Restful Web Service.我在Tomcat v7.0上运行它我使用Hibernate将数据映射到数据库(MysqL).我有一个查询获取可交付成果列表:
deliverables = (List<Deliverable>) session.createQuery("Select deliverable from
                Task as t where t.project.id= :id And t.user.username = :name")
    .setLong("id",projectId).setString("name",username).list();

它给了我一百个可交付成果,但我希望在Task表中有TOP三.我感谢任何帮助.我该如何修改查询

请注意,这三个可交付成果不应重复.

与@FGreg的答案存在冲突.当我使用这些查询时,一切正常:

deliverables = (List<Deliverable>) session.createQuery(
    "from Deliverable as d where d.project.id= :id").setLong("id",projectId).list();
    long task_id;
    for(Deliverable d: deliverables) {
        task_id =  (long) session.createQuery("Select Max(id) from Task as t where     
        t.deliverable.id = :id and t.user.username = :name")
        .setLong("id",d.getId()).setString("name",username).uniqueResult();
         d.setTask_id(task_id);
    }
    sortDeliverable(deliverables);   // sort by Task_id

我认为在@FGreg提供的查询中,不考虑max(id),例如,task_id = 31,643使用了两次可交付成果.可能会认为Task_id = 31,这是错误的.有任何帮助来解决它吗?

我试过没有明显的@FGreg查询.然后它除了重复问题以外工作正常.

我试过这个:

deliverables = (List<Deliverable>) session.createQuery("from Deliverable as d inner join 
 Task as t where t.id = (select max(t1.id) from Task as t1 where t1.deliverable.id = d.id)   
 and d.project.id= :id and t.user.username = :name order by t.id desc")
                    .setMaxResults(3)
                    .setLong("id",projectId)
                    .setString("name",username)
                    .list();

我在堆栈跟踪中得到了New Error:

Jul 13,2012 5:03:10 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR:  Path expected for join!
Jul 13,2012 5:03:10 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR:  Path expected for join!
 Path expected for join!
    at org.hibernate.hql.internal.ast.HqlsqlWalker.createFromJoinElement(HqlsqlWalker.java:371)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.joinElement(HqlsqlBaseWalker.java:3477)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.fromElement(HqlsqlBaseWalker.java:3263)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.fromElementList(HqlsqlBaseWalker.java:3141)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.fromClause(HqlsqlBaseWalker.java:694)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.query(HqlsqlBaseWalker.java:550)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.selectStatement(HqlsqlBaseWalker.java:287)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.statement(HqlsqlBaseWalker.java:235)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:350)
    at $Proxy65.createQuery(Unknown Source)
    at se.softwerk.timelog.controller.DeliverableManager.deliverableList2(DeliverableManager.java:185)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponSEOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Jul 13,2012 5:03:10 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR:  Invalid path: 't.id'
Jul 13,2012 5:03:10 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR:  Invalid path: 't.id'
 Invalid path: 't.id'
    at org.hibernate.hql.internal.ast.util.LiteralProcessor.lookupConstant(LiteralProcessor.java:131)
    at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:219)
    at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:118)
    at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114)
    at org.hibernate.hql.internal.ast.HqlsqlWalker.resolve(HqlsqlWalker.java:883)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.expr(HqlsqlBaseWalker.java:1246)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.exprOrSubquery(HqlsqlBaseWalker.java:4252)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.comparisonExpr(HqlsqlBaseWalker.java:3730)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.logicalExpr(HqlsqlBaseWalker.java:1923)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.logicalExpr(HqlsqlBaseWalker.java:1848)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.logicalExpr(HqlsqlBaseWalker.java:1848)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.whereClause(HqlsqlBaseWalker.java:782)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.query(HqlsqlBaseWalker.java:583)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.selectStatement(HqlsqlBaseWalker.java:287)
    at org.hibernate.hql.internal.antlr.HqlsqlBaseWalker.statement(HqlsqlBaseWalker.java:235)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:350)
    at $Proxy65.createQuery(Unknown Source)
    at se.softwerk.timelog.controller.DeliverableManager.deliverableList2(DeliverableManager.java:185)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

模型类:

@Entity
public class Task {

    @Id
    @GeneratedValue
    private long id;

    @ManyToOne(optional = false)
    private User user;

    @ManyToOne(optional = false)
    private Project project;


    @ManyToOne(optional = false)
    private Deliverable deliverable;
}

并且可交付:

@Entity
public class Deliverable {

    @Id
    @GeneratedValue
    private long id;

    @Column(name = "short",nullable = false)
    private String key;

    @Column(nullable = false)
    private String name;

    @OneToMany(targetEntity = Task.class,mappedBy = "deliverable",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    @JsonIgnore
    private Collection<Task> tasks;

    @ManyToOne(optional = false,fetch = FetchType.EAGER)
    @JoinColumn(name = "project")
    private Project project;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "parent")
    private Deliverable parent;

    @OneToMany(targetEntity = Deliverable.class,mappedBy = "parent",cascade = CascadeType.ALL)
    @JsonIgnore
    private Collection<Deliverable> children;
}

解决方法

我想你要求按ID排序的前3个可交付成果.你可以尝试这样的事情:

编辑:好的,我会再多拿一次.这应该为您提供由Task.id订购的前3个可交付物,仅获取与max相关的Deliverable(Task.id)

deliverables = 
        (List<Deliverable>) session.createQuery(
            "from Deliverable as d,Task as t
                where t.deliverable.id = d.id and
                t.id = (select max(t1.id) from Task t1 where t1.deliverable.id = d.id)
                and d.project.id= :id
                and t.user.username = :name
                order by t.id desc")
        .setMaxResults(3)
        .setLong("id",projectId)
        .setString("name",username)
        .list();

猜你在找的MsSQL相关文章