qiao88 2009-02-17 16:03
浏览 156
已采纳

spring2.0+hibernate的奇怪问题

我的目的很简单;
new 一个studnet
然后把它保存到数据库;

[code="java"]public class hibernateDaoHbn extends HibernateDaoSupport {

public static void main(String[] args) {
    Student student=null;
            student=new Student();
            student.setAge("15");
            student.setName("kulinglei");

    List<Student> list=null;

    hibernateDaoHbn hdao=null;
    ApplicationContext ctx=null;
                    ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
                    hdao=(hibernateDaoHbn) ctx.getBean("dao");
                  hdao.getHibernateTemplate().saveOrUpdate(student);

        list=(List<Student>)hdao.getHibernateTemplate().find("From Student");
    System.out.println(list.get(0).getOid())            

}

}[/code]

我的数据就是保存不到数据库、
但显示的oid一直在增加

如果我先把一些数据放到数据库。用
list=(List)hdao.getHibernateTemplate().find("From Student");
for(Student s:list){
System.out.println(s.getOid)
}
事先保存的可以在控制台看到

也就是可以访问但不能插入(权限不用考虑。)

[b]问题补充:[/b]
先谢谢gembler 我试了之后还不行;
我想不是那个原因。我记得以前我上面的代码就可以保存的。

是不是我的jar包有问题啊。希望大家来看看。帮帮忙

[b]问题补充:[/b]
我的spring文件

[code="java"]<?xml version="1.0" encoding="UTF-8"?>
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="classpath:hibernate.cfg.xml">
    </property>
</bean>


<bean id="dao"
    class="test.hibernateDaoHbn">
    <property name="sessionFactory"
        ref="SessionFactory">
    </property> 

</bean>

[/code]
[b]问题补充:[/b]
控制台显示
Hibernate: insert into Student (name, age) values (?, ?)
[b]问题补充:[/b]

我用的是hibernate的模板;没有配置事务,应该是用了默认的事务吧。

[b]问题补充:[/b]
我记得以前不用事务就可以保存的(spring1.x)
现在加了事务以解决
方法如下

[code="java"]public class hibernateDaoHbn extends HibernateDaoSupport implements hiDao {

public static void main(String[] args) {

     hiDao hdao=null;
       ApplicationContext ctx=null;
                    ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
                    hdao=(hiDao)ctx.getBean("dao");
                    hdao.execu();

}

public void execu() {
    Student student=null;
    student=new Student();
    student.setAge("15");
    student.setName("kulinglei");
    student.setOid(2L);
List<Student> list=null;    
this.getHibernateTemplate().save(student);

list=(List) this.getHibernateTemplate().find("From Student");
System.out.println(list.size());

}

}[/code]

[code="java"]<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

      <bean id="SessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
        value="classpath:hibernate.cfg.xml">
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory"/>
</bean>

<bean id="dao"
    class="test.hibernateDaoHbn">       

    <property name="sessionFactory"
          ref="SessionFactory">
    </property>

</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

[/code]

[code="java"]
public interface hiDao {
@Transactional(propagation=Propagation.REQUIRED)
public void execu();
}[/code]

请问一下spring2.0的注释驱动事务 不用接口怎么配置啊

[b]问题补充:[/b]
不用接口代理已经解决

加上它就可以了
/aop:aspectj-autoproxy
[b]问题补充:[/b]
谢谢了,你给的方向是正确的

  • 写回答

2条回答 默认 最新

  • my_asker 2009-02-18 09:12
    关注

    你的事务配置是否生效?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集