0

これは私の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"
    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>

applicationcontext.xml から StudentDAO の参照を取得する永続層クラス

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)
4

1 に答える 1