<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=auni_ku" rel="nofollow">Sharif Uddin</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-5007?redirect=false" rel="nofollow">SPR-5007</a></strong> and commented</p>
<p dir="auto">HibernateTemplate.loadAll(clazz) returns duplicate entries if the Class 'clazz' contains collection field and database contains entry related to that collection field.</p>
<p dir="auto">for example,<br>
A Customer contains list of Product (one-to-many).<br>
In database say there are Products p1,p2 for customer c1 and Product p3 for Customer c2.<br>
Now ..loadAll(Customer.class) returns a list contains {c1,c1,c2}.</p>
<p dir="auto">related class: org.springframework.orm.hibernate3.HibernateTemplate</p>
<p dir="auto">related method:<br>
public List loadAll(final Class entityClass) throws DataAccessException {<br>
return (List) executeWithNativeSession(new HibernateCallback() {<br>
public Object doInHibernate(Session session) throws HibernateException {<br>
Criteria criteria = session.createCriteria(entityClass);<br>
prepareCriteria(criteria);<br>
return criteria.list();<br>
}<br>
});<br>
}</p>
<p dir="auto">Possible Solution:<br>
changing the line</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Criteria criteria = session.createCriteria(entityClass);"><pre class="notranslate"><code class="notranslate">Criteria criteria = session.createCriteria(entityClass);
</code></pre></div>
<p dir="auto">to<br>
Criteria criteria = session.createCriteria(entityClass).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.5</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=isopov" rel="nofollow">Ivan Sopov</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9845?redirect=false" rel="nofollow">SPR-9845</a></strong> and commented</p>
<p dir="auto">If I inject javax.inject.Provider for creating prototype scoped beans into session scoped beans it fails when creating second session:</p>
<p dir="auto">2012-10-01 18:45:50.113:WARN::/pins/default<br>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionBeanWithDefaultProvider': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.inject.Provider com.sopovs.moradanen.SessionBeanWithDefaultProvider.prototypePojoProvider; nested exception is java.lang.IllegalArgumentException: Can not set javax.inject.Provider field com.sopovs.moradanen.SessionBeanWithDefaultProvider.prototypePojoProvider to com.sopovs.moradanen.PrototypeBean<br>
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)<br>
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)<br>
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)<br>
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)<br>
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:332)<br>
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:43)<br>
at org.springframework.web.context.request.SessionScope.get(SessionScope.java:92)<br>
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:328)<br>
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)<br>
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)<br>
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)<br>
at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectFactory.getObject(DefaultListableBeanFactory.java:1040)<br>
at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyProvider.get(DefaultListableBeanFactory.java:1056)<br>
at com.sopovs.moradanen.DefaultProviderController.sayHello(DefaultProviderController.java:19)<br>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)...</p>
<p dir="auto">I have created sample project reproducing the issue and published it on github: <a href="https://github.com/isopov/provider-in-session-with-spring">https://github.com/isopov/provider-in-session-with-spring</a></p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1.2</p>
<p dir="auto"><strong>Reference URL:</strong> <a href="https://github.com/isopov/provider-in-session-with-spring">https://github.com/isopov/provider-in-session-with-spring</a></p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398117666" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13819" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13819/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13819">#13819</a> <code class="notranslate">@Inject</code> Provider or <code class="notranslate">@Autowired</code> ObjectFactory issue with session scoped bean (<em><strong>"duplicates"</strong></em>)</li>
</ul>