相关疑难解决方法(0)

@Transactional注释在哪里?

你应该把它@Transactional放在DAO类和/或它们的方法中,还是更好地注释使用DAO对象调用的Service类?或者注释两个"层​​"是否有意义?

java spring annotations dao transactions

504
推荐指数
16
解决办法
23万
查看次数

Spring Transactions和hibernate.current_session_context_class

我有一个使用Hibernate 4和Spring Transactions的Spring 3.2应用程序.所有方法都运行良好,我可以正确访问数据库以保存或检索实体.然后,我介绍了一些多线程,并且由于每个线程都访问了db,我从Hibernate收到以下错误:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Run Code Online (Sandbox Code Playgroud)

我从网上读到我要添加<prop key="hibernate.current_session_context_class">thread</prop>到我的Hibernate配置中,但是现在每次我尝试访问db时都会得到:

org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction
Run Code Online (Sandbox Code Playgroud)

但是我的服务方法是注释的@Transactional,并且在添加之前一切正常<prop key="hibernate.current_session_context_class">thread</prop>.

为什么没有交易,尽管方法是用@Transactional注释的?我怎么解决这个问题?

这是我的Hibernate配置(包括会话上下文属性):

<?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:tx="http://www.springframework.org/schema/tx"
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-3.1.xsd">

<!-- Hibernate session factory -->
<bean
    id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
    <property name="dataSource" >
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties" >
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop> 
            <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>  
        </props>
    </property>   
    <property name="annotatedClasses" …
Run Code Online (Sandbox Code Playgroud)

spring hibernate hibernate-session spring-transactions

21
推荐指数
1
解决办法
3万
查看次数

org.hibernate.HibernateException:如果没有活动事务,save无效

我正在创建JSF应用程序并在其中使用一些hibernate内容.我想要做的就是将实体保存到数据库中,但我不断得到这个异常:

org.hibernate.HibernateException: save is not valid without active transaction
Run Code Online (Sandbox Code Playgroud)

起初我得到了这个例外:

org.hibernate.HibernateException: No CurrentSessionContext configured!
Run Code Online (Sandbox Code Playgroud)

然后我发现我需要将它添加到我的hibernate配置中:

<property name="hibernate.current_session_context_class">thread</property>
Run Code Online (Sandbox Code Playgroud)

这解决了这个问题,但现在出现了上述问题.我将实体保存到数据库中,如下所示:

public void create(T entity) {
    getSessionFactory().getCurrentSession().save(entity);
}
Run Code Online (Sandbox Code Playgroud)

我的hibernate.cfg.xml文件如下所示:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/online_tests_management</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="com.groupgti.onlinetests.management.db.Service"/>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我在用:

  • Hibernate-4.1.4.Final
  • JDK 1.6
  • 雄猫6
  • JSF 2.0
  • PrimeFaces 3.3.1
  • MySQL的

有人知道问题出在哪里吗?

java mysql persistence hibernate

8
推荐指数
1
解决办法
2万
查看次数