简单的hibernate sql语句查询

  1. 对象查询

            

public User findUserByIdUser(String id) {

// TODO Auto-generated method stub

String sql="select * from T_USER t where t.ID='"+id+"'";

SQLQuery sqlQuery = this.getSession().createSQLQuery(sql).addEntity(User.class);

return (User) sqlQuery.uniqueResult();

}

2.对象集合查询

        

public List<User> findUserByTrade(String id) {

// TODO Auto-generated method stub

   final String sql="select * from T_USER t where t.trade_id='"+id+"'";

return this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {

@Override

public List<User> doInHibernate(Session session)

throws HibernateException, SQLException {

SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);

return query.list();

}

});

}


你可能感兴趣的:(简单的hibernate sql语句查询)