欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Hibernate Native SQL Example

本文來(lái)自:http://hibernate.javabeat.net/tutorials/17-hibernate-native-sql-example.php

Native SQL is handwritten SQL for all database operations like create,update, delete and select. Hibernate Native Query also supports storedprocedures. Hibernate allows you to run Native SQL Query for all the databaseoperations, so you can use your existing handwritten sql with Hibernate, thisalso helps you in migrating your SQL/JDBC based application to Hibernate.

In this example we will show you how you can use Native SQL with hibernate.You will learn how to use Native to calculate average and then in anotherexample select all the objects from table.

Here is the code of Hibernate Native SQL:

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.criterion.*;
import org.hibernate.cfg.*;
import java.util.*;
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 = (Insuranceit.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();
    }
    
  }
}

Following query is used to calculate the average of  invested amount:

/*Hibernate Native Query Average Examle*/

String sql ="selectstddev(ins.invested_amount) as stdErr, "+ " avg(ins.invested_amount) as mean"+ " from insurance ins";

The following code:

Query query = session.createSQLQuery(sql).addScalar("stdErr",Hibernate.DOUBLE).
addScalar("mean",Hibernate.DOUBLE);

Creates a new instance of SQLQuery for the given SQL query string and theentities returned by the query are detached. 

To return all the entities from database we have used the following query:

/* 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 insuranceins")
.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());
}

When you run the program through it should display the following result:

log4j:WARN No appenderscould be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select stddev(ins.invested_amount) as stdErr,avg(ins.invested_amount) as mean from insurance ins

mean amount: 592.1584

stdErr amount: 923.5714

Hibernate: select ins.ID as ID0_, ins.insurance_name asinsurance2_2_0_, ins.invested_amount as invested3_2_0_,ins.investement_date as investem4_2_0_ from insurance ins

ID: 1

Name: Car Insurance

ID: 2

Name: Life Insurance

ID: 3

Name: Life Insurance

ID: 4

Name: Car Insurance

......

.......

In this example you learned how to use Native Query with Hibernate.

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
hibernate緩存
hibernate中只選取某些列的寫(xiě)法
hibernate的緩存
Hibernate之緩存使用一
(轉)hibernate常用API詳解
Hibernate5.2之原生SQL查詢(xún)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久