DATAJDBC-158 - BasicJdbcPersistentEntityInformation honors Persistable implementations.
This commit is contained in:
committed by
Greg Turnquist
parent
08691d6c45
commit
ae4e2a7deb
@@ -24,11 +24,7 @@ import java.util.stream.StreamSupport;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.dao.NonTransientDataAccessException;
|
||||
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
|
||||
import org.springframework.data.jdbc.mapping.model.*;
|
||||
import org.springframework.data.jdbc.support.JdbcUtil;
|
||||
import org.springframework.data.mapping.PropertyHandler;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
@@ -233,9 +229,10 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <S, ID> ID getIdValueOrNull(S instance, JdbcPersistentEntity<S> persistentEntity) {
|
||||
|
||||
EntityInformation<S, ID> entityInformation = new BasicJdbcPersistentEntityInformation<>(persistentEntity);
|
||||
EntityInformation<S, ID> entityInformation = (EntityInformation<S, ID>) context.getRequiredPersistentEntityInformation(persistentEntity.getType());
|
||||
|
||||
ID idValue = entityInformation.getId(instance);
|
||||
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.model;
|
||||
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Jens Schauder
|
||||
@@ -33,6 +35,18 @@ public class BasicJdbcPersistentEntityInformation<T, ID> extends PersistentEntit
|
||||
this.persistentEntity = persistentEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew(T entity) {
|
||||
return entity instanceof Persistable ? ((Persistable) entity).isNew() : super.isNew(entity);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
@Override
|
||||
public ID getId(T entity) {
|
||||
return entity instanceof Persistable ? ((Persistable<ID>)entity).getId() : super.getId(entity);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation#setId(java.lang.Object, java.util.Optional)
|
||||
|
||||
@@ -18,9 +18,7 @@ package org.springframework.data.jdbc.repository.support;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.jdbc.core.DataAccessStrategy;
|
||||
import org.springframework.data.jdbc.core.JdbcEntityTemplate;
|
||||
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
|
||||
import org.springframework.data.jdbc.repository.SimpleJdbcRepository;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
@@ -50,9 +48,7 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
|
||||
|
||||
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(aClass);
|
||||
return new BasicJdbcPersistentEntityInformation<>((JdbcPersistentEntity<T>) persistentEntity);
|
||||
return (EntityInformation<T, ID>) context.getRequiredPersistentEntityInformation(aClass);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.model;
|
||||
|
||||
import static org.assertj.core.api.Java6Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class BasicJdbcPersistentEntityInformationUnitTests {
|
||||
|
||||
JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy(), cs -> {});
|
||||
private DummyEntity dummyEntity = new DummyEntity();
|
||||
private PersistableDummyEntity persistableDummyEntity = new PersistableDummyEntity();
|
||||
|
||||
@Test // DATAJDBC-158
|
||||
public void idIsBasedOnIdAnnotatedProperty() {
|
||||
|
||||
dummyEntity.id = 42L;
|
||||
assertThat(context.getRequiredPersistentEntityInformation(DummyEntity.class).getRequiredId(dummyEntity))
|
||||
.isEqualTo(42L);
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-158
|
||||
public void idIsBasedOnPersistableGetId() {
|
||||
|
||||
assertThat( //
|
||||
context.getRequiredPersistentEntityInformation(PersistableDummyEntity.class)
|
||||
.getRequiredId(persistableDummyEntity) //
|
||||
).isEqualTo(23L);
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-158
|
||||
public void isNewIsBasedOnIdAnnotatedPropertyBeingNull() {
|
||||
|
||||
assertThat(context.getRequiredPersistentEntityInformation(DummyEntity.class).isNew(dummyEntity)).isTrue();
|
||||
dummyEntity.id = 42L;
|
||||
assertThat(context.getRequiredPersistentEntityInformation(DummyEntity.class).isNew(dummyEntity)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-158
|
||||
public void isNewIsBasedOnPersistableIsNew() {
|
||||
|
||||
persistableDummyEntity.isNewFlag = true;
|
||||
assertThat(
|
||||
context.getRequiredPersistentEntityInformation(PersistableDummyEntity.class).isNew(persistableDummyEntity))
|
||||
.isTrue();
|
||||
|
||||
persistableDummyEntity.isNewFlag = false;
|
||||
assertThat(
|
||||
context.getRequiredPersistentEntityInformation(PersistableDummyEntity.class).isNew(persistableDummyEntity))
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
private static class DummyEntity {
|
||||
@Id Long id;
|
||||
}
|
||||
|
||||
private static class PersistableDummyEntity implements Persistable<Long> {
|
||||
boolean isNewFlag;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Long getId() {
|
||||
return 23L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew() {
|
||||
return isNewFlag;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user