我的目的很简单;
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]
谢谢了,你给的方向是正确的