你applicationContext-common.xml里的sessionFactory是怎么配置的?

解决方案 »

  1.   

    hibernate.cfg.xml配置:<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration
        PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
    <session-factory> <!-- local connection properties -->
    <property name="hibernate.connection.url">
    jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=d_jeanswest;
    </property>
    <property name="hibernate.connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver
    </property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">123456</property>
    <!-- property name="hibernate.connection.pool_size"></property --> <!-- dialect for Microsoft SQL Server -->
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property> <property name="hibernate.show_sql">true</property>

    <property name="hibernate.current_session_context_class">thread</property>

    <mapping resource="com/jeanswest/model/TGoods.hbm.xml" />
    </session-factory></hibernate-configuration>
      

  2.   

    applicationContext-common.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: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-2.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    </bean>           

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="del*" propagation="REQUIRED"/>
    <tx:method name="modify*" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
    </tx:attributes>
    </tx:advice>

    <!-- 那些类的哪些方法参与事务 -->
    <aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution(* com.jeanswest.manager.*.*(..))"/>
    <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
    </aop:config>
    </beans>
      

  3.   

    首先要在在web.xml里配置:
      <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
      </context-param>
     
      <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    这样Tomcat在启动的时候才能读取你的applicationContext-common.xml,接着初始化sessionFactory。
    而且applicationContext-common.xml里要这样配置:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    </bean>
      

  4.   

    applicationContext-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: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-2.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


    <bean id="goodsManager" class="com.jeanswest.manager.GoodsManagerImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="logManager" ref="logManager"/>
    </bean>
    <bean id="logManager" class="com.jeanswest.manager.LogManagerImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>



    </beans>
      

  5.   

    org.hibernate.MappingException: could not instantiate id generator
    看起来似乎是hbm.xml映射文件里某个主键的配置不对
      

  6.   

    映射文件我是用工具自動生成的,是這樣設置的:
    <id
    name="Id"
    type="integer"
    column="f_goodsid"
    >
    <generator class="sequence"/>
    </id>
      

  7.   

    <id name="Id" type="integer" column="f_goodsid">
       <generator class="sequence">
           <param name="sequence">seq_id</param>
       </generator>
    </id>还要你的seq_id你在数据库里已经创建了这个sequence
      

  8.   

    配置文件里没有configLocation它啊?
      

  9.   

            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
        <!-- 配置sessionFactory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>    
        </bean>    
    configLocation已經配置了。。
      

  10.   

    晕,你用的是sqlserver,sqlserver是没有序列的,你的主键是自动增长的吗,如果是可以
    <id name="Id" type="integer" column="f_goodsid">
      <generator class="native" />
    </id> 或者
    <generator class="increment" />
      

  11.   

    解決了,感謝各位高手,是因為<generator class="sequence"> 設置錯誤了。。
    我還有個問題,native跟sequence有什么區別,請高手簡單介紹下!
      

  12.   

    sequence(序列)
    采用数据库提供的sequence 机制生成主键。如Oralce 中的Sequence。返回的标识符是long, short或者 int类型的。
    native
    由Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式。
      

  13.   

    謝謝。現在是沒出異常了,不過就是數據添加不到數據庫中。不知道什么原因,我才剛接觸hibernate
    下面是控制臺輸出信息:
    Hibernate: insert into t_goods (f_goodscode, f_goodsclass, f_goodsbrand, f_goodsname, f_etprice, f_saleprice, f_cut, f_serviceday, f_goodssize, f_goodscolor, f_goodsweight, f_goodsdescription, f_goodsissale, f_goodscount) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) select scope_identity()
    11:05:19,140  WARN JDBCExceptionReporter:54 - SQL Warning: 0, SQLState: 
    11:05:19,140  WARN JDBCExceptionReporter:55 - [Microsoft][SQLServer 2000 Driver for JDBC]Database changed to d_jeanswestflexstore
    11:05:19,140  WARN JDBCExceptionReporter:54 - SQL Warning: 5701, SQLState: 01000
    11:05:19,140  WARN JDBCExceptionReporter:55 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Changed database context to 'd_jeanswestflexstore'.
    11:05:19,140  WARN JDBCExceptionReporter:54 - SQL Warning: 0, SQLState: 
    11:05:19,140  WARN JDBCExceptionReporter:55 - [Microsoft][SQLServer 2000 Driver for JDBC]Language changed to us_english
    11:05:19,156  WARN JDBCExceptionReporter:54 - SQL Warning: 5703, SQLState: 01000
    11:05:19,156  WARN JDBCExceptionReporter:55 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Changed language setting to us_english.
    Hibernate: insert into t_log (type, detail, time) values (?, ?, ?) select scope_identity()