[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5288) Envers auditReader.find() returns wrong data for embedded components using fields with default values

Erik-Berndt Scheper (JIRA) noreply at atlassian.com
Wed Jun 16 10:12:25 EDT 2010


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

Erik-Berndt Scheper commented on HHH-5288:
------------------------------------------

There's a simple fix for this issue:

org.hibernate.envers.entities.mapper.ComponentPropertyMapper.java




{noformat}
    public void mapToEntityFromMap(AuditConfiguration verCfg, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
        if (data == null || obj == null) {
            return;
        }

        Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData);

		// If all properties are null and single, then the component has to be null also.
		boolean allNullAndSingle = true;
		for (Map.Entry<PropertyData, PropertyMapper> property : delegate.getProperties().entrySet()) {
			if (data.get(property.getKey().getName()) != null || !(property.getValue() instanceof SinglePropertyMapper)) {
				allNullAndSingle = false;
				break;
			}
		}

		// And we don't have to set anything on the object - the default value is null
		if (!allNullAndSingle) {
			// set the component
			try {
				Object subObj = ReflectHelper.getDefaultConstructor(
						Thread.currentThread().getContextClassLoader().loadClass(componentClassName)).newInstance();
				setter.set(obj, subObj, null);
				delegate.mapToEntityFromMap(verCfg, subObj, data, primaryKey, versionsReader, revision);
			} catch (Exception e) {
				throw new AuditException(e);
			}
		}
    }
{noformat}


should be changed to:


{noformat}
    public void mapToEntityFromMap(AuditConfiguration verCfg, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
        if (data == null || obj == null) {
            return;
        }

        Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData);

		// If all properties are null and single, then the component has to be null also.
		boolean allNullAndSingle = true;
		for (Map.Entry<PropertyData, PropertyMapper> property : delegate.getProperties().entrySet()) {
			if (data.get(property.getKey().getName()) != null || !(property.getValue() instanceof SinglePropertyMapper)) {
				allNullAndSingle = false;
				break;
			}
		}

		if (allNullAndSingle) {
			// single property, but default value need not be null, so we'll set it to null anyway 
			setter.set(obj, null, null);
			
		} else {
			// set the component
			try {
				Object subObj = ReflectHelper.getDefaultConstructor(
						Thread.currentThread().getContextClassLoader().loadClass(componentClassName)).newInstance();
				setter.set(obj, subObj, null);
				delegate.mapToEntityFromMap(verCfg, subObj, data, primaryKey, versionsReader, revision);
			} catch (Exception e) {
				throw new AuditException(e);
			}
		}
    }
{noformat}


> Envers auditReader.find() returns wrong data for embedded components using fields with default values
> -----------------------------------------------------------------------------------------------------
>
>                 Key: HHH-5288
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5288
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: envers
>    Affects Versions: 3.5.0-Final, 3.5.1, 3.5.2
>            Reporter: Erik-Berndt Scheper
>            Assignee: Adam Warski
>         Attachments: Envers--HHH-5288-testcase.patch, HHH-5288-testcase-and-fix.patch
>
>
> I have found that using auditReader.find() to retrieve audit information for entities containing embedded components with default values. This is a regression issue (also occurs in 1.2.2.GA release of Envers - Hibernate 3.3 compatible, but did not occur not in 1.2.1 GA) that started occurring since HHH-3957.
> Sample entity and embeddable: 
> {code:title=DefaultValueComponentTestEntity.java|borderStyle=solid}
> 	@Id
> 	@GeneratedValue
> 	private Integer id;
> 	@Embedded
> 	@Audited
> 	@AttributeOverrides( { @AttributeOverride(name = "comp2.str1", column = @Column(name = "COMP2_STR1")) })
> 	private DefaultValueComponent1 comp1 = null;
> {code}
> {code:title=DefaultValueComponent1.java|borderStyle=solid}
> 	private String str1;
> 	@Embedded
> 	private DefaultValueComponent2 comp2 = new DefaultValueComponent2();
> {code}
> {code:title=DefaultValueComponent2.java|borderStyle=solid}
> 	private String str1 = "defaultValue";
> 	private String str2;
> {code}
> When I try to persist an instance of DefaultValueComponentTestEntity, with comp1 = null and subsequently call auditReader.find(), an instance is returned where:
> {noformat} "defaultValue".equals(comp1.comp2.str1){noformat} 
> (using java bean notation for simplicity).
> I will attach a complete testcase. 

-- 
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