跪求高手解決spring+hibernate異常,分不是問題,困擾了1天了。謝謝 你applicationContext-common.xml里的sessionFactory是怎么配置的? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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> 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> 首先要在在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> 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> org.hibernate.MappingException: could not instantiate id generator看起来似乎是hbm.xml映射文件里某个主键的配置不对 映射文件我是用工具自動生成的,是這樣設置的:<id name="Id" type="integer" column="f_goodsid" > <generator class="sequence"/> </id> <id name="Id" type="integer" column="f_goodsid"> <generator class="sequence"> <param name="sequence">seq_id</param> </generator></id>还要你的seq_id你在数据库里已经创建了这个sequence 配置文件里没有configLocation它啊? 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已經配置了。。 晕,你用的是sqlserver,sqlserver是没有序列的,你的主键是自动增长的吗,如果是可以<id name="Id" type="integer" column="f_goodsid"> <generator class="native" /></id> 或者<generator class="increment" /> 解決了,感謝各位高手,是因為<generator class="sequence"> 設置錯誤了。。我還有個問題,native跟sequence有什么區別,請高手簡單介紹下! sequence(序列)采用数据库提供的sequence 机制生成主键。如Oralce 中的Sequence。返回的标识符是long, short或者 int类型的。native由Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式。 謝謝。現在是沒出異常了,不過就是數據添加不到數據庫中。不知道什么原因,我才剛接觸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_jeanswestflexstore11:05:19,140 WARN JDBCExceptionReporter:54 - SQL Warning: 5701, SQLState: 0100011: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_english11:05:19,156 WARN JDBCExceptionReporter:54 - SQL Warning: 5703, SQLState: 0100011: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() hibernate联合主键序列化问题,急!!!! dao 设计相关,急 求助struts连接mysql数据库???? Mybatis传入参数问题 紧急求助!!!! 在JbuilderX下运行STRUTS应用,总是有错 誰有JBX開發Struts的示例嗎? 如果已经写好了webservice函数和接口,如何写客户端java文件(有main函数)测试? 大家有谁用过jbuilder7自带的ejb sample ESiteWL??怎么安装?来者有分 hql 语句不明白的 请推荐一本学习xml的书(java)! 关于log4j 在j2ee项目中 的应用问题
<!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>
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>
<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>
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>
看起来似乎是hbm.xml映射文件里某个主键的配置不对
<id
name="Id"
type="integer"
column="f_goodsid"
>
<generator class="sequence"/>
</id>
<generator class="sequence">
<param name="sequence">seq_id</param>
</generator>
</id>还要你的seq_id你在数据库里已经创建了这个sequence
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
configLocation已經配置了。。
<id name="Id" type="integer" column="f_goodsid">
<generator class="native" />
</id> 或者
<generator class="increment" />
我還有個問題,native跟sequence有什么區別,請高手簡單介紹下!
采用数据库提供的sequence 机制生成主键。如Oralce 中的Sequence。返回的标识符是long, short或者 int类型的。
native
由Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式。
下面是控制臺輸出信息:
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()