从你说的问题上看出来你可能对hibernate了解还是不深刻.
 
首先给你说下Session实例创建过程
1 Configuration config = new Configuration().configure()
2 SessionFactory sessionFactory = config.buildSessionFactory()
3 Session session = sessionFactory.openSession()然后现在你用Spring进行AOP 所以这些都是互相引用reference就好了(在xml文件中进行配置)
出现上述问题就是你配置hibernate一些property时出错了,自己好好检查一下!要参考的话可以给你.

解决方案 »

  1.   

    我给你个我们原来项目,仅供参考
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
    <!-- <value>oracle.jdbc.driver.OracleDriver</value>-->
    <value>net.sourceforge.jtds.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:jtds:sqlserver://10.108.47.110:1433/sysdb</value>
    <!-- <value>jdbc:oracle:thin:@192.168.1.254:1521:sysdb</value> -->
    </property>
    <property name="username">
    <value>student</value>
    </property>
    <property name="password">
    <value>student</value>
    </property>
    </bean> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
    destroy-method="close">
    <property name="dataSource">
    <ref local="dataSource" />
    </property>
    <property name="mappingResources">
    <list>
    <value>com/iss/isscrm/entity/Accounts.hbm.xml</value>
    <value>com/iss/isscrm/entity/Accountsandrole.hbm.xml</value>
    <value>com/iss/isscrm/entity/Menu.hbm.xml</value>
    <value>com/iss/isscrm/entity/Role.hbm.xml</value>
    <value>com/iss/isscrm/entity/Roleandmenu.hbm.xml</value>
    <value>com/iss/isscrm/entity/Student.hbm.xml</value>
    <value>com/iss/isscrm/entity/Enterprise.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.SQLServerDialect
    </prop>
    <!-- 每次运行自动创建表 -->
    <!-- <prop key="hibernate.hbm2ddl.auto">create</prop>-->
    <!-- 解决hql中文查询乱码问题 -->
    <prop key="hibernate.query.factory_class">
    org.hibernate.hql.classic.ClassicQueryTranslatorFactory
    </prop>
    </props>
    </property>
    </bean> <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    </bean> <bean id="IBaseDaoImpl" class="com.iss.isscrm.dao.IBaseDaoImpl">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    </bean> <bean id="IBaseDaoImplProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="transactionManager" />
    </property>
    <property name="proxyInterfaces">
    <list>
    <value>com.iss.isscrm.dao.IBaseDao</value>
    </list>
    </property>
    <property name="target">
    <ref local="IBaseDaoImpl" />
    </property>
    <property name="transactionAttributes">
    <props> <prop key="insertObject*">PROPAGATION_REQUIRED</prop>
    <prop key="deleteObject*">PROPAGATION_REQUIRED</prop>
    <prop key="updateObject*">PROPAGATION_REQUIRED</prop>
    <prop key="getObject*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
      

  2.   

    我那里还是有问题的。但是很奇怪,我自己做的一个登录页面中的sessionFactory = null的。(dao中有setter的)
    但是能够正常查询。
    下面是我那个问题程序的XML<?xml version="1.0" encoding="GB2312"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean name="/showAction" class="com.leonguo.struts.action.ShowAction">
    <property name="showBo" ref="showBo">
    </property>
    </bean>

    <bean id="showBo" class="com.leonguo.bo.ShowBo">
    <property name="showDao">
    <ref bean="showDaoProxy"/> 
    </property>
    </bean>

    <bean id="showDao" class="com.leonguo.dao.ShowDaoImpl">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

    <bean id="dataSourse"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
    </property>
    <property name="url"
    value="jdbc:microsoft:sqlserver://localhost:1433">
    </property>
    <property name="username" value="sa"></property>
    <property name="password" value="sa"></property>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
    <property name="dataSource">
    <ref bean="dataSourse" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>com/leonguo/entity/Department.hbm.xml</value>
    <value>com/leonguo/entity/Emp.hbm.xml</value>
    <value>com/leonguo/entity/Mession.hbm.xml</value></list>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
       <ref local="sessionFactory" />
      </property>
    </bean>

    <bean id="showDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
      <property name="target">
       <ref local="showDao" />
      </property>
      <property name="transactionAttributes">
       <props>
         <prop key="save*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
                    <prop key="add*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
                    <prop key="modify*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
       </props>
      </property>
     </bean>
    </beans>然后是Action:
    private ShowBo showBo = null; public ShowBo getShowBo() {
    return showBo;
    } public void setShowBo(ShowBo showBo) {
    this.showBo = showBo;
    } public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) { ShowActionForm actionForm = (ShowActionForm) form;
    try {
    showBo.setDatasToForm(actionForm);
    return mapping.findForward("success");
    } catch (Exception e) {
    System.out.println(e.toString());
    return mapping.findForward("default");
    } }
      

  3.   

    我那里还是有问题的。但是很奇怪,我自己做的一个登录页面中的sessionFactory = null的。(dao中有setter的)
    但是能够正常查询。
    下面是我那个问题程序的XML<?xml version="1.0" encoding="GB2312"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean name="/showAction" class="com.leonguo.struts.action.ShowAction">
    <property name="showBo" ref="showBo">
    </property>
    </bean>

    <bean id="showBo" class="com.leonguo.bo.ShowBo">
    <property name="showDao">
    <ref bean="showDaoProxy"/> 
    </property>
    </bean>

    <bean id="showDao" class="com.leonguo.dao.ShowDaoImpl">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

    <bean id="dataSourse"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
    </property>
    <property name="url"
    value="jdbc:microsoft:sqlserver://localhost:1433">
    </property>
    <property name="username" value="sa"></property>
    <property name="password" value="sa"></property>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
    <property name="dataSource">
    <ref bean="dataSourse" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>com/leonguo/entity/Department.hbm.xml</value>
    <value>com/leonguo/entity/Emp.hbm.xml</value>
    <value>com/leonguo/entity/Mession.hbm.xml</value></list>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
       <ref local="sessionFactory" />
      </property>
    </bean>

    <bean id="showDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
      <property name="target">
       <ref local="showDao" />
      </property>
      <property name="transactionAttributes">
       <props>
         <prop key="save*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
                    <prop key="add*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
                    <prop key="modify*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
       </props>
      </property>
     </bean>
    </beans>然后是Action:
    private ShowBo showBo = null; public ShowBo getShowBo() {
    return showBo;
    } public void setShowBo(ShowBo showBo) {
    this.showBo = showBo;
    } public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) { ShowActionForm actionForm = (ShowActionForm) form;
    try {
    showBo.setDatasToForm(actionForm);
    return mapping.findForward("success");
    } catch (Exception e) {
    System.out.println(e.toString());
    return mapping.findForward("default");
    } }
      

  4.   


    不好意思上面发重复了
    bo中
    private ShowDao showDao = null; public ShowDao getShowDao() {
    return showDao;
    } public void setShowDao(ShowDao showDao) {
    this.showDao = showDao;
    }

    public ShowBo(){

    }
    @SuppressWarnings("unchecked")

    public void setDatasToForm(ShowActionForm actionForm){
    String strHql = "FROM com.leonguo.entity.Emp";
    List lstResult = showDao.getDatas(strHql);

    System.out.println("strHql 执行完成!Bo");

    List<ResultData> lstDatas = new ArrayList<ResultData>();
    for (int i = 0; i < lstResult.size(); i++) {
    Emp empData = new Emp();
    empData = (Emp) lstResult.get(i);
    Department departData = new Department();
    departData = empData.getDepartment(); ResultData resultData = new ResultData();
    resultData.setEmpId(empData.getEmpId());
    resultData.setEmpName(empData.getEmpName());
    resultData.setDepartmentName(departData.getDepartmentName()); lstDatas.add(resultData);
    } actionForm.setLstDatas(lstDatas);
    }
      

  5.   

    daoImpl中:
    private static final Logger logger = Logger.getLogger(ShowDaoImpl.class); @SuppressWarnings("unused") 
    private SessionFactory sessionFactory = null; public void setSessionFactory(SessionFactory sessionFactory) {
    if (logger.isDebugEnabled()) {
    logger.debug("setSessionFactory(SessionFactory) - start");
    } this.sessionFactory = sessionFactory; if (logger.isDebugEnabled()) {
    logger.debug("setSessionFactory(SessionFactory) - end");
    }
    } @SuppressWarnings("unchecked")
    public List getDatas(String strHql) {

    if (logger.isDebugEnabled()) {
    logger.debug("getDatas(String) - start");
    } System.out.println( "strHql ====== " + strHql );
    List lstResult = new ArrayList();
    try{
    lstResult = this.getHibernateTemplate().find(strHql);
    System.out.println("HQL语句执行完毕 DaoImpl"); if (logger.isDebugEnabled()) {
    logger.debug("getDatas(String) - end");


    //lstResult = sessionFactory.getCurrentSession().createQuery(strHql).list();

    return lstResult;

    }
    catch(Exception e){
    logger.error("getDatas(String)", e); System.out.println(e.toString()); if (logger.isDebugEnabled()) {
    logger.debug("getDatas(String) - end");
    }
    return null;
    }
      

  6.   

    最后是log4j的错误信息:INFO - JDBC driver: SQLServer, version: 2.2.0040
    INFO - Using dialect: org.hibernate.dialect.SQLServerDialect
    INFO - Using default transaction strategy (direct JDBC transactions)
    INFO - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    INFO - Automatic flush during beforeCompletion(): disabled
    INFO - Automatic session close at end of transaction: disabled
    INFO - Scrollable result sets: enabled
    INFO - JDBC3 getGeneratedKeys(): disabled
    INFO - Connection release mode: on_close
    INFO - Default batch fetch size: 1
    INFO - Generate SQL with comments: disabled
    INFO - Order SQL updates by primary key: disabled
    INFO - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    INFO - Using ASTQueryTranslatorFactory
    INFO - Query language substitutions: {}
    INFO - Second-level cache: enabled
    INFO - Query cache: disabled
    INFO - Cache provider: org.hibernate.cache.EhCacheProvider
    INFO - Optimize cache for minimal puts: disabled
    INFO - Structured second-level cache entries: disabled
    INFO - Echoing all SQL to stdout
    INFO - Statistics: disabled
    INFO - Deleted entity synthetic identifier rollback: disabled
    INFO - Default entity-mode: pojo
    INFO - building session factory
    WARN - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/apache-tomcat-5.5.23/webapps/sample_3/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
    ERROR - CGLIB Enhancement failed: com.leonguo.entity.Department
    java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
    at net.sf.cglib.core.ClassEmitter.begin_class(ClassEmitter.java:77)
    at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:173)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)。
    WARN - could not create proxy factory for:com.leonguo.entity.Department
    org.hibernate.HibernateException: CGLIB Enhancement failed: com.leonguo.entity.Department
    at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:132)
    at org.hibernate.proxy.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:41)
    at org.hibernate.tuple.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:161)
    at org.hibernate.tuple.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:131)。
    Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
    at net.sf.cglib.core.ClassEmitter.begin_class(ClassEmitter.java:77)
    at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:173)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:116)
    ... 89 more
    ERROR - CGLIB Enhancement failed: com.leonguo.entity.Emp
    java.lang.NoClassDefFoundError
    at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:116)
    at org.hibernate.proxy.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:41)
    at org.hibernate.tuple.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:161)
    at org.hibernate.tuple.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:131)
    at org.hibernate.tuple.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
    at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:64)
    at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:257)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
    at org.hibernate.persister.PersisterFactory.createClassPers

    WARN - could not create proxy factory for:com.leonguo.entity.Mession
    org.hibernate.HibernateException: CGLIB Enhancement failed: com.leonguo.entity.Mession
    at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:132)
    at org.hibernate.proxy.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:41)
    at org.hibernate.tuple.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:161)
    at org.hibernate.tuple.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:131)
    at org.hibernate.tuple.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
    at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:64)
    at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:257)。
    Caused by: java.lang.NoClassDefFoundError
    at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:116)
    ... 89 more
    INFO - Not binding factory to JNDI, no JNDI name configured
    INFO - Using DataSource [org.apache.commons.dbcp.BasicDataSource@1d46259] of Hibernate SessionFactory for HibernateTransactionManager
    INFO - Root WebApplicationContext: initialization completed in 9875 ms
    INFO - Initializing composable request processor for module prefix ''
    INFO - Initialize action of type: org.springframework.web.struts.DelegatingActionProxy
    ERROR - getDatas(String)
    java.lang.NullPointerException
    at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
    at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
    at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
    差不多是这样的
      

  7.   

    挖,你的错误是在太多了,建议一下你还是找本书,把上面的源码搞下来,慢慢研究,这样才能更好的理解!不过你上面的问题,从异常来看!
    1 没有+驱动包
    2 你的数据库方言(dialect)也没有配置
    3 你的SessionFactory可能根本没有创建,或者你在创建SessionFactory中没有将起获取!愿君好运...
      

  8.   

    嗯,问题已经解决了,是在Spring中必须要把用到的表的关联表实体全都反转出来。在Web.xml中添加Hiernate的
    延迟加载,就可以解了
      

  9.   

    你好  我想问下怎么在Spring中sessionFactory 里配置 表的关联表的反转呢