Native SQL in Hibernate
public class NativeQueryExample {
public static void main(String[] args) {
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
String sql =" select a.name as name, "+
" sum(a.amount) as total "+
" from Account a";
Query query = session.createSQLQuery(sql)
.addScalar("name",Hibernate.String)
.addScalar("total",Hibernate.DOUBLE);
Object [] data = (Object [])query.uniqueResult();
System.out.println("name : " + data[0]);
System.out.println("amount : " + data[1]);
session.close();
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
No comments:
Post a Comment