⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nativequeryexample.java

📁 Hibernate Doc Tut .You will learn how to use and create a simple Hibernate examples.
💻 JAVA
字号:
package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.criterion.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 * @author Deepak Kumar
 * 
 * http://www.roseindia.net Hibernate Native Query Example
 *  
 */
public class NativeQueryExample {
	public static void main(String[] args) {
		Session session = null;

		try{
			// This step will read hibernate.cfg.xml and prepare hibernate for use
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session =sessionFactory.openSession();
			/* Hibernate Native Query Average Examle*/
		 	String sql ="select stddev(ins.invested_amount) as stdErr, "+
		 		" avg(ins.invested_amount) as mean "+
		 		" from insurance ins";
		 	Query query = session.createSQLQuery(sql).addScalar("stdErr",Hibernate.DOUBLE).
		 		addScalar("mean",Hibernate.DOUBLE);
	 		//Double [] amount = (Double []) query.uniqueResult(); 
		 	Object [] amount = (Object []) query.uniqueResult(); 
 			System.out.println("mean amount: " + amount[0]);
 			System.out.println("stdErr amount: " + amount[1]);

 			/* Example to show Native query to select all the objects from database */
 			/* Selecting all the objects from insurance table */
 			List insurance = session.createSQLQuery("select  {ins.*}  from insurance ins")
			.addEntity("ins", Insurance.class)
		    .list();
			for (Iterator it = insurance.iterator(); it.hasNext();) {
				Insurance insuranceObject = (Insurance) it.next();
				System.out.println("ID: " + insuranceObject.getLngInsuranceId());
				System.out.println("Name: " + insuranceObject.getInsuranceName());
			}
			
	        session.close();
		}catch(Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -