<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" /><bean
id="StudentDAO" class="org.myeclipse.hibernatespring.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="persistenceLayer"
class="org.myeclipse.hibernatespring.PersistenceLayer"
abstract="false" lazy-init="default" autowire="default"
p:studentDAO-ref="StudentDAO">
</bean></beans>
package org.myeclipse.hibernatespring;
public class PersistenceLayer {
private StudentDAO studentDAO;
public StudentDAO getStudentDAO() {
return studentDAO;
}
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}
public Student listStudent(Integer id)
{
return studentDAO.findById(id);
}
}
package org.myeclipse.hibernatespring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class BuisnessLogic {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student st=new Student();
st.setId(4);
st.setName("Gaurav");
st.setPer(56.87f);
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
PersistenceLayer pl=(PersistenceLayer) bf.getBean("persistenceLayer");
Student stu=pl.listStudent(2);
System.out.print(stu.getName());
}
}
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at org.myeclipse.hibernatespring.StudentDAO.getCurrentSession(StudentDAO.java:39)
at org.myeclipse.hibernatespring.StudentDAO.findById(StudentDAO.java:71)
at org.myeclipse.hibernatespring.PersistenceLayer.listStudent(PersistenceLayer.java:15)
at org.myeclipse.hibernatespring.BuisnessLogic.main(BuisnessLogic.java:20)
答案 0 :(得分:0)
首先使用ApplicationContext
代替BeanFactory
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
PersistenceLayer pl=(PersistenceLayer) xml.getBean("persistenceLayer");
在您的配置中,您可以根据<tx:annotation-driven />
配置注释驱动的事务,但是您无法使用相应的注释@Transactional
注释您的类。添加注释。
@Transactional
public class PersistenceLayer { ... }