No puedo usar una transaccion en una sesion usando Hibernate 4 y Spring 4|
Buenas tardes espero alguien conteste una cuestión que surgió.
El problema es que cuando quiero usar el siguiente código para guardar o actualizar mi objeto.
Me sale el siguiente error:
Exception in thread "AWT-EventQueue-0" org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Mi sesion factory lo obtengo de mi configuracion de Spring y mi archivo de configuracion de Hibernate.
Alguna idea del error???
Ya se que me indica que estoy usando dos sesiones para guardar la coleccion (el objeto) pero mi problema es que no se como
lo puedo solucionar si; según yo estoy usando una sola sesion por que obtengo la sesion actual con el getCurrentSession().
- Inicie sesión o regístrese para enviar comentarios
Ficheros de configuración
Podrías postear tus ficheros de configuración a ver si hay algo extraño.
Mis ficheros de configuración.
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--Configuraciones que se toman del arhivo hibernate.properties-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/basedatos</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
<!--<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>-->
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.show_sql">false</property>
<!--Configuraciones para poder establecer una transaccion activa antes de cada consulta-->
<!-- <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.flushMode">FLUSH_AUTO</property>-->
<!--<property name="hibernate.connection.autocommit">false</property>-->
<!-- <property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> -->
<!-- Estas configuraciones son las que usa hibernate (c3p0) para conexión a bd-->
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">150</property>
<!-- Estas configuracione es para habilitar el uso de cachen de segundo nivel en hibernate -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</property>
<!-- termina configuración de librería de conexión de bd de hibernate-->
<mapping resource="modelos.hbm.xml"/>
....
</session-factory>
</hibernate-configuration>
SpringContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans <a href="http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
" title="http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
">http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
</a> <a href="http://www.springframework.org/schema/aop" title="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a> <a href="http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
" title="http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
">http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
</a> <a href="http://www.springframework.org/schema/context" title="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a> <a href="http://www.springframework.org/schema/context/spring-context-4.1.xsd
" title="http://www.springframework.org/schema/context/spring-context-4.1.xsd
">http://www.springframework.org/schema/context/spring-context-4.1.xsd
</a> <a href="http://www.springframework.org/schema/webflow-config" title="http://www.springframework.org/schema/webflow-config">http://www.springframework.org/schema/webflow-config</a> <a href="http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
" title="http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
">http://www.springframework.org/schema/webflow-config/spring-webflow-conf...</a> <a href="http://www.springframework.org/schema/jee" title="http://www.springframework.org/schema/jee">http://www.springframework.org/schema/jee</a> <a href="http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
" title="http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
">http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
</a> <a href="http://www.springframework.org/schema/jms" title="http://www.springframework.org/schema/jms">http://www.springframework.org/schema/jms</a> <a href="http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
" title="http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
">http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
</a> <a href="http://www.springframework.org/schema/lang" title="http://www.springframework.org/schema/lang">http://www.springframework.org/schema/lang</a> <a href="http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
" title="http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
">http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
</a> <a href="http://www.springframework.org/schema/osgi" title="http://www.springframework.org/schema/osgi">http://www.springframework.org/schema/osgi</a> <a href="http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
" title="http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
">http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
</a> <a href="http://www.springframework.org/schema/tx" title="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
" title="http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
</a> <a href="http://www.springframework.org/schema/util" title="http://www.springframework.org/schema/util">http://www.springframework.org/schema/util</a> <a href="http://www.springframework.org/schema/util/spring-util-4.1.xsd
">
" title="http://www.springframework.org/schema/util/spring-util-4.1.xsd
">
">http://www.springframework.org/schema/util/spring-util-4.1.xsd
">
</a> <context:annotation-config />
<context:component-scan base-package="paquete de mapeo" />
<aop:aspectj-autoproxy />
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--<cache:annotation-driven cache-manager="cacheManager" />-->
<!-- <cache:annotation-driven cache-manager="cacheManager" />-->
<!--<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>-->
<!--<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" p:shared="true"/>-->
<!-- <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml"/>-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:hibernate.properties</value>
<value>file:hibernate.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" depends-on="propertyConfigurer" primary="true" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="sessionFactory" primary="true" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/> -->
<property name="configLocation" value="classpath:${hibernate.cfg.file}" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Declaraciones de Beans UI -->
</beans>