Skip to content

AbstractEntityInformation.isNew() method violates declared API behavior #2567

@superbrown

Description

@superbrown

The javadoc for the class reads as such:

/**
 * Base class for implementations of {@link EntityInformation}. Considers an entity to be new whenever
 * {@link #getId(Object)} returns {@literal null}.

However, the implementation of the isNew(T entity) method also considers the object new if the object's id is set to 0:

	public boolean isNew(T entity) {

		ID id = getId(entity);
		Class<ID> idType = getIdType();

		if (!idType.isPrimitive()) {
			return id == null;
		}

		if (id instanceof Number) {
			return ((Number) id).longValue() == 0L;
		}

		throw new IllegalArgumentException(String.format("Unsupported primitive id type %s!", idType));
	}

This has surfaced as a problem with a project I'm working on because the project's design relied upon 0 being considered a valid ID.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions