我只是查询User表中的userId,但是hibernate的查询日志却是下面这些from User u WHERE 1=1  and u.userId=3
Hibernate: 
    select
        user0_.userId as userId240_,
        user0_.addition as addition240_,
        user0_.address as address240_,
        user0_.college as college240_,
        user0_.email as email240_,
        user0_.gender as gender240_,
        user0_.lastLoginTime as lastLogi7_240_,
        user0_.nicknam as nicknam240_,
        user0_.passWord as passWord240_,
        user0_.privilege as privilege240_,
        user0_.registerTime as registe11_240_,
        user0_.telephone as telephone240_,
        user0_.userName as userName240_ 
    from
        user_info_table user0_ 
    where
        1=1 
        and user0_.userId=3
Hibernate: 
    select
        pk0_.userId as userId240_3_,
        pk0_.tId as tId3_,
        pk0_.tId as tId253_2_,
        pk0_.addition as addition253_2_,
        pk0_.task as task253_2_,
        pk0_.sdId as sdId253_2_,
        pk0_.userId as userId253_2_,
        taskhistor1_.sd_id as sd1_246_0_,
        taskhistor1_.addition as addition246_0_,
        taskhistor1_.city as city246_0_,
        taskhistor1_.cityBaseCount as cityBase4_246_0_,
        taskhistor1_.userId as userId246_0_,
        taskhistor1_.sdTask as sdTask246_0_,
        taskhistor1_.sdocCount as sdocCount246_0_,
        taskhistor1_.sdocLimitTime as sdocLimi7_246_0_,
        taskhistor1_.sdvpLimitTime as sdvpLimi8_246_0_,
        taskhistor1_.ssbLimitTime as ssbLimit9_246_0_,
        taskhistor1_.state as state246_0_,
        taskhistor1_.szTime as szTime246_0_,
        user2_.userId as userId240_1_,
        user2_.addition as addition240_1_,
        user2_.address as address240_1_,
        user2_.college as college240_1_,
        user2_.email as email240_1_,
        user2_.gender as gender240_1_,
        user2_.lastLoginTime as lastLogi7_240_1_,
        user2_.nicknam as nicknam240_1_,
        user2_.passWord as passWord240_1_,
        user2_.privilege as privilege240_1_,
        user2_.registerTime as registe11_240_1_,
        user2_.telephone as telephone240_1_,
        user2_.userName as userName240_1_ 
    from
        people_task_table pk0_ 
    left outer join
        task_history_table taskhistor1_ 
            on pk0_.sdId=taskhistor1_.sd_id 
    left outer join
        user_info_table user2_ 
            on taskhistor1_.userId=user2_.userId 
    where
        pk0_.userId=?
10:52:32,326 TRACE BasicBinder:82 - binding parameter [1] as [INTEGER] - 3
Hibernate: 
    select
        pa0_.userId as userId240_3_,
        pa0_.paId as paId3_,
        pa0_.paId as paId254_2_,
        pa0_.addition as addition254_2_,
        pa0_.baseCount as baseCount254_2_,
        pa0_.salary as salary254_2_,
        pa0_.ssb as ssb254_2_,
        pa0_.subject as subject254_2_,
        pa0_.sdId as sdId254_2_,
        pa0_.userId as userId254_2_,
        taskhistor1_.sd_id as sd1_246_0_,
        taskhistor1_.addition as addition246_0_,
        taskhistor1_.city as city246_0_,
        taskhistor1_.cityBaseCount as cityBase4_246_0_,
        taskhistor1_.userId as userId246_0_,
        taskhistor1_.sdTask as sdTask246_0_,
        taskhistor1_.sdocCount as sdocCount246_0_,
        taskhistor1_.sdocLimitTime as sdocLimi7_246_0_,
        taskhistor1_.sdvpLimitTime as sdvpLimi8_246_0_,
        taskhistor1_.ssbLimitTime as ssbLimit9_246_0_,
        taskhistor1_.state as state246_0_,
        taskhistor1_.szTime as szTime246_0_,
        user2_.userId as userId240_1_,
        user2_.addition as addition240_1_,
        user2_.address as address240_1_,
        user2_.college as college240_1_,
        user2_.email as email240_1_,
        user2_.gender as gender240_1_,
        user2_.lastLoginTime as lastLogi7_240_1_,
        user2_.nicknam as nicknam240_1_,
        user2_.passWord as passWord240_1_,
        user2_.privilege as privilege240_1_,
        user2_.registerTime as registe11_240_1_,
        user2_.telephone as telephone240_1_,
        user2_.userName as userName240_1_ 
    from
        people_action_table pa0_ 
    left outer join
        task_history_table taskhistor1_ 
            on pa0_.sdId=taskhistor1_.sd_id 
    left outer join
        user_info_table user2_ 
            on taskhistor1_.userId=user2_.userId 
    where
        pa0_.userId=?
