org.hibernate.QueryException:该集合未被引用

时间:2013-12-11 07:38:33

标签: spring hibernate-mapping

请找到java类xml文件。  package com.example;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHibernateFilter {
    @SuppressWarnings("unchecked")
    public static void main(String... strings) {
        ApplicationContext context = new ClassPathXmlApplicationContext("sravan.xml");
        SessionFactory factory = context.getBean(SessionFactory.class);
        Session session = factory.openSession();
        Family family = (Family)session.get(Family.class, 1);
        //Set<Person> persons = family.getMembers();
        //System.out.println("persons "+persons);
        List<Person> entityList = session.createFilter(family.getMembers(), "where id > :id").setInteger("id", 0).list();
        List<String> entityNames = session.createFilter(entityList, "select firstName").list();
        System.out.println("names are "+entityNames);
    }
}

<?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:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config />   
    <context:component-scan base-package="com.example" />

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver" />
        <property name="url" value="jdbc:mysql://localhost/sravan"/>
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value="com.example"></property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>             
                <prop key="hibernate.current_session_context_class">thread</prop>               
                <prop key="show_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>             
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
</beans>

如果我运行main方法得到 org.hibernate.QueryException:该集合未引用异常。我错过任何配置?并向我解释异常

0 个答案:

没有答案