解决方案 »

  1.   

    你忘了执行HQL了 一般createQuery 后边还有 什么 list()  un..... exe....这三个 是执行语句的动作
      

  2.   

    其实我之前也也加过.list()的,还是没用,可能之后修改的时候忘记加这句了。能帮我再看看配置什么的是不是有问题了呢
      

  3.   

    @Test
    public void saveTest() {
    Session s = sessionFactory.openSession();
    for (int i = 0; i < 100; i++) {
    Tuser t = new Tuser(i + "");
    s.save(t);
    }
    s.close();
    } @Test
    public void getTest() {
    String mobilePhone = "1";
    Session s = sessionFactory.openSession();
    Tuser t = (Tuser) s.createQuery("from Tuser t where t.mobilePhone=?")
    .setParameter(0, mobilePhone).uniqueResult(); logger.info(t);
    logger.info(json(t)); s.close(); }
    Hibernate: 
        select
            tuser0_.ID as ID0_,
            tuser0_.MOBILEPHONE as MOBILEPH2_0_ 
        from
            TUSER tuser0_ 
        where
            tuser0_.MOBILEPHONE=?
    [test.UserDaoImplTest]entity.Tuser@22976eda
    [test.UserDaoImplTest]{"id":3,"mobilePhone":"1"}
    这怎么会出不来呢,我这就建了一个Dao就出来了呀。
      

  4.   

    你的 Hibernate是4.1.4的吗?那我再试试看。还有哦,给HQL语句赋值怎么是setParameter呢,怎么不是setString呢?
      

  5.   

    如果后面的out都没执行的话,那可能是个Error,而不是个Exception,暂时用捕获一下Throwable试试。
      

  6.   

    我终于把这个问题解决了,原来是Hibernate4的不同,需要配置hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext,没配置这个的话getcurrentsession方法不会去用当前session,所以需要配置这个东西去绑定到springcontext中去用,这个是用JUnit工具测试出来的,真tm好用啊
      

  7.   

    请问你是怎么解决的呢,我把 hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext 加了还是不行,这是我的applicationCountext.xml 代码:<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.hirisun.xx.sshTest" />
    <!-- 数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
    <property name="username" value="workUser " />
    <property name="password" value="work" />
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    </props>
    </property>
    <property name="packagesToScan">
    <list>
    <value>com.hirisun.xx.sshTest.bean</value>
    </list>
    </property>
    </bean> <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>帮我看看问题出那了,谢谢了。