10:52:32,327 TRACE BasicBinder:82 - binding parameter [1] as [INTEGER] - 3
Hibernate: 
    select
        mkoccu0_.userId as userId240_4_,
        mkoccu0_.id as id4_,
        mkoccu0_.id as id245_3_,
        mkoccu0_.angDa as angDa245_3_,
        mkoccu0_.asus as asus245_3_,
        mkoccu0_.huaQing as huaQing245_3_,
        mkoccu0_.jiJia as jiJia245_3_,
        mkoccu0_.jinYing as jinYing245_3_,
        mkoccu0_.mId as mId245_3_,
        mkoccu0_.meiJie as meiJie245_3_,
        mkoccu0_.mingXuan as mingXuan245_3_,
        mkoccu0_.other as other245_3_,
        mkoccu0_.qch as qch245_3_,
        mkoccu0_.sbdk as sbdk245_3_,
        mkoccu0_.sdDate as sdDate245_3_,
        mkoccu0_.shuangMin as shuangMin245_3_,
        mkoccu0_.sdId as sdId245_3_,
        mkoccu0_.userId as userId245_3_,
        mkoccu0_.weiXing as weiXing245_3_,
        mkoccu0_.wholeNum as wholeNum245_3_,
        mkoccu0_.yingTai as yingTai245_3_,
        et1_.mId as mId241_0_,
        et1_.city as city241_0_,
        et1_.etName as etName241_0_,
        et1_.platform as platform241_0_,
        taskhistor2_.sd_id as sd1_246_1_,
        taskhistor2_.addition as addition246_1_,
        taskhistor2_.city as city246_1_,
        taskhistor2_.cityBaseCount as cityBase4_246_1_,
        taskhistor2_.userId as userId246_1_,
        taskhistor2_.sdTask as sdTask246_1_,
        taskhistor2_.sdocCount as sdocCount246_1_,
        taskhistor2_.sdocLimitTime as sdocLimi7_246_1_,
        taskhistor2_.sdvpLimitTime as sdvpLimi8_246_1_,
        taskhistor2_.ssbLimitTime as ssbLimit9_246_1_,
        taskhistor2_.state as state246_1_,
        taskhistor2_.szTime as szTime246_1_,
        user3_.userId as userId240_2_,
        user3_.addition as addition240_2_,
        user3_.address as address240_2_,
        user3_.college as college240_2_,
        user3_.email as email240_2_,
        user3_.gender as gender240_2_,
        user3_.lastLoginTime as lastLogi7_240_2_,
        user3_.nicknam as nicknam240_2_,
        user3_.passWord as passWord240_2_,
        user3_.privilege as privilege240_2_,
        user3_.registerTime as registe11_240_2_,
        user3_.telephone as telephone240_2_,
        user3_.userName as userName240_2_ 
    from
        etoccupancy_table mkoccu0_ 
    left outer join
        et_table et1_ 
            on mkoccu0_.mId=et1_.mId 
    left outer join
        task_history_table taskhistor2_ 
            on mkoccu0_.sdId=taskhistor2_.sd_id 
    left outer join
        user_info_table user3_ 
            on taskhistor2_.userId=user3_.userId 
    where
        mkoccu0_.userId=?
10:52:32,328 TRACE BasicBinder:82 - binding parameter [1] as [INTEGER] - 3
Hibernate: 
    select
        epcs0_.userId as userId240_5_,
        epcs0_.epcId as epcId5_,
        epcs0_.epcId as epcId248_4_,
        epcs0_.asusEpcNum as asusEpcNum248_4_,
        epcs0_.asusNum as asusNum248_4_,
        epcs0_.netBookNum as netBookNum248_4_,
        epcs0_.sdDate as sdDate248_4_,
        epcs0_.storeId as storeId248_4_,
        epcs0_.sdId as sdId248_4_,
        epcs0_.userId as userId248_4_,
        epcs0_.wholeNum as wholeNum248_4_,
        store1_.storeId as storeId242_0_,
        store1_.GLNum as GLNum242_0_,
        store1_.assistant as assistant242_0_,
        store1_.company as company242_0_,
        store1_.mId as mId242_0_,
        store1_.storeAddr as storeAddr242_0_,
        store1_.storeNum as storeNum242_0_,
        store1_.telephone as telephone242_0_,
        et2_.mId as mId241_1_,
        et2_.city as city241_1_,
        et2_.etName as etName241_1_,
        et2_.platform as platform241_1_,
        taskhistor3_.sd_id as sd1_246_2_,
        taskhistor3_.addition as addition246_2_,
        taskhistor3_.city as city246_2_,
        taskhistor3_.cityBaseCount as cityBase4_246_2_,
        taskhistor3_.userId as userId246_2_,
        taskhistor3_.sdTask as sdTask246_2_,
        taskhistor3_.sdocCount as sdocCount246_2_,
        taskhistor3_.sdocLimitTime as sdocLimi7_246_2_,
        taskhistor3_.sdvpLimitTime as sdvpLimi8_246_2_,
        taskhistor3_.ssbLimitTime as ssbLimit9_246_2_,
        taskhistor3_.state as state246_2_,
        taskhistor3_.szTime as szTime246_2_,
        user4_.userId as userId240_3_,
        user4_.addition as addition240_3_,
        user4_.address as address240_3_,
        user4_.college as college240_3_,
        user4_.email as email240_3_,
        user4_.gender as gender240_3_,
        user4_.lastLoginTime as lastLogi7_240_3_,
        user4_.nicknam as nicknam240_3_,
        user4_.passWord as passWord240_3_,
        user4_.privilege as privilege240_3_,
        user4_.registerTime as registe11_240_3_,
        user4_.telephone as telephone240_3_,
        user4_.userName as userName240_3_ 
    from
        laptop_epc_table epcs0_ 
    left outer join
        store_table store1_ 
            on epcs0_.storeId=store1_.storeId 
    left outer join
        et_table et2_ 
            on store1_.mId=et2_.mId 
    left outer join
        task_history_table taskhistor3_ 
            on epcs0_.sdId=taskhistor3_.sd_id 
    left outer join
        user_info_table user4_ 
            on taskhistor3_.userId=user4_.userId 
    where
        epcs0_.userId=?
10:52:32,330 TRACE BasicBinder:82 - binding parameter [1] as [INTEGER] - 3
。。User的userId是其他这里出现的表的外键。
我只要查询User表中的userId,谁能告诉我怎么办?

解决方案 »

  1.   

    你做了关联关系,而且设置了级别
    调一下,  而且改fetch="lazy"
      

  2.   

     纠正一下是级联关系。把cascade级别改一下
      

  3.   

    但是如果我设置fetch=lazy的话,gson就会报错,说session已关闭什么的
      

  4.   

    怎么加?这是我的beans.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.mr" />   <!--  
    <bean id="userDaoImp" class="com.mr.dao.UserDaoImp">
    <property name="sessionFactory" ref="sessionFactory">
    </property>
    </bean>
    <bean id="dppDaoImp" class="com.mr.dao.DPPDaoImp">
    <property name="sessionFactory" ref="sessionFactory">
    </property>
    </bean> <bean id="userService" class="com.mr.service.UserService">
    <property name="userDao" ref="userDaoImp">
    </property>
    <property name="dppDao" ref="dppDaoImp">
    </property>
    </bean>

      -->
    <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <value>classpath:jdbc.properties</value>
    </property>
    </bean> <bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <!--<property name="dataSource" ref="dataSource" /> -->
    <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    </bean> <bean id="txManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean> <tx:annotation-driven transaction-manager="txManager" />
        
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
         <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!--
    <bean name="dataAccessAction" class="com.mr.action.DataAccessAction">
      <property name="userService" ref="userService" />
        </bean> 
         --></beans>
    这是web.xml<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
      <display-name></display-name>
      <welcome-file-list>
        <welcome-file>pickTable.jsp</welcome-file>
        <welcome-file>viewdata.jsp</welcome-file>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    </web-app>
    谢谢了
      

  5.   

    其实我之前试过了,一直出现问题
    加了这个openSessionInView
    也没什么效果。不知道为什么,这问题拖的有一段时间了哎
      

  6.   

    web.xml原始配置:
     <!-- 过滤spring中对于hibernate的session关闭管理 -->
     <filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>
       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
      </filter-class>
     </filter>是 防止懒加载的情况下,session关闭时,抱错。
       对session  进行延时。
      

  7.   

    其实 懒加载是 在你查询的时候 先返回给你一个代理对象,并没有真正去查 数据库,当你真正需要这个数据的时候,才会去查数据库,这个时候 就会产生一个问题就是,到页面的时候才真正用到数据,这个时候session已经关闭,无法对数据进行查询。
      所以需要 opensessioninview 来将session 延长一段时间
      

  8.   

    楼主只查询userId就把lazy设置成true就不会把关联的数据查询出来了  但是要用到关联的表的数据 我建议楼主还是把lazy设置成false
      

  9.   

    问题是这样有些条目查的出来,但是有些就查不出来了,会报java.lang.IllegalStateException: circular reference error
      Offending field: user  Offending object: preserveType: false, type: class com.mr.orm.User, obj: com.mr.orm.User@1db8f8e
    com.google.gson.CircularReferenceException.createDetailedException(CircularReferenceException.java:43)
    com.google.gson.JsonSerializationVisitor.visitObjectField(JsonSerializationVisitor.java:117)
    com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:69)
    com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
    的错
      

  10.   

    直接 excuteSQLQuery不就完了 你只要个id 不需要对象么
      

  11.   

    OpenSessionInView加了,session close的问题也解决掉了,但是还是级联查询的我把
    cascade改成@OneToMany(mappedBy="user",cascade={CascadeType.REMOVE})还是没用,查询的时候仍旧是级联
      

  12.   

    开了级联查询了,改掉hibernate映射文件中的懒加载,应该是这样吧。
      

  13.   

    在web.xml里面加上这段代码,这样的话,如果开启了懒加载,就不会有问题了
    <!--启用延迟加载功能的领域对象给 Web 层  -->
    <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  14.   

    如果上面的方法还是不行。。,,那就手动的,,将你不需要的属性set 为 null 
      

  15.   

    晕,是查询userId的所有内容么??那你对应的映射关联不查的话就别用抓取,用fetch抓取就是了,hql语句没错,用createCriteria()查询吧..