title: How to get data from database table and update that data into another table in hibernate? I have done it in JSP but i want to do in Hibernate
body: <p>I am developing an application and i want to get data from a table and want to update another table from that data. I have done it in JSP but now i just started to work on hibernate so i want to do it in hibernate.</p>
  
  <p>I have 2 tables post_table and user_table in my database. I am fetching data from post_table and want to update user_table from that data. Here is my <strong>JSP</strong> code..</p>
  
  <pre><code>String sql = "SELECT uid,SUM(value) AS SumByUID FROM post_table group by uid;"
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery(sql);
  while(rs.next()){
  String mySum = rs.getString("SumByUID");
  String u_id = rs.getString("uid");
  
  String sql1 = "update user_table set rank='"+mySum+"' where uid='"+u_id+"'";
  PreparedStatement pst = con.prepareStatement(sql1);
  pst.executeUpdate();
  }
  </code></pre>
  
  <p>I tried to do in hibernate and i fetched data from post_table. Here is my Entity class <strong>Post.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="post_table")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class Post implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue
  @Column(name="id")
  private long id;
  
  @Column(name="uid")
  private long userId;
  
  @Column(name="value")
  private long val;
  
  public long getId() {
      return id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  public long getUserId() {
      return userId;
  }
  
  public void setUserId(long userId) {
      this.userId = userId;
  }
  
  public long getVal() {
      return val;
  }
  
  public void setVal(long val) {
      this.val = val;
  }
  
  }
  </code></pre>
  
  <p>Here is my <strong>DAO</strong> class</p>
  
  <pre><code>@SuppressWarnings({ "unchecked", "rawtypes" })
  public List&lt;Post&gt; getPostList() throws Exception {
      session = sessionFactory.openSession();
      Criteria cr = session.createCriteria(Post.class).setResultTransformer(Transformers.aliasToBean(Result.class));
      ProjectionList projList = Projections.projectionList();
      projList.add(Projections.sum("val"), "topValue");
      projList.add(Projections.groupProperty("userId"), "uid");
      cr.setProjection(projList);
      List postList = cr.list();
      tx = session.getTransaction();
      session.beginTransaction();
      tx.commit();
      return postList;
  }
  </code></pre>
  
  <p>I fetched data from post_table and set the result in <strong>Result.java</strong>. Here is my Reslut.java</p>
  
  <pre><code>@Entity
  @Table(name="user_table")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class Result implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue
  @Column(name="id")
  private long id;
  
  @Column(name="rank")
  private long topValue;
  
  private long uid;
  
  
  public long getTopValue() {
      return topValue;
  }
  public void setTopValue(long topValue) {
      this.topValue = topValue;
  }
  
  public long getUid() {
      return uid;
  }
  public void setUid(long uid) {
      this.uid = uid;
  }
  public long getId() {
      return id;
  }
  public void setId(long id) {
      this.id = id;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And here is my <strong>Controller</strong></p>
  
  <pre><code>@RequestMapping(value = "/posts", method = RequestMethod.GET)
  public @ResponseBody
  List&lt;Post&gt; getEmployee() {
  
      List&lt;Post&gt; postList = null;
      try {
          postList = profileService.getPostList();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      return postList;
  }
  </code></pre>
  
  <p>Please help me because i am unable to update user_table with that data.</p>