HHH-6177: fixing a bug with mapping null references introduced by the last pull request
This commit is contained in:
parent
4662d0a714
commit
abaae293d8
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue