Skip to content

DATACMNS-976 - AbstractEntityInformation considers Persistable.isNew(). #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.13.0.BUILD-SNAPSHOT</version>
<version>1.13.0.DATACMNS-976-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2017 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.
Expand All @@ -17,6 +17,7 @@

import java.io.Serializable;

import org.springframework.data.domain.Persistable;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.util.Assert;

Expand All @@ -26,6 +27,7 @@
*
* @author Oliver Gierke
* @author Nick Williams
* @author Christoph Strobl
*/
public abstract class AbstractEntityInformation<T, ID extends Serializable> implements EntityInformation<T, ID> {

Expand All @@ -48,6 +50,10 @@ public AbstractEntityInformation(Class<T> domainClass) {
*/
public boolean isNew(T entity) {

if(entity instanceof Persistable) {
return ((Persistable) entity).isNew();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import lombok.Builder;

import java.io.Serializable;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ReflectionUtils;
Expand All @@ -33,6 +36,7 @@
*
* @author Oliver Gierke
* @author Nick Williams
* @author Christoph Strobl
*/
public class AbstractEntityInformationUnitTests {

Expand Down Expand Up @@ -88,6 +92,17 @@ public void rejectsUnsupportedPrimitiveIdType() {
information.isNew(new UnsupportedPrimitiveIdEntity());
}

@Test // DATACMNS-976
public void considersPersistableIsNew() {

FooEn<ImplementingPersistable, String> information = new FooEn<ImplementingPersistable, String>(ImplementingPersistable.class);

assertThat(information.isNew(ImplementingPersistable.builder().id(100L).isNew(true).build()), is(true));
assertThat(information.isNew(ImplementingPersistable.builder().isNew(true).build()), is(true));
assertThat(information.isNew(ImplementingPersistable.builder().id(100L).isNew(false).build()), is(false));
assertThat(information.isNew(ImplementingPersistable.builder().isNew(false).build()), is(false));
}

static class PrimitiveIdEntity {

@Id long id;
Expand Down Expand Up @@ -124,4 +139,21 @@ public Class<ID> getIdType() {
return (Class<ID>) ReflectionUtils.findField(type, "id").getType();
}
}

@Builder
static class ImplementingPersistable implements Persistable<Long> {

final Long id;
final boolean isNew;

@Override
public Long getId() {
return id;
}

@Override
public boolean isNew() {
return isNew;
}
}
}