Illegal attempt to associate a collection with two open sessions
Bonjour,
J'ai une erreur "Illegal attempt to associate a collection with two open sessions" lorsque je met à jour un enregistrement avec une relation Many-To-Many!
J'ai testé pas mal de config, et rien à faire, je ne trouve pas !
J'aimerai savoir, ma session je dois la .close() après chaque action?
Est-il obligatoire d'utiliser des transactions? je ne sais pas si cela a un rapport!
Voici mes configs:
Spring/Hibernate
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="annotatedClasses">
<list>
<value>mypackage.Customer</value>
<value>mypackage.History</value>
<value>mypackage.Quarter</value>
<value>mypackage.Task</value>
<value>mypackage.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- <prop key="hibernate.show_sql">true</prop> -->
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
</props>
</property>
</bean>
<!-- Property PlaceHolder post-processor -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- MySQL DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.connection.driver_class}</value>
</property>
<property name="url">
<value>${jdbc.connection.url}</value>
</property>
<property name="username">
<value>${jdbc.connection.username}</value>
</property>
<property name="password">
<value>${jdbc.connection.password}</value>
</property>
</bean> |
Pour ce qui est de mon dao "Task" (celui qui pose problème):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| public class TaskDaoImpl implements TaskDao {
/**
* The session factory.
*/
private SessionFactory sessionFactory;
/**
* The default constructor.
*/
public TaskDaoImpl() {
this.sessionFactory = null;
}
/**
* @return sessionFactory
*/
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
/**
* @param sessionFactory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* @param id
* @return the task
* @throws RuntimeException
*/
public Task getTask(long id) {
Session session = null;
try {
session = this.sessionFactory.openSession();
return (Task) session.load(Task.class, Long.valueOf(id));
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* @param inTask
* @throws RuntimeException
*/
public void addTask(Task inTask) {
Session session = null;
try {
session = this.sessionFactory.openSession();
session.save(inTask);
session.flush();
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
} finally {
if (session != null) {
session.close();
}
}
}
/**
* @return the list of tasks
* @throws RuntimeException
*/
@SuppressWarnings("unchecked")
public List<Task> findListTask() {
Session session = null;
try {
session = this.sessionFactory.openSession();
Query query = session.createQuery("from Task");
return query.list();
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* @param inTask
* @throws RuntimeException
*/
public void updateTask(Task inTask) {
Session session = null;
try {
session = this.sessionFactory.openSession();
session.update(inTask);
session.flush();
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
} finally {
if (session != null) {
session.close();
}
}
}
} |
Il y a-t-il quelque chose de spécial à faire lorsque j'utilise une relation Many-To-Many?
Car pour tous mes autres services (User,Customer, ...) tout fonctionne parfaitement!
Merci d'avance pour vos réponses!