如何从数据库表中获取数据并将该数据更新到hibernate中的另一个表中?我在JSP中完成了它,但我想在Hibernate

时间:2015-07-23 07:17:50

标签: java mysql spring hibernate jsp

我正在开发一个应用程序,我想从表中获取数据,并希望从该数据更新另一个表。我已经在JSP中完成了它,但现在我刚刚开始使用hibernate,所以我想在hibernate中完成它。

我的数据库中有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();
}

我试着在hibernate中做,我从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。

1 个答案:

答案 0 :(得分:0)

尝试下面的代码快照,我希望它有帮助:)

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