Advertisement
Guest User

Untitled

a guest
Nov 26th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package com.example.dao.dto;
  2.  
  3. import com.example.dao.impl.jpa.AbstractJpaDAO;
  4.  
  5. import javax.persistence.*;
  6. import java.io.Serializable;
  7.  
  8. @Entity
  9. @Table(name = "widgets")
  10. @NamedNativeQuery(name = "findTopVotedWidgetsByVisitor",
  11.                   query =
  12.                      "SELECT" +
  13.                      "   widgets.label_id, " +
  14.                      "   widgets.name, " +
  15.                      "   widgets.description " +
  16.                      "   VotesByWidgetandVisitor.numVotes as votes" +
  17.                      " FROM widgets" +
  18.                      "   JOIN (" +
  19.                      "     SELECT" +
  20.                      "       answers.label_id," +
  21.                      "       count(*) AS numVotes" +
  22.                      "     FROM votes" +
  23.                      "       JOIN answers ON votes.item_id = answers.answer_id" +
  24.                      "       JOIN questions ON answers.question_id = questions.question_id" +
  25.                      "     WHERE questions.event_id = :eventId" +
  26.                      "       AND votes.visitor_id = cast(trim(:visitorId) as uuid)" +
  27.                      "     GROUP BY answers.label_id, votes.visitor_id" +
  28.                      "   ) AS VotesByContribLabelAndVisitor ON widgets.label_id = VotesByWidgetAndVisitor.label_id" +
  29.                      " ORDER BY numVotes DESC",
  30.                   resultSetMapping = "foomap")
  31. @SqlResultSetMapping(name="foomap",
  32.                      entities=
  33.                         @EntityResult(entityClass=WidgetItem.class, fields={
  34.                            @FieldResult(name="id", column="label_id"),
  35.                            @FieldResult(name="name", column="name"),
  36.                            @FieldResult(name="description", column="description")}),
  37.                      columns={
  38.                         @ColumnResult(name="votes")}
  39. )
  40. public class WidgetItem extends AbstractJpaDAO implements Serializable {
  41.  
  42.   // Mapped
  43.   private long id;
  44.   private String name;
  45.   private String description;
  46.   private Long votes;
  47.  
  48.   @Id
  49.   @Column(name = "label_id")
  50.   public long getId () {
  51.     return id;
  52.   }
  53.  
  54.  
  55.   protected void setId (long id) {
  56.     this.id = id;
  57.   }
  58.  
  59.  
  60.  
  61.   public String getName () {
  62.     return name;
  63.   }
  64.  
  65.  
  66.   public void setName (String name) {
  67.     this.name = name;
  68.   }
  69.  
  70.   public String getDescription () {
  71.     return description;
  72.   }
  73.  
  74.  
  75.   public void setDescription (String description) {
  76.     this.description = description;
  77.   }
  78.  
  79.   @Embedded
  80.   public Long getVotes () {
  81.     return votes;
  82.   }
  83.  
  84.   public void setVotes(Long votes) {
  85.     this.votes = votes;
  86.   }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement