我正在使用休眠条件从数据库中获取数据,现在我想将该数据插入到另一个表中。
这是我的 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;
}
这是我的帖子类
@Entity
@Table(name="post")
@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;
}
}
这是我的控制器
@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;
}
请建议我正确的方法来做到这一点我是休眠的新手