我知道冬眠很好,相當彈簧芯集成,我可以使用Hibernate模板和休眠DAO支持類都集成,但是從休眠3.1我們已經上下文session,即我們需要的不是使用Hibernate模板和休眠DAO支持,但是當我試圖用這個概念整合和注入SessionFactory的在我的DAO類所有的事都寫,但我想插入數據休眠顯示插入查詢,但數據不會保存到數據庫,請幫助我,我能做些什麼。 這裏是我的代碼春3.0和Hibernate 3.5上下文session
@Transactional
public class UserDAO {
SessionFactory sessionFactory;
public void save(User transientInstance) {
try {
sessionFactory.openSession().save(transientInstance);
} catch (RuntimeException re) {
throw re;
}
}
public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
return (UserDAO) ctx.getBean("UserDAO");
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
這是我的beans.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="UserDAO" class="dao.UserDAO">
<property name="sessionFactory"><ref bean="sessionFactory" /></property>
</bean>
這是我的主要代碼
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=new User("firstName", "lastName", "address", "phoneNo", "mobileNo", "email", "password", false, null);
SessionFactory factory=(SessionFactory)context.getBean("sessionFactory");
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
UserDAO ud=(UserDAO)context.getBean("UserDAO");
ud.save(user);
tx.commit();
session.close();
任何一個可以建議最好的網站,瞭解關於Spring – 2014-04-26 11:35:16