DATACMNS-357 - Added support for primitive ids in EntityInformation.

AbstractEntityInformation now detects primitive id types and adapts the isNew state according to the Java Language Specification §4.12.5. Supported are primitive Numbers and the entity is considered new if the value of the primitive field is zero.

Original pull request: #37.
This commit is contained in:
Nick Williams
2013-08-09 22:28:27 -05:00
committed by Oliver Gierke
parent b059da8f4f
commit 98b8129a74
3 changed files with 151 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,11 +25,15 @@ import org.springframework.util.Assert;
* {@link #getId(Object)} returns {@literal null}.
*
* @author Oliver Gierke
* @author Nick Williams
*/
public abstract class AbstractEntityInformation<T, ID extends Serializable> implements EntityInformation<T, ID> {
private final Class<T> domainClass;
private boolean idTypePrimitiveSet;
private boolean idTypePrimitive;
/**
* Creates a new {@link AbstractEntityInformation} from the given domain class.
*
@@ -50,7 +54,8 @@ public abstract class AbstractEntityInformation<T, ID extends Serializable> impl
*/
public boolean isNew(T entity) {
return getId(entity) == null;
ID id = getId(entity);
return id == null || this.isIdTypePrimitive() && id instanceof Number && ((Number) id).longValue() == 0;
}
/*
@@ -64,4 +69,13 @@ public abstract class AbstractEntityInformation<T, ID extends Serializable> impl
return this.domainClass;
}
protected boolean isIdTypePrimitive() {
if (!this.idTypePrimitiveSet) {
this.idTypePrimitive = getIdType() != null && getIdType().isPrimitive();
this.idTypePrimitiveSet = true;
}
return this.idTypePrimitive;
}
}