2

Jeseyを使用してJava(JAX-RS)でRestfulWebサービスを実装しています。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();

それは私に百の成果物を与えています、しかし私はタスクテーブルでトップ3を持ちたいです。助けてくれてありがとう。クエリを変更するにはどうすればよいですか?

これらの3つの成果物は繰り返されるべきではないことに注意してください。

@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で2回使用されます。これは、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();

そして、スタックトレースで新しいエラーが発生しました:

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", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private Collection<Deliverable> children;
}
4

3 に答える 3

2

ID順で上位3Deliverableを求めていると思います。Taskあなたはこのようなことを試すことができます:

編集:わかりました、これでもう1つ刺します。これにより、に関連付けられているものだけを取得してDeliverable注文された上位3つが表示されます。Task.idDeliverablemax(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();
于 2012-07-11T21:09:42.750 に答える
2

mysqlにはTOPがありません。SQLの最後にORDERBY句とLIMIT句を使用してください。

于 2012-07-12T05:13:59.160 に答える
1

mysqlでTOP関数が使用できるかどうかはわかりませんが、それを使用して結果を共有してみてください。次のように書くことができます:SELECT TOP 2 * FROM Persons

このリンクを確認してください。詳細が表示されます。 ここにリンクの説明を入力してください

于 2012-07-12T03:41:22.353 に答える