<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "eager fetching of entity associations using a jpql query"]]></title>
		<link>http://forums.terracotta.org/forums/posts/list/16.page</link>
		<description><![CDATA[Latest messages posted in the topic "eager fetching of entity associations using a jpql query"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ when I call the following query similar to the below:

'select   a from Auction as a
join fetch mycategory '

I receive the auction entity with an eagerly fetched 1:m or m:m  association mycategory.

on the second call of the query , the query is cached and the auction is returned with a null set for the association UNLESS  within a transaction in which case the proxy is called .

this ultimately causes a lazy initializationException since the code is passing around a detatched entity

the only way around this is to set all associations as eager which is not a good idea.

I have tried @Cache(usage = CacheConcurrencyStrategy.READ_WRITE,include="all") to include lazy and non lazy, however this doesnt fix the issue:

below are the jars Im using.

 terracotta-toolkit-1.1-runtime-2.1.0.jar
 ehcache-core-2.3.2.jar
ehcache-terracotta-2.3.2.jar

Hibernate3.jar : 3.2.4 sp1
Hibernate-entitymanager.jar : 4.2.3.GA
]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#26247</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#26247</link>
				<pubDate><![CDATA[Wed, 6 Apr 2011 01:28:19]]> GMT</pubDate>
				<author><![CDATA[ mankind1]]></author>
			</item>
			<item>
				<title>Re:eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ Can you please send us your hibernate config files and the ehcache.xml?]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#26278</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#26278</link>
				<pubDate><![CDATA[Thu, 7 Apr 2011 01:51:19]]> GMT</pubDate>
				<author><![CDATA[ ankur]]></author>
			</item>
			<item>
				<title>Re:eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ 
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="myCache_ae" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
 <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
	properties="hostName=10.2.0.50, port=40001,socketTimeoutMillis=2000"/> 

  <defaultCache
      maxElementsInMemory="100000"
      eternal="true" transactionalMode="off">                  		      
       <terracotta valueMode="serialization" coherent="true"  clustered="false"/>
  </defaultCache>
    

     <cache name="com.bluecycle.zephyr.auctions.domain.User"
      maxElementsInMemory="100000"
      eternal="true" transactionalMode="off">               		
       <terracotta valueMode="serialization" coherent="true"  clustered="false"/> 
    </cache>

    <cache name="com.bluecycle.zephyr.auctions.domain.User.groups"
      maxElementsInMemory="100000"
      eternal="true" transactionalMode="off">            		
       <terracotta valueMode="serialization" coherent="true"  clustered="false"/> 
    </cache>


   <terracottaConfig url="10.2.0.50:9532"/> 
</ehcache>


****persistence.xml **************************


<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="AuctionEngineEJB-PU" transaction-type="JTA">
     <!-- <exclude-unlisted-classes>true</exclude-unlisted-classes>-->
     <provider>org.hibernate.ejb.HibernatePersistence</provider>  
     <jta-data-source>java:/autosalvageDS</jta-data-source>
     
        <properties>
			<property name="jboss.entity.manager.jndi.name" value="java:/Manager1"/>
			<property name="jboss.entity.manager.factory.jndi.name" value="java:/entityManagerFactory"/>
			<property name="hibernate.current_session_context_class" value="jta"/> 
			<!-- property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/-->
            <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
			 
			<!-- below 2 properties used to fix bugs in ehcache parsing -->
			<property name="hibernate.cache.region_prefix" value=""/>
			<property name="hibernate.session_factory_name" value="SessionFactory" />

			
			<!-- name the ehcache configuration file -->			 
			<property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml" />   
			<!--property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" /-->  
			<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" /> 
			
			<!-- basic cache settings -->		
			<property name="hibernate.cache.use_second_level_cache" value="true"/>	 	                    
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.max_fetch_depth" value="5"/>
            
            <!-- for debugging turn off after use -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.use_sql_comments" value="false"/> 
            <property name="hibernate.generate_statistics" value="true"/>
            
          <!--   <property name="javax.persistence.sharedCache.mode" value="ALL"/>  -->
                         
        </properties>
    </persistence-unit>
</persistence>


*************************************************

User entity with many-to-many relationship with groups

@Entity
@Table(name = "USERS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE,include="all")
public class User implements Serializable {

    @Id
    @Column(name = "USER_ID", insertable = false, updatable = false, nullable = false)
    private Long id;

    @Column(name = "USER_NAME", insertable = false, updatable = false, nullable = false)
    private String username;

    @Column(name = "DIGESTED_PASSWORD", insertable = false, updatable = false)
    private String password;

    @Column(name = "CONTACT_POSTCODE", length = 15, insertable = false, updatable = false)
    protected String postcode;

    @Column(name = "CONTACT_EMAIL_ADDRESS1", length = 255, insertable = false, updatable = false)
    private String email;

    @Column(name = "NAME_FIRST", length = 255, insertable = false, updatable = false)
    private String firstName;

    @Column(name = "NAME_LAST", length = 255, insertable = false, updatable = false)
    private String lastName;

    @Column(name = "COMPANY_NAME ", length = 255, insertable = false, updatable = false)
    private String company;

    @ManyToMany()
    @JoinTable(name = "USER_GROUPS_LINK",
            joinColumns = @JoinColumn(name = "USER_ID", insertable = false, updatable = false),
            inverseJoinColumns = @JoinColumn(name = "GROUP_ID", insertable = false, updatable = false))
     @Cache(usage= CacheConcurrencyStrategy.READ_WRITE,include="all",region="com.bluecycle.zephyr.auctions.domain.User.groups")    
      private Set<UserGroup> groups = new HashSet<UserGroup>();

   
    public User() {
    }

  
    public Long getId() {
        return id;
    }

    
    public String getUsername() {
        return username;
    }

   
    public String getPassword() {
        return password;
    }

    
    public String getEmail() {
        return email;
    }

    
    public String getFirstName() {
        return firstName;
    }   

    public String getLastName() {
        return lastName;
    }

    public String getCompany() {
        return company;
    }

    public Set<UserGroup> getGroups() {
        return groups;
    }

    public String getPostcode() {
        return postcode;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        //noinspection RedundantIfStatement
        if ((this.username == null) ? (other.username != null) : !this.username.equals(other.username)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + (this.username != null ? this.username.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, StringStyle.STYLE).append("id", id).append("username", username).toString();
    }

}


]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#26281</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#26281</link>
				<pubDate><![CDATA[Thu, 7 Apr 2011 02:55:13]]> GMT</pubDate>
				<author><![CDATA[ mankind1]]></author>
			</item>
			<item>
				<title>eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ Can you please try "@ManyToMany(fetch = FetchType.EAGER)" for groups property?]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#26283</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#26283</link>
				<pubDate><![CDATA[Thu, 7 Apr 2011 04:11:08]]> GMT</pubDate>
				<author><![CDATA[ ankur]]></author>
			</item>
			<item>
				<title>Re:eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ yes, with eager fetching set on the association, there is no lazyinitializationException.

however, I wish the entity assocations to be set as lazy and let the jpa query itself to determine whether to  eagerly fetch  or not (by the use of the 'join fetch groups' )

this has been working on the codebase before the 2nd level cache was introduced.



]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#26284</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#26284</link>
				<pubDate><![CDATA[Thu, 7 Apr 2011 04:45:53]]> GMT</pubDate>
				<author><![CDATA[ mankind1]]></author>
			</item>
			<item>
				<title>Re:eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ I think the best suggestion is that you try running using Hibernate's built in Hashtable based second level cache (org.hibernate.cache.HashtableCacheProvider).  If you still see the problem with this second level cache provider, then this is most likely either a bug in  Hibernate or somewhere in your configuration or use of it.

If everything works okay with the Hashtable provider then the next course of action would be try and create a test case so that we can reproduce this internally.

Chris]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#26295</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#26295</link>
				<pubDate><![CDATA[Thu, 7 Apr 2011 10:01:55]]> GMT</pubDate>
				<author><![CDATA[ cdennis]]></author>
			</item>
			<item>
				<title>Re:eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ thanks Chris,

I'll try your suggestion out and let you know the outcome.
I'll also create a simplified usecase that you can reproduce

]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#26313</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#26313</link>
				<pubDate><![CDATA[Fri, 8 Apr 2011 01:18:06]]> GMT</pubDate>
				<author><![CDATA[ mankind1]]></author>
			</item>
			<item>
				<title>Re:eager fetching of entity associations using a jpql query</title>
				<description><![CDATA[ Issue seems to be resolved.Please let us know if more information is required.]]></description>
				<guid isPermaLink="true">http://forums.terracotta.org/forums/posts/list/5232.page#29253</guid>
				<link>http://forums.terracotta.org/forums/posts/list/5232.page#29253</link>
				<pubDate><![CDATA[Fri, 22 Jul 2011 05:33:15]]> GMT</pubDate>
				<author><![CDATA[ rajoshi]]></author>
			</item>
	</channel>
</rss>