Using Spring 1.1.1 and Hibernate 2.1.6.
I'm trying to use OpenSessionInView. If I set singleSession="false" (seems to be the recommended approach), and add an object to a persistent collection, the insert is ignored.
If I set singleSession="true" the insert works fine.
Relevent code:
Spring configuration
Hibernate configurationCode:<bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor"> <property name="singleSession"><value>false</value></property> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean>
ClientImpl code segmentsCode:<class name="com.joliciel.aplikaterm.domain.ClientImpl" table="Client" dynamic-update="false" dynamic-insert="false"> <id name="id" column="idClient" type="long" > <generator class="hilo" /> </id> ... <set name="dictionnairesDB" table="Dictionnaire" lazy="true" inverse="true" cascade="all-delete-orphan" sort="unsorted" > <key column="idClient" /> <one-to-many class="com.joliciel.aplikaterm.domain.DictionnaireImpl"/> </set> </class>
To run ajouterDictionnaire (add dictionary), the client is being retrieved as follows:Code:protected Set getDictionnairesDB() { return this.dictionnaires; } protected void setDictionnairesDB(Set dictionnaires) { this.dictionnaires=dictionnaires;} public void ajouterDictionnaire(Dictionnaire dico) { LOG.debug("ajouterDictionnaire(" + dico.getNom() + ")"); DictionnaireImpl leDico = (DictionnaireImpl) dico; leDico.setClient(this); this.dictionnaires.add(leDico); LOG.debug("end ajouterDictionnaire()"); }
I did a diff between log4j files created with singleSession=false and singleSession=true, and here are the results.Code:Client client = null; Iterator clients = getHibernateTemplate().iterate("FROM ClientImpl AS client" + " WHERE client.codeClient = ?" + " AND client.actif=?", new Object[] {codeClient, Boolean.TRUE}, new Type[] {Hibernate.STRING, Hibernate.BOOLEAN}); if (clients.hasNext()) client = (Client) clients.next(); return client;
Extracts from log4j before ajouterDictionnaire with singleSession=false (no insertion):
DEBUG [net.sf.hibernate.impl.SessionImpl] - loading [com.joliciel.aplikaterm.domain.ClientImpl#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - attempting to resolve [com.joliciel.aplikaterm.domain.ClientImpl#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - object not resolved in any cache [com.joliciel.aplikaterm.domain.ClientImpl#1]
DEBUG [net.sf.hibernate.persister.EntityPersister] - Materializing entity: [com.joliciel.aplikaterm.domain.ClientImpl#1]
DEBUG [net.sf.hibernate.impl.BatcherImpl] - about to open: 1 open PreparedStatements, 1 open ResultSets
DEBUG [net.sf.hibernate.SQL] - select clientimpl0_.idClient as idClient0_, clientimpl0_.typeDeClient as typeDeCl2_0_, clientimpl0_.version as version0_, clientimpl0_.codeClient as codeClient0_, clientimpl0_.nom as nom0_, clientimpl0_.courrielAdmin as courriel6_0_, clientimpl0_.fuseauHoraire as fuseauHo7_0_, clientimpl0_.siteWeb as siteWeb0_, clientimpl0_.texte_dAccueil as texte_dA9_0_, clientimpl0_.actif as actif0_, clientimpl0_.idLangueParDefaut as idLangu11_0_, clientimpl0_.fichierLogo as fichier12_0_, clientimpl0_.largeurLogo as largeur13_0_, clientimpl0_.hauteurLogo as hauteur14_0_ from Client clientimpl0_ where clientimpl0_.idClient=?
DEBUG [net.sf.hibernate.loader.Loader] - total objects hydrated: 1
DEBUG [net.sf.hibernate.impl.SessionImpl] - resolving associations for [com.joliciel.aplikaterm.domain.LangueImpl#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - done materializing entity [com.joliciel.aplikaterm.domain.LangueImpl#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - creating collection wrapper:[com.joliciel.aplikaterm.domain.ClientImpl.organisa tionsDB#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - creating collection wrapper:[com.joliciel.aplikaterm.domain.ClientImpl.dictionn airesDB#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - done materializing entity [com.joliciel.aplikaterm.domain.ClientCentreDeDocum entation#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - initializing non-lazy collections
DEBUG [net.sf.hibernate.impl.IteratorImpl] - exhausted results
DEBUG [net.sf.hibernate.impl.IteratorImpl] - closing iterator
Extracts from log4j before ajouterDictionnaire with singleSession=true (insertion succeeds):
DEBUG [net.sf.hibernate.impl.SessionImpl] - loading [com.joliciel.aplikaterm.domain.ClientImpl#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - attempting to resolve [com.joliciel.aplikaterm.domain.ClientImpl#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - resolved object in session cache [com.joliciel.aplikaterm.domain.ClientImpl#1]
DEBUG [net.sf.hibernate.impl.IteratorImpl] - exhausted results
DEBUG [net.sf.hibernate.impl.IteratorImpl] - closing iterator
Actual call to ajouterDictionnaire is identical.
Extracts from log4j after ajouterDictionnaire with singleSession=false (no insertion):
DEBUG [net.sf.hibernate.impl.SessionImpl] - flushing session
DEBUG [net.sf.hibernate.impl.SessionImpl] - Flushing entities and processing referenced collections
DEBUG [net.sf.hibernate.impl.SessionImpl] - Processing unreferenced collections
DEBUG [net.sf.hibernate.impl.SessionImpl] - Scheduling collection removes/(re)creates/updates
DEBUG [net.sf.hibernate.impl.SessionImpl] - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
DEBUG [net.sf.hibernate.impl.SessionImpl] - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG [net.sf.hibernate.impl.SessionImpl] - executing flush
DEBUG [net.sf.hibernate.impl.SessionImpl] - post flush
DEBUG [net.sf.hibernate.impl.SessionImpl] - transaction completion
Extracts from log4j after ajouterDictionnaire with singleSession=true (insertion succeeds):
DEBUG [net.sf.hibernate.impl.SessionImpl] - flushing session
DEBUG [net.sf.hibernate.engine.Cascades] - processing cascades for: com.joliciel.aplikaterm.domain.ClientCentreDeDocum entation
DEBUG [net.sf.hibernate.engine.Cascades] - cascading to collection: com.joliciel.aplikaterm.domain.ClientImpl.organisa tionsDB
DEBUG [net.sf.hibernate.engine.Cascades] - cascading to collection: com.joliciel.aplikaterm.domain.ClientImpl.dictionn airesDB
DEBUG [net.sf.hibernate.engine.Cascades] - cascading to saveOrUpdate()
DEBUG [net.sf.hibernate.impl.SessionImpl] - saveOrUpdate() persistent instance
DEBUG [net.sf.hibernate.engine.Cascades] - cascading to saveOrUpdate()
DEBUG [net.sf.hibernate.impl.SessionImpl] - saveOrUpdate() persistent instance
DEBUG [net.sf.hibernate.engine.Cascades] - cascading to saveOrUpdate()
DEBUG [net.sf.hibernate.impl.SessionImpl] - saveOrUpdate() unsaved instance
DEBUG [net.sf.hibernate.id.TableHiLoGenerator] - new hi value: 2
DEBUG [net.sf.hibernate.impl.SessionImpl] - generated identifier: 65537
DEBUG [net.sf.hibernate.impl.SessionImpl] - saving [com.joliciel.aplikaterm.domain.DictionnaireImpl#65 537]
...
DEBUG [net.sf.hibernate.impl.SessionImpl] - Collection dirty: [com.joliciel.aplikaterm.domain.ClientImpl.dictionn airesDB#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - Flushing entities and processing referenced collections
DEBUG [net.sf.hibernate.impl.SessionImpl] - Updating entity: [com.joliciel.aplikaterm.domain.ClientCentreDeDocum entation#1]
...
DEBUG [net.sf.hibernate.impl.SessionImpl] - Collection found: [com.joliciel.aplikaterm.domain.ClientImpl.dictionn airesDB#1], was: [com.joliciel.aplikaterm.domain.ClientImpl.dictionn airesDB#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - Collection found: [com.joliciel.aplikaterm.domain.UtilisateurImpl.lan guesDeTravailDB#2], was: [com.joliciel.aplikaterm.domain.UtilisateurImpl.lan guesDeTravailDB#2]
DEBUG [net.sf.hibernate.impl.SessionImpl] - Collection found: [com.joliciel.aplikaterm.domain.OrganisationImpl.ut ilisateursDB#1], was: [com.joliciel.aplikaterm.domain.OrganisationImpl.ut ilisateursDB#1]
DEBUG [net.sf.hibernate.impl.SessionImpl] - Processing unreferenced collections
DEBUG [net.sf.hibernate.impl.SessionImpl] - Scheduling collection removes/(re)creates/updates
DEBUG [net.sf.hibernate.impl.SessionImpl] - Flushed: 1 insertions, 1 updates, 0 deletions to 11 objects
DEBUG [net.sf.hibernate.impl.SessionImpl] - Flushed: 0 (re)creations, 1 updates, 0 removals to 6 collections
...
DEBUG [net.sf.hibernate.impl.SessionImpl] - executing flush
DEBUG [net.sf.hibernate.persister.EntityPersister] - Inserting entity: [com.joliciel.aplikaterm.domain.DictionnaireImpl#65 537]
...
DEBUG [net.sf.hibernate.impl.BatcherImpl] - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG [net.sf.hibernate.impl.BatcherImpl] - closing statement
DEBUG [net.sf.hibernate.impl.SessionImpl] - post flush
DEBUG [net.sf.hibernate.impl.SessionImpl] - transaction completion
Any ideas?
Best regards,
Assaf


Reply With Quote