簡體   English   中英

如何從數據庫表中獲取數據並將數據更新到休眠中的另一個表中? 我已經在JSP中完成了,但是我想在Hibernate中完成

[英]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

我正在開發應用程序,我想從一個表中獲取數據,並希望從該數據中更新另一個表。 我已經在JSP中完成了它,但是現在我才開始在休眠狀態下工作,所以我想在休眠狀態下進行。

我的數據庫中有2個表post_table和user_table。 我正在從post_table獲取數據,並希望從該數據更新user_table。 這是我的JSP代碼。

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();
}

我試圖在休眠狀態下進行操作,並從post_table中獲取了數據。 這是我的實體類Post.java

@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;
}

}

這是我的DAO課程

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Post> 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;
}

我從post_table獲取數據,並將結果設置在Result.java中 這是我的Reslut.java

@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;
}

}

這是我的控制器

@RequestMapping(value = "/posts", method = RequestMethod.GET)
public @ResponseBody
List<Post> getEmployee() {

    List<Post> postList = null;
    try {
        postList = profileService.getPostList();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return postList;
}

請幫助我,因為我無法使用該數據更新user_table。

嘗試下面的代碼捕捉,希望對您有所幫助:)

@SuppressWarnings({ "unchecked", "rawtypes" })
  public List<Result> getPostList() throws Exception {
      session = sessionFactory.openSession();
      tx = session.beginTransaction();

      Criteria cr = session.createCriteria(Post.class);
      ProjectionList projList = Projections.projectionList();
      projList.add(Projections.sum("val"), "topValue");
      projList.add(Projections.groupProperty("userId"), "uid");
      cr.setProjection(projList);
      cr.setResultTransformer(Transformers.aliasToBean(Result.class));
      List<Result> postList = cr.list();
      // please make sure that you are using the class field name rather than database field name in below query.
      String updateHql = "update Result set topValue = :topValue where uid = :uid";
      Query query = null;
      for(Result result : postList){
        query=session.createQuery(updateHql);
        query.setLong("topValue", result.getTopValue());
        query.setLong("uid", result.getUid());
        query.executeUpdate();
        session.flush();
      }

      session.flush();
      tx.commit();
      return postList;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM