Spring 4 + hibernate 4 - HibernateException:找不到当前线程的Session

时间:2014-05-11 12:17:43

标签: spring hibernate session

我已经阅读了有关此问题的所有其他问题/答案,但仍然遗漏了一些内容。有人可以帮忙吗?

我发现<prop key="hibernate.current_session_context_class">thread</prop>部分有效,但它认为它不是春季当前会话,所以我评论它(如果我使用它,它会给出这个:{{1} })。

的pom.xml

HibernateException: get is not valid without active transaction

应用context.xml中

<properties>

    <!-- Spring -->
    <spring-framework.version>4.0.3.RELEASE</spring-framework.version>
    <spring.security.version>3.2.3.RELEASE</spring.security.version>
    <!-- Hibernate / JPA -->
    <hibernate.version>4.3.5.Final</hibernate.version>
    <spring-data-jpa.version>1.5.2.RELEASE</spring-data-jpa.version>
</properties>

<dependencies>

    <!-- Spring MVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <!-- Spring and Transactions -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

    <!-- Java Persistence API  -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data-jpa.version}</version>
    </dependency>

</dependencies> 

User.java

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="org.portal" />

<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- DATABASE -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://admin@127.0.0.1::3306" />
    <property name="username" value="admin" />
    <property name="password" value="admin" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
        <list>
            <value>org.portal.user.User</value>
            <value>org.portal.user.role.Role</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
        </props>
    </property>
</bean>

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

<bean id="userService" class="org.portal.user.UserServiceImpl">
    <property name="userRepository" ref="userRepository"/>
</bean>
<bean id="userRepository" class="org.portal.user.UserRepositoryImpl"/>
</beans>

UserServiceImpl.java

@Entity  
@Table(name="user")  
public class User {

@Column(name = "email", nullable = false)
private String email;

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
}

UserRepositoryImpl.java

@Service
class UserServiceImpl implements UserService, UserDetailsService   {

@Autowired
private UserRepository userRepository;

@Transactional
public org.portal.user.User getUserByEmail(String email) {
    org.portal.user.User user = userRepository.getUserByEmail(email);
    return user;
}

public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    org.portal.user.User user = getUserByEmail(email);
}
}

1 个答案:

答案 0 :(得分:2)

在您的代码loadUserByUsername(不是@Transactional)中调用getUserByEmail。虽然它标记为@Transactional,但注释(代理)不会生效,因为两种方法都属于同一个类。在大多数情况下,最好的方法是使用@Transactional注释标记整个@Service类。