Spring+Hibernate 能查询,但不能保存数据!

wnhoo 2008-07-17 01:18:40
Spring+Hibernate 能查询,但不能保存数据!

单独用Hibernate可以查询、保存数据;但是如果采用Spring+Hibernate 的Spring Dao 方式是能查询,但是不能保存。

applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="GuestbookDAO" class="com.oneic.hibernate.GuestbookDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>

hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">wyt</property>
<property name="connection.url">
jdbc:oracle:thin:@192.168.1.188:1521:oneic
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">oneic</property>
<property name="connection.password">wyt</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<mapping resource="com/oneic/hibernate/Guestbook.hbm.xml" />
</session-factory>

</hibernate-configuration>

其中hbm、Dao操作都是myeclipse工具自动生成的!

测试代码
public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();
Guestbook g = new Guestbook();
g.setUsername("曹操");
g.setTitle("test spring and hibernate");
Transaction tx = session.beginTransaction();
session.save(g);
session.flush();
tx.commit();

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"/applicationContext.xml");
GuestbookDAO dao = (GuestbookDAO) ctx.getBean("GuestbookDAO");
Guestbook g2 = new Guestbook();
g2.setUsername("刘备");
g2.setTitle("test spring and hibernate");
dao.save(g2);


}
}

直接 HibernateSessionFactory 方式可以插入数据,但是下面采用SpringDao方式却不能插入,控制台输出正常,没有任何错误信息,但是数据库中没有数据!

Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into WYT.GUESTBOOK (USERNAME, EMAIL, URL, TITLE, CONTENT, TIME, GID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into WYT.GUESTBOOK (USERNAME, EMAIL, URL, TITLE, CONTENT, TIME, GID) values (?, ?, ?, ?, ?, ?, ?)
...全文
799 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ee4456 2008-07-17
  • 打赏
  • 举报
回复
我想可能是没有commit吧.......你insert了以后没有commit建议拿getHiberanteTemplate来用,它会帮你做一些事情..
我想可能是没有commit吧.......你insert了以后没有commit建议拿getHiberanteTemplate来用,它会帮你做一些事情..
Landor2004 2008-07-17
  • 打赏
  • 举报
回复
这里面用到了spring2的AspectJ表示法,楼主可以看看spring的中文帮助,
意思就是说符合* com.xxx.dao.*.*(..)表达式的类的save*、delete*等方法会被transactionManager管理,也就是加上事务

* com.xxx.dao.*.*(..)也是AspectJ表达式
wnhoo 2008-07-17
  • 打赏
  • 举报
回复
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true </prop>

这个方式也是可以,不过这个和用Spring事务有什么区别了,在性能上是否有差别,还是其他什么原因!
wnhoo 2008-07-17
  • 打赏
  • 举报
回复
感谢,Landor2004 !

但是

<tx:advice id="txAdvice">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="save*" />
<tx:method name="delete*" />
<tx:method name="add*" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="fooServiceOperation"
expression="execution(* com.xxx.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="fooServiceOperation" />
</aop:config>


这部分具体什么意思能否给解释一下!
jyq0105 2008-07-17
  • 打赏
  • 举报
回复
Spring 中没有配置事务
qq278095755 2008-07-17
  • 打赏
  • 举报
回复
没有用spring来管理事务.
wangzh_1983 2008-07-17
  • 打赏
  • 举报
回复
加上
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
daybybyby 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 phon_oy 的回复:]
为什么不用getHibernateTemplate().save();
[/Quote]

对阿,用spring整合hibernate了,为什么手动操作session?
Landor2004 2008-07-17
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>
<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 id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="GuestbookDAO" class="com.oneic.hibernate.GuestbookDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--加入事务部分-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="save*" />
<tx:method name="delete*" />
<tx:method name="add*" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="fooServiceOperation"
expression="execution(* com.xxx.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="fooServiceOperation" />
</aop:config>

</beans>


别忘了expression="execution(* com.xxx.dao.*.*(..))" />改成你自己的dao的包
wnhoo 2008-07-17
  • 打赏
  • 举报
回复
请问针对我上述问题,具体怎么配置了,能说明详细步骤及原理!谢谢!
phon_oy 2008-07-17
  • 打赏
  • 举报
回复
为什么不用getHibernateTemplate().save();
M_song 2008-07-17
  • 打赏
  • 举报
回复
那就是没有配置相关的事务了!
zidasine 2008-07-17
  • 打赏
  • 举报
回复
Spring 中没有配置事务

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