
spring和hibernate整合问题
<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/Hib...
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.cp.domain.sys.User" table="user" catalog="cp" lazy="false">
<id name="userNo" type="java.lang.String">
<column name="userNo" length="50" />
<generator class="assigned" />
</id>
<many-to-one name="power" class="com.cp.domain.sys.Power" fetch="select">
<column name="powerid" length="50" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="50" />
</property>
<property name="passwd" type="java.lang.String">
<column name="passwd" length="50" />
</property>
<set name="flowsForChouid" inverse="true">
<key>
<column name="chouid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
<set name="flowsForFuid" inverse="true">
<key>
<column name="fuid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
<set name="flowsForAddid" inverse="true">
<key>
<column name="addid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
<set name="flowsForTiaoid" inverse="true">
<key>
<column name="tiaoid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
</class>
</hibernate-mapping>
DAO中的保存用户方法继承HibernateDaoSupport
public void saveUser(User user) {
this.getHibernateTemplate().save(user);
}
测试的方法继承TestCase
public void test() {
User user= new User();
user.setUserNo("a004");
user.setName("刘云争");
user.setPasswd("it'sme");
BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext-*.xml");
UserDao userDao = (UserDao)bf.getBean("userDao");
userDao.saveUser(user);
}
问题:原来在配置文件中没有设置lazy="false"的时候,既不能查也不能添加。设置后调用按ID查找的方法的时候能查出来,但是还是添加不上,也不报错Hibernate也发SQL语句,但就是添加不上。不用spring的时候,自己写的sessionFaction能查能添,感觉原因就在User的配置文件里,怎么也找不到!~
文件太长了,下面是applicationContext.xml
http://hi.baidu.com/newstude/blog/item/5c10e05bfb5abad29c8204a4.html 展开
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.cp.domain.sys.User" table="user" catalog="cp" lazy="false">
<id name="userNo" type="java.lang.String">
<column name="userNo" length="50" />
<generator class="assigned" />
</id>
<many-to-one name="power" class="com.cp.domain.sys.Power" fetch="select">
<column name="powerid" length="50" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="50" />
</property>
<property name="passwd" type="java.lang.String">
<column name="passwd" length="50" />
</property>
<set name="flowsForChouid" inverse="true">
<key>
<column name="chouid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
<set name="flowsForFuid" inverse="true">
<key>
<column name="fuid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
<set name="flowsForAddid" inverse="true">
<key>
<column name="addid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
<set name="flowsForTiaoid" inverse="true">
<key>
<column name="tiaoid" length="50" />
</key>
<one-to-many class="com.cp.domain.inventory.Flow" />
</set>
</class>
</hibernate-mapping>
DAO中的保存用户方法继承HibernateDaoSupport
public void saveUser(User user) {
this.getHibernateTemplate().save(user);
}
测试的方法继承TestCase
public void test() {
User user= new User();
user.setUserNo("a004");
user.setName("刘云争");
user.setPasswd("it'sme");
BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext-*.xml");
UserDao userDao = (UserDao)bf.getBean("userDao");
userDao.saveUser(user);
}
问题:原来在配置文件中没有设置lazy="false"的时候,既不能查也不能添加。设置后调用按ID查找的方法的时候能查出来,但是还是添加不上,也不报错Hibernate也发SQL语句,但就是添加不上。不用spring的时候,自己写的sessionFaction能查能添,感觉原因就在User的配置文件里,怎么也找不到!~
文件太长了,下面是applicationContext.xml
http://hi.baidu.com/newstude/blog/item/5c10e05bfb5abad29c8204a4.html 展开
展开全部
唉,,这个问题我碰过。。注意:使用lazy="false",之后
问题:原来在配置文件中没有设置lazy="false"的时候,既不能查也不能添加。设置后调用按ID查找的方法的时候能查出来,但是还是添加不上,也不报错Hibernate也发SQL语句,但就是添加不上。不用spring的时候,自己写的sessionFaction能查能添,感觉原因就在User的配置文件里,怎么也找不到!~
---->出现这样的问题正常。
由于hibernate是面向对象的
1。你的那个ID无效,如果那个ID是user表的,save时需要已实体方式
2.Spring配置文件有误,。不用spring的时候,自己写的sessionFaction能查能添,感觉原因就在User的配置文件里,怎么也找不到!~
估计是spring事务配置有误!!
操作时不发出sql语句,为什么?
需要配置事务管理。。。
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!-- 扫描bean的方式 -->
<context:component-scan base-package="com.javabbs"></context:component-scan>
</beans>
<?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: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.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
- <!--
连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}" />
- <!--
连接池的最大值 -->
<property name="maxActive" value="${maxActive}" />
- <!--
最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}" />
- <!--
最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}" />
</bean>
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
问题:原来在配置文件中没有设置lazy="false"的时候,既不能查也不能添加。设置后调用按ID查找的方法的时候能查出来,但是还是添加不上,也不报错Hibernate也发SQL语句,但就是添加不上。不用spring的时候,自己写的sessionFaction能查能添,感觉原因就在User的配置文件里,怎么也找不到!~
---->出现这样的问题正常。
由于hibernate是面向对象的
1。你的那个ID无效,如果那个ID是user表的,save时需要已实体方式
2.Spring配置文件有误,。不用spring的时候,自己写的sessionFaction能查能添,感觉原因就在User的配置文件里,怎么也找不到!~
估计是spring事务配置有误!!
操作时不发出sql语句,为什么?
需要配置事务管理。。。
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!-- 扫描bean的方式 -->
<context:component-scan base-package="com.javabbs"></context:component-scan>
</beans>
<?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: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.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
- <!--
连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}" />
- <!--
连接池的最大值 -->
<property name="maxActive" value="${maxActive}" />
- <!--
最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}" />
- <!--
最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}" />
</bean>
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
展开全部
在spring的配置文件中.sessionfactory下的属性中加上hibernate的映射文件.
<property name="mappingResources">
<list>
<value>*/User.hbm.xml</value>
</list>
</property>
也可以直接引用hibernate的配置.
<property name="mappingResources">
<list>
<value>*/User.hbm.xml</value>
</list>
</property>
也可以直接引用hibernate的配置.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sessionFactory 的配置不知道你在spring中怎么配的,可能是sessionFactory给spring管理的时候你配置错了。即为你配置了两个或多个sessionFactory使用不同的session
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看看事务配置好没有,“也不报错Hibernate也发SQL语句”,很可以是没有提交当前事务造成的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询