0

私のapplication-context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
    <property name="url"><value>jdbc:mysql://localhost:3306/myDB</value></property>
    <property name="username"><value>myUser</value></property>
    <property name="password"><value>myPwd</value></property>

</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>                            
    <property name="mappingResources">
<list>
<value>user.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties" >
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>


<bean id="userdao" class="x.y.z.UserDao">
<property name="sessionFactory" ref="mySessionFactory"/>

</bean>

     </beans>

user.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="x.y.m">
<class name="User" table="user">
<id name="userId" column="id">
     <generator class="native"/>
</id>

 <property name="firstname" column="firstName" />
 <property name="lastName" column="lastName"/>
 <property name="login" column="login"/>
 <property name="pass" column="pass"/>
 <property name="superemail" column="superEmail"/>
 </class>

</hibernate-mapping>

UserDao.java:

package x.y.z;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import x.y.m.User;


@Component
public class UserDao {

    SessionFactory sessionFactory;

    public void addUser(User user) {
        Session session;
        try {
            try {

                session = this.sessionFactory.getCurrentSession();

            System.out.println(session.isConnected());
                session.save(user);
            } catch (RuntimeException e) {
                System.out.println("Not Getting Session");
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            System.out.println("printing in the catch");
            e.printStackTrace();
        }

    }

    public void setSessionFactory(SessionFactory _sessionFactory) {
        this.sessionFactory = _sessionFactory;

    }

}

UserController.java:

  package x.y.c.a;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;


    import x.y.z.UserDao;
    import x.y.m.User;


    @Controller
    public class UserController  {
        @Autowired UserDao userdao ;
        @RequestMapping(value = "/user", method = RequestMethod.GET)
           public ModelAndView student() {
              return new ModelAndView("user", "command", new User());
           }

           @RequestMapping(value = "/addUser", method = RequestMethod.POST)
           public String addStudent(@ModelAttribute("SpringWeb")User user, 
           ModelMap model) {
              model.addAttribute("userId", user.getUserId());
              model.addAttribute("login", user.getLogin());
              model.addAttribute("pass", user.getPass());
              model.addAttribute("firstname", user.getFirstname());
              model.addAttribute("lastName", user.getLastName());
              model.addAttribute("superemail", user.getSuperemail());

            //  UserDao u = new UserDao();
              System.out.println("u==null = "+ userdao==null);
             // System.out.println("u.getSessionFactory()==null = "+ userdao.getSessionFactory()==null);
              userdao.addUser(user);
             // userdao.addUser(user.getId(),user.getLogin(),user.getPass(),user.getFirstname(),user.getLastname(),user.getSuperemail());
              return "result";
           }

           /**
             * @return Returns the userdao.
             */
            public UserDao getUserdao() {
                return userdao;
            }
            /**
             * @param userdao The userdao to set.
             */
            public void setUserdao(UserDao userdao) {
                System.out.println("setting user dao ");
                this.userdao = userdao;
            }

    }

ユーザーレコードを追加しようとすると、次の例外が発生し続けます。

20:13:44,433 INFO  [STDOUT] Not Getting Session
20:13:44,434 ERROR [STDERR] java.lang.NullPointerException
20:13:44,436 ERROR [STDERR]     at com.cpt.dao.assessment.UserDao.addUser(UserDao.java:41)
20:13:44,437 ERROR [STDERR]     at com.cpt.controller.assessment.UserController.addStudent(UserController.java:42)
20:13:44,437 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
20:13:44,439 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
20:13:44,440 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
20:13:44,440 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Method.java:601)
20:13:44,441 ERROR [STDERR]     at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
20:13:44,442 ERROR [STDERR]     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
20:13:44,443 ERROR [STDERR]     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)

20:13:44,444 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
20:13:44,444 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
20:13:44,445 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
20:13:44,445 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
20:13:44,445 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
20:13:44,446 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
20:13:44,446 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)
20:13:44,447 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
20:13:44,447 ERROR [STDERR]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
20:13:44,448 ERROR [STDERR]     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
20:13:44,448 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)
20:13:44,448 ERROR [STDERR]     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)
20:13:44,449 ERROR [STDERR]     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)
20:13:44,449 ERROR [STDERR]     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
20:13:44,450 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
20:13:44,450 ERROR [STDERR]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
20:13:44,451 ERROR [STDERR]     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
20:13:44,451 ERROR [STDERR]     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)20:13:44,452 ERROR [STDERR]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
20:13:44,452 ERROR [STDERR]     at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
20:13:44,452 ERROR [STDERR]     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
20:13:44,453 ERROR [STDERR]     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
20:13:44,453 ERROR [STDERR]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
20:13:44,454 ERROR [STDERR]     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
20:13:44,455 ERROR [STDERR]     at java.lang.Thread.run(Thread.java:722)

助けてください。

4

2 に答える 2

1

トランザクションマネージャーをデカールします(私は休止状態を使用します):

<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>

次に、メソッドまたはクラスにトランザクションとして注釈を付けます。

于 2012-09-11T15:11:01.450 に答える
1

元の問題はこれに似ています

UserDaoアプリケーション コンテキストにの 2 つのインスタンスがあります。1 つは で手動で宣言されapplication-context.xml、もう 1 つは<context:component-scan>( の存在により@Component) によって検出されます。前者にはsessionFactory( による<property>) セットがありますが、後者にはそれがありません ( ではないため@Autowired)。

したがって、Bean を手動で宣言する (および を削除する@Component) か、Spring が自動的に検出できるようにする (この場合は手動で設定しないため、で注釈sessionFactoryを付ける) 必要があります。@Autowired

No Hibernate Session bound to threaderror は、元の問題を解決したことを示しており、NimChimpsky の回答と、Spring 管理のトランザクションの使用に関するその他の資料を@Transactional参照する必要があります。

于 2012-09-11T16:07:45.937 に答える