HHH-6177: fixing a bug with mapping null references introduced by the last pull request

This commit is contained in:
Adam Warski 2011-06-22 08:42:13 +02:00
parent 4662d0a714
commit abaae293d8
6 changed files with 39 additions and 14 deletions

View File

@ -58,7 +58,9 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme
}
for (SingleIdMapper mapper : ids.values()) {
mapper.mapToEntityFromMap(ret, data);
if (!mapper.mapToEntityFromMap(ret, data)) {
return null;
}
}
return ret;

View File

@ -61,9 +61,9 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl
mapToMapFromId(data, getter.get(obj));
}
public void mapToEntityFromMap(Object obj, Map data) {
public boolean mapToEntityFromMap(Object obj, Map data) {
if (data == null || obj == null) {
return;
return false;
}
Getter getter = ReflectionTools.getGetter(obj.getClass(), idPropertyData);
@ -71,11 +71,17 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl
try {
Object subObj = ReflectHelper.getDefaultConstructor( getter.getReturnType() ).newInstance();
setter.set(obj, subObj, null);
boolean ret = true;
for (IdMapper idMapper : ids.values()) {
idMapper.mapToEntityFromMap(subObj, data);
ret &= idMapper.mapToEntityFromMap(subObj, data);
}
if (ret) {
setter.set(obj, subObj, null);
}
return ret;
} catch (Exception e) {
throw new AuditException(e);
}

View File

@ -34,7 +34,12 @@ public interface IdMapper {
void mapToMapFromEntity(Map<String, Object> data, Object obj);
void mapToEntityFromMap(Object obj, Map data);
/**
* @param obj Object to map to.
* @param data Data to map.
* @return True if data was mapped; false otherwise (when the id is {@code null}).
*/
boolean mapToEntityFromMap(Object obj, Map data);
Object mapToIdFromEntity(Object data);

View File

@ -47,10 +47,13 @@ public class MultipleIdMapper extends AbstractCompositeIdMapper implements Simpl
mapToMapFromId(data, obj);
}
public void mapToEntityFromMap(Object obj, Map data) {
public boolean mapToEntityFromMap(Object obj, Map data) {
boolean ret = true;
for (IdMapper idMapper : ids.values()) {
idMapper.mapToEntityFromMap(obj, data);
ret &= idMapper.mapToEntityFromMap(obj, data);
}
return ret;
}
public IdMapper prefixMappedProperties(String prefix) {

View File

@ -53,13 +53,20 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu
this.propertyData = propertyData;
}
public void mapToEntityFromMap(Object obj, Map data) {
public boolean mapToEntityFromMap(Object obj, Map data) {
if (data == null || obj == null) {
return;
return false;
}
Object value = data.get(propertyData.getName());
if (value == null) {
return false;
}
Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData);
setter.set(obj, data.get(propertyData.getName()), null);
setter.set(obj, value, null);
return true;
}
public Object mapToIdFromMap(Map data) {

View File

@ -9,6 +9,8 @@ import org.junit.Test;
import javax.persistence.EntityManager;
import static org.junit.Assert.*;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@ -42,14 +44,14 @@ public class UnidirectionalMulIdWithNulls extends AbstractEntityTest {
@Test
public void testNullReference() {
UniRefIngMulIdEntity nullRef = getAuditReader().find(UniRefIngMulIdEntity.class, 2, 1);
assert nullRef.getReference() == null;
assertNull(nullRef.getReference());
}
@Test
public void testNotNullReference() {
EmbIdTestEntity eite = getAuditReader().find(EmbIdTestEntity.class, ei, 1);
UniRefIngMulIdEntity notNullRef = getAuditReader().find(UniRefIngMulIdEntity.class, 1, 1);
assert notNullRef.getReference() != null;
assert notNullRef.getReference().equals(eite);
assertNotNull(notNullRef.getReference());
assertEquals(notNullRef.getReference(), eite);
}
}