[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3854) Issue with greedy loading of associations (default-lazy=false )

Oliver Thallmair (JIRA) noreply at atlassian.com
Mon May 16 15:47:25 EDT 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3854?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=42331#action_42331 ] 

Oliver Thallmair commented on HHH-3854:
---------------------------------------

I discovered this problem in my project.

The problem as was described above is that AbstractEntityTuplizer.proxyFactory == null. I think a solution is to decide in ToOneIdMapper.mapToEntityFromMap
whether a proxy can be used or not. This can be achieved by asking the persister.

In *ToOneIdMapper.mapToEntityFromMap*:

{noformat}
else {
   EntityConfiguration entCfg = verCfg.getEntCfg().get(referencedEntityName);
   if(entCfg == null) {
    // a relation marked as RelationTargetAuditMode.NOT_AUDITED 
    	entCfg = verCfg.getEntCfg().getNotVersionEntityConfiguration(referencedEntityName);
    }
            	
    Class<?> entityClass = ReflectionTools.loadClass(entCfg.getEntityClassName());

	// THIS IS THE CHANGE:
        // get the persister and ask for a proxy
	// if there is one get the proxy otherwise get the object directly
    EntityPersister persister = versionsReader.getSessionImplementor().getFactory().getEntityPersister(referencedEntityName);
    if(persister.hasProxy()) {
             	
		value = persister.createProxy((Serializable)entityId,
       			new ToOneDelegateSessionImplementor(versionsReader, entityClass, entityId, revision, verCfg));
                
	} else {
                	
		// get the object directly (code from ToOneDelegateSessionImplementor)
       	if(verCfg.getEntCfg().getNotVersionEntityConfiguration(referencedEntityName) == null){
			// audited relation, look up entity with envers
			value = versionsReader.find(entityClass, referencedEntityName, entityId, revision);
        } else {
			// notAudited relation, look up entity with hibernate
            value = versionsReader.getSessionImplementor().immediateLoad(referencedEntityName, (Serializable) entityId);
        }
    }
}                	
{noformat}
This code works with the testcase. Can someone please have a look if it is a valid patch?



> Issue with greedy loading of associations (default-lazy=false )
> ---------------------------------------------------------------
>
>                 Key: HHH-3854
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3854
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: envers
>         Environment: Hibernate - core 3.4.0-SNAPSHOT, Hibernate-envers (from trunk)
>            Reporter: Amar Singh
>         Attachments: envers-HHH-3854-testcases.patch, greedy-loading.zip
>
>
> I have an Entity GP which has a many to one association to entity CA.
> <?xml version="1.0"?>
> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
>                                    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
> <hibernate-mapping package="org.hibernate.envers.test.integration.onetoone.bidirectional" default-lazy="false">
> <class name="BidirectionalRefIngPK" table="BIDIRECTIONAL_REFERENCING_ENTITY">
>   <id name="id" type="long" column="BIDIRECTIONAL_REFERENCING_ID" >
>    	<generator class="native"/>
>   </id>
>   <property name="data"/>
>   <many-to-one name="reference" cascade="save-update" class="BidirectionalRefEdPK" column="BIDIRECTIONAL_REFERENCED_ID"/>
>  </class>
>  
>  <class name="BidirectionalRefEdPK" table="BIDIRECTIONAL_REFERENCED_ENTITY">
>   <id name="longId" type="long" column="BIDIRECTIONAL_REFERENCED_ID">
>    <generator class="native"/>
>   </id>
>   <property name="data"/>
>  </class>
>  
> </hibernate-mapping>
> With this when I load a previous version of BidirectionalRefIngPK, the loading mechanism tries to create a proxy for BidirectionalRefEdPK, but in my case lazy is false so an internal call to createProxy will return NULL hence .
> This happens in ToOneIdMapper.java in method mapToEntityFromMap around line 82, which then internally calls for AbstractEntityTuplizer.java line 395 for getProxyFactory() ending in NULL pointer exception because proxy factory is null as mine is a greedy loading. 
> TO REPRODUCE
> ~~~~~~~~~~~~~
> Please find a zipped folder attached. The zipped file has 4 files:
> - GreedyLoading.java (actual testng test case)
> - BidirectionalRefEdPK.java (Entity 1)
> - BidirectionalRefIngPK.java (Entity 2)
> - greedyloading.hbm.xml
> Please place the three java files in its package (org.hibernate.envers.test.integration.onetoone.bidirectional), couldn't find the right package so created test case accordingly. Put the greedyloading.hbm.xml in src/test/resources.
> Now when testng runs GreedyLoading.java you should see NPE.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list