奇葩问题。 hibernates 无法查询到懒加载的关联数据。

闪客 发布于 2013/05/16 09:36
阅读 1K+
收藏 0

项目采用 hib-4.2.0  +  spring3.2.2 +springMVC

常规配置。结果单端多对一无法查询到懒加载的一端数据,必须采用lazy false才能获取到。

直接上配置,各位看官麻烦指点下。首次遇到。

web.XML:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 配置log4j -->
<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>webApp.root</param-value>
</context-param>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>6000</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- dispatcher servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 编码过滤器 -->
<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
</init-param>
<init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>hibernateOpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
    <param-name>flushMode</param-name>
    <param-value>AUTO</param-value>
</init-param>
</filter>

<filter-mapping>
    <filter-name>hibernateOpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- SiteMesh -->
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

applicationContext.xml:

<!-- 配置c3p0连接池数据源信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/zg_cms?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="55555"/>
<!-- 
<property name="minPoolSize" value="2"/>
<property name="maxPoolSize" value="15"/>
<property name="initialPoolSize" value="3"/>
<property name="maxIdleTime" value="600"/>
<property name="acquireIncrement" value="3"/>
<property name="maxStatements" value="0"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="acquireRetryAttempts" value="30"/>
<property name="breakAfterAcquireFailure" value="false"/>
<property name="testConnectionOnCheckout" value="false"/> -->
</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.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:/com/zg/entities/xml/</value>
</list>
</property>
</bean>
<!-- 注解方式配置springMVC映射器和适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!-- 线程安全访问Session -->
<property name="synchronizeOnSession" value="true"/>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
        <value>text/plain;charset=UTF-8</value>
      </list>
      </property>
</bean> 
</list>
</property>
</bean>

<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.zg"/>
<mvc:annotation-driven/>

hib关联映射

Answer.hbm.xml:

<many-to-one name="backUser" class="com.zg.entities.BackUser" fetch="select">
            <column name="FK_ASSE_BACK_USER_ID" />
</many-to-one>


结果查询不到 backUser 数据

写了个测试。截图如下。


加载中
0
子夜歌
同求   一样的问题   后边怎么解决的?
0
w
wangketao

真恶心,放了一坨代码在这

返回顶部
顶部