Spring + hibernate =在创建sessionfactory时出错

时间:2015-03-02 07:37:10

标签: java spring hibernate security spring-security

我正在使用带有spring的hibernate。 调用getCurrentSession方法

时出现以下错误
protected Session getCurrentSession(){
   return getSessionFactory().openSession();
}

错误:

    java.lang.StackOverflowError
    org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>    (JdbcCoordinatorImpl.java:97)
    org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:87)
    org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:254)
   org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1609)
    org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:973)

我已经搜索了很多,并检查了stackoverflow上给出的所有答案,但无法解决问题:(任何人都可以帮助我,为什么我会遇到这样的错误。

调用/广告系列方法时出现错误

我的控制器类是:

package com.nativeunlock.security.controller;

import java.security.Principal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nativeunlock.dto.CampaignEntity;
import com.nativeunlock.service.ICampaignService;
import com.nativeunlock.service.IUserService;


@Controller
public class LoginController {

    @Autowired
    IUserService userServices;

    @Autowired
    ICampaignService campaignService;


    @Secured("ROLE_OPERATION")
    @RequestMapping(value="/campaign", method = RequestMethod.GET)
    public String campaign(ModelMap model, Principal principal ) {

        try {   

            List<CampaignEntity> userEntity = campaignService.getCampaignList();
            System.out.println(userEntity.size());

            String name = principal.getName();
            model.addAttribute("username", name);
            return "campaign";
        } catch (Exception e) {
            return login(model, principal);
        }       
    }

    @Secured("ROLE_OPERATION")
    @RequestMapping(value="/addCampaign", method = RequestMethod.GET)
    public String addCampaign(ModelMap model, Principal principal ) {       
        try {           
            return "addCampaign";
        } catch (Exception e) {
            return login(model, principal);
        }       
    }


    @RequestMapping(value="/admin", method = RequestMethod.GET)
    public String login(ModelMap model, Principal principal) {

        if(principal != null) {
            return campaign(model, principal);          
        }
        return "login_page";

    }

    @RequestMapping(value="/loginError", method = RequestMethod.GET)
    public String loginerror(ModelMap model) {

        return login(model, null);

    }


    @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {

        return login(model, null);

    }  
}

我的服务类:

package com.nativeunlock.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nativeunlock.dao.ICampaignDao;
import com.nativeunlock.dto.CampaignEntity;

@Component
public class CampaignService implements ICampaignService {

    @Autowired
    ICampaignDao CampaignDao;

    @Override
    public List<CampaignEntity> getCampaignList() {

        return CampaignDao.getCampaignList();
    }

}

我的dao课程是:

package com.nativeunlock.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nativeunlock.dto.CampaignEntity;

import org.hibernate.Query;

@Component
public class CampaignDao implements ICampaignDao{

    @Autowired
    SessionFactory sessionFactory;

    Session session = null;
    Transaction tx = null;


    protected SessionFactory getSessionFactory() {
        if (sessionFactory == null){
            throw new IllegalStateException("SessionFactory has not been set on DAO before usage");
        } 
        return sessionFactory;
    }

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

    protected Session getCurrentSession(){
        return getSessionFactory().openSession();
    }

    public Query getNamedQuery(String queryName){
        return getCurrentSession().getNamedQuery(queryName);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CampaignEntity> getCampaignList() {     
        final Query clientQuery = getNamedQuery(CampaignEntity.GET_CAMPAIGNS_QUERY);
        return (List<CampaignEntity>) clientQuery.list();
    }

}

我的实体类是

package com.nativeunlock.dto;

import javax.persistence.*;

import lombok.Getter;
import lombok.Setter;


@NamedQueries({
    @NamedQuery(
            name = CampaignEntity.GET_CAMPAIGNS_QUERY,
            query = "from CampaignEntity campaign"
    )     
})

@Entity
@Table(name = "campaign")
public class CampaignEntity {

    public static final String GET_CAMPAIGNS_QUERY ="SiteURLs.getCampaignList";

    @Id
    @GeneratedValue
    @Column(name = "ID")
    @Getter
    @Setter
    private int campain_id;


    @Getter
    @Setter
    @Column(name = "no_of_views")   
    private int no_of_views;


    @Getter
    @Setter
    @Column(name = "video_url") 
    private int video_url;      

}

我的调度员servlet是

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <context:component-scan base-package="com.nativeunlock.**" />

    <context:property-placeholder location="classpath:common.properties" />

    <bean id="dataSource" class="${datasource.class}">
        <property name="driverClassName" value="${mysql.driver}" />
        <property name="url" value="${mysql.url}" />
        <property name="username" value="${mysql.username}" />
        <property name="password" value="${mysql.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.nativeunlock.dao.**"/>       
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
    </bean>

    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


    <bean id="savedRequestAwareAuthenticationSuccessHandler"
        class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
        <property name="targetUrlParameter" value="targetUrl" />
    </bean>


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <security:global-method-security
        jsr250-annotations="enabled" pre-post-annotations="enabled"
        secured-annotations="enabled" />

</beans>

我的日志是:

Mar 02, 2015 12:51:07 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Mar 02, 2015 12:51:07 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Mar 02, 2015 12:51:10 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Mar 02, 2015 12:51:23 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/Audience-Box] threw exception [Handler processing failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:97)
    at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:87)
    at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:254)
    at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1609)
    at org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:973)
    at com.nativeunlock.dao.CampaignDao.getCurrentSession(CampaignDao.java:37)
    at com.nativeunlock.dao.CampaignDao.getNamedQuery(CampaignDao.java:41)
    at com.nativeunlock.dao.CampaignDao.getCampaignList(CampaignDao.java:47)
    at com.nativeunlock.service.CampaignService.getCampaignList(CampaignService.java:23)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:36)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
    at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)
    at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)

1 个答案:

答案 0 :(得分:0)

您的问题是您在彼此之间递归调用以下两个函数。

at com.nativeunlock.security.controller.LoginController.login(LoginController.java:61)
at com.nativeunlock.security.controller.LoginController.campaign(LoginController.java:42)

您不必这样做,因为您的控制器已经使用注释@Secured("ROLE_OPERATION")进行保护。因此,如果需要授权,Spring MVC将自动调用登录控制器。您所要做的就是在配置文件中配置逻辑控制器操作。 (或注释)

在您删除递归后我读到了您对真实问题的评论,即

org.hibernate.MappingException: Named query not known: getCampaignList at org.hibernate.internal.AbstractSessionImpl.getNamedQuery(AbstractSess ionImpl.java:154) at org.hibernate.internal.SessionImpl.getNamedQuery(SessionImpl.java:137 1) at com.nativeunlock.dao.CampaignDao.getNamedQuery(CampaignDao.java:41) 

这是因为这一行

public static final String GET_CAMPAIGNS_QUERY ="SiteURLs.getCampaignList";

你必须确保你试图在那里实现的逻辑是正确的。重新检查SiteURLs.getCampaignList。希望这不应该存在于应用程序中。