Hibernate 不同数据库的连接及SQL方言 
<!--MySql 驱动程序 eg. mysql-connector-java-5.0.4-bin.jar-->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  <!-- JDBC URL -->
  <property name="connection.url">jdbc:mysql://localhost/dbname?characterEncoding=gb2312</property>  <!-- 数据库用户名-->
  <property name="connection.username">root</property>  <!-- 数据库密码-->
  <property name="connection.password">root</property>
  
  
  <!--Sql Server 驱动程序 eg. jtds-1.2.jar-->
  <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>  <!-- JDBC URL -->
  <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname</property>  <!-- 数据库用户名-->
  <property name="connection.username">sa</property>  <!-- 数据库密码-->
  <property name="connection.password"></property>  
  
  <!--Oracle 驱动程序 ojdbc14.jar-->
  <property name="dialect">org.hibernate.dialect.OracleDialect</property>
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  <!-- JDBC URL -->
  <property name="connection.url">jdbc:oracle:thin:@localhost:1521:dbname</property>  <!-- 数据库用户名-->
  <property name="connection.username">test</property>  <!-- 数据库密码-->
  <property name="connection.password">test</property>如果出现如下错误,则可能是Hibernate SQL方言 (hibernate.dialect)设置不正确。
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]'last_insert_id' 不是可以识别的 函数名。

解决方案 »

  1.   

    统计函数查询
    可以在HQL中使用函数,经常使用的函数如下:
    count():统计记录条数。
    min():求最小值。
    max():求最大值。
    sum():求和。
    avg():求平均值。
    例如,要取得Student实例的数量,可以编写如下HQL语句:
    select count(*) from Student
    取得Student平均年龄的HQL语句:
    select avg(s.age) from Student as s
    可以使用distinct去除重复的数据:
    select distinct s.age from Student as s
      

  2.   

    上面的精神可嘉可不是我要的结果.我要的是如何配置及用什么语句对其进行不用数据库中两表的查询操作,有代码的SHOW一把.你是第一个,回答我问题的也很积极,所以得给分.
      

  3.   

    Hibernate 可以实现分页查询,例如: 
    从第2万条开始取出100条记录 [code:1]Query q = session.createQuery("from Cat as c"); 
    q.setFirstResult(20000); 
    q.setMaxResults(100); 
    List l = q.list();[/code:1] 
      

  4.   


    你试试在Hibernate中就你那样可以吗?来点儿实践的让俺开开眼,你上面的那我也知道.
      

  5.   

    至于第一个问题,只要配置多个连接池就行了。很容易实现的。就像2楼所说的那样。至于得到总记录数,方法有很多。不过最好用,最简单的方法就是用Query这个类的QueryList方法。他返回一个List集合,你只要得到List的大小就知道总记录条数了。List.size()就可以得到你想要的数据
      

  6.   

    Query orderList=sess.createQuery("select count(*) from OrderForm");
    return ((Integer)orderList.iterate().next()).longValue();或者
    return ((Integer)orderList.uniqueResult()).intValue();
      

  7.   

    统计函数查询 
    可以在HQL中使用函数,经常使用的函数如下: 
    count():统计记录条数。 
    min():求最小值。 
    max():求最大值。 
    sum():求和。 
    avg():求平均值。 
    例如,要取得Student实例的数量,可以编写如下HQL语句: 
    select count(*) from Student 
    取得Student平均年龄的HQL语句: 
    select avg(s.age) from Student as s 
    可以使用distinct去除重复的数据: 
    select distinct s.age from Student as s
      

  8.   

    count 用法:public int countOrderHistory(long startTime, long endTime)
    throws HibernateException {
    String method = "countOrderHistory";
    Session session = null; try {
    String sql = "SELECT COUNT(*) FROM order_history as oh JOIN meal as m ON oh.muid = m.muid WHERE oh.expired = "
    + Constant.ORDERHISTORY_EXPIRED
    + " and oh.created_date >= "
    + startTime
    + " and oh.created_date <=" + endTime; session = HibernateUtil.currentSession(); SQLQuery q = session.createSQLQuery(sql); List list = q.list(); return ((BigInteger) list.get(0)).intValue(); } catch (HibernateException e) {
    error("", method, "hibernate exception", e);
    throw e;
    }
    }
      

  9.   

    第一个问题看二楼
    第二个
    SELECT count(*) FROM Emp 可以得到总行数 
      

  10.   


    你说的是这样的吧.
    Query query=session.createQuery(" select count(*) from map.manytoone.Student");
    query.executeUpdate();
    你试试可以吗?
      

  11.   

    不好意思,有可能我没有说清楚,我再表达一下我的意思.希望大家能给出我想要的答案.1.如何使用Hibernate进行不同数据库的连接?(最好能给出进行连接的配置或代码). 
    比如:select * from 数据库1.数据表1 where 数据库1.数据表1.userID=数据库二.数据表2.usernameID;
    上面我是经SQL的语句表达出来了。
    我想如果用HQL表达如何表达出来?2.在Hibernate中如何统计总记录数.用类似的 count(*)实现怎么作?(必须使用HQL语句就一个统计操作有那些方法). -----------------------------------------
    等待中………………
      

  12.   


    我用的:
    Query query=session.createQuery(" select count(*) from map.manytoone.Student");
    query.executeUpdate();
    出现如下情况:
    --------------------------------------------------------------------------------------org.hibernate.HibernateException: Not supported for select queries
    at org.hibernate.hql.ast.QueryTranslatorImpl.errorIfSelect(QueryTranslatorImpl.java:260)
    at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:321)
    at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1019)
    at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)
    at map.manytoone.manytooneTest.testCountExam(manytooneTest.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      

  13.   

    Query query=session.createQuery(" select count(*) from map.manytoone.Student"); 
    query.uniqueResult(); 
    !可以
    或者
    Query query=session.createQuery(" from map.manytoone.Student"); 
    List list=query.list();
    return list.size(); 
      

  14.   

    query.executeUpdate(); 返回的是更新或者删除所影响的行数!
      

  15.   

    hibernate的count(*)最容易了.直接返回list.size()就可以了
      

  16.   


    我的代码如下:
    ------------------------------------------------------
    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.SQLQuery;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import java.util.*;
    public class manytooneTest extends TestCase{

    Session session=null;
    static SessionFactory sessionFactory=null;
    static {
    try {
    Configuration config = new Configuration().configure();
    sessionFactory=config.buildSessionFactory(); 
    } catch(HibernateException e) {
    e.printStackTrace();
    }
    }
    public void testCountExam(){
    Session s = HibernateUtil.currentSession();
    SQLQuery q = s.createSQLQuery(" select count(*) from map.manytoone.Student");
    List str=q.list();
                    //如何得到统计数量
    }
    }
      

  17.   

    ---------------------------
    List list=query.list(); 
    return list.size();
    这个可以得出一个统计的数量。那query.uniqueResult(),你是如何得到的。是Integer n=query.uniqueResult();可我试了不可以呀。那query.executeUpdate();是返回的进行添,删,改所影响的数量。那仅仅查询我怎么得不到呢。麻烦你再看看是不是这样的,再回复的详细点儿。
    谢谢你了。我会给你分的.
      

  18.   


    这样有点浪费了。Query q = session.createQuery("select count(*) from table");
    int count = ((Integer)q.uniqueResult()).intValue();
      

  19.   

    你是说的用hibernate多表查询吗?
    我有个方法你可以试试~~
    /**
    *@param pid 是个外键
      
    **/
    public ScrollableResults findAllcount(int pid)//多表查询
        { 
            try {
             String queryString ="select 表1.*,表2.* from 表1,表2  where 表1.outkey=表2.id and 表1.outkey=?";
             Session g=getSession();
     
                Query queryObject =g.createQuery(queryString);
          queryObject.setParameter(0, pid);
           //g.close();
         return queryObject.scroll();
       
             } catch (RuntimeException re) {
                log.error("find by property name failed", re);
                throw re;
             }
        }这个方法查询语句和SQL语句就一样了,只不过表名用的是类名这个你应该知道吧 导入一下import org.hibernate.ScrollableResults;这个包就行了