簡單的hibernate sql語句查詢

  1. 對象查詢sql

            

public User findUserByIdUser(String id) {session

// TODO Auto-generated method stubide

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

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

return (User) sqlQuery.uniqueResult();對象

}get

2.對象集合查詢it

        

public List<User> findUserByTrade(String id) {io

// TODO Auto-generated method stubclass

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

}

});

}

相關文章
相關標籤/搜索