Browse Source
Moved to both the usage of the newly introduced PersistentEntity.isNew(…) and identifier lookups via PersistentEntity instead of using a custom EntityInformation implementation. JdbcRepositoryFactory now creates a PersistentEntityInformation, SimpleJdbcRepository simply works with a PersistentEntity. Removed references to EntityInformation (a repository subsystem concept) from the template implementation. Removed BasicJdbcPersistentEntity and its tests entirely. JdbcAuditingEventListener is now using an IsNewAwareAuditingHandler. Related tickets: DATACMNS-1333.pull/73/head
12 changed files with 79 additions and 283 deletions
@ -1,58 +0,0 @@
@@ -1,58 +0,0 @@
|
||||
/* |
||||
* Copyright 2017-2018 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.core.mapping; |
||||
|
||||
import org.springframework.data.domain.Persistable; |
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation; |
||||
import org.springframework.lang.Nullable; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
* @since 1.0 |
||||
*/ |
||||
public class BasicJdbcPersistentEntityInformation<T, ID> extends PersistentEntityInformation<T, ID> |
||||
implements JdbcPersistentEntityInformation<T, ID> { |
||||
|
||||
private final JdbcPersistentEntity<T> persistentEntity; |
||||
|
||||
public BasicJdbcPersistentEntityInformation(JdbcPersistentEntity<T> persistentEntity) { |
||||
|
||||
super(persistentEntity); |
||||
|
||||
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.core.mapping.model.JdbcPersistentEntityInformation#setId(java.lang.Object, java.util.Optional) |
||||
*/ |
||||
@Override |
||||
public void setId(T instance, Object value) { |
||||
persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getRequiredIdProperty(), value); |
||||
} |
||||
} |
||||
@ -1,43 +0,0 @@
@@ -1,43 +0,0 @@
|
||||
/* |
||||
* Copyright 2017-2018 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.core.mapping; |
||||
|
||||
import org.springframework.data.repository.core.EntityInformation; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
* @since 1.0 |
||||
*/ |
||||
public interface JdbcPersistentEntityInformation<T, ID> extends EntityInformation<T, ID> { |
||||
|
||||
void setId(T instance, Object value); |
||||
|
||||
/** |
||||
* Returns the identifier of the given entity or throws and exception if it can't be obtained. |
||||
* |
||||
* @param entity must not be {@literal null}. |
||||
* @return the identifier of the given entity |
||||
* @throws IllegalArgumentException in case no identifier can be obtained for the given entity. |
||||
*/ |
||||
default ID getRequiredId(T entity) { |
||||
|
||||
ID id = getId(entity); |
||||
if (id == null) |
||||
throw new IllegalStateException(String.format("Could not obtain required identifier from entity %s!", entity)); |
||||
|
||||
return id; |
||||
} |
||||
} |
||||
@ -1,96 +0,0 @@
@@ -1,96 +0,0 @@
|
||||
/* |
||||
* Copyright 2017-2018 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.core.mapping; |
||||
|
||||
import static org.assertj.core.api.Assertions.*; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.domain.Persistable; |
||||
import org.springframework.data.jdbc.core.mapping.BasicJdbcPersistentEntityInformation; |
||||
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; |
||||
import org.springframework.lang.Nullable; |
||||
|
||||
/** |
||||
* Unit tests for {@link BasicJdbcPersistentEntityInformation}. |
||||
* |
||||
* @author Jens Schauder |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class BasicJdbcPersistentEntityInformationUnitTests { |
||||
|
||||
JdbcMappingContext context = new JdbcMappingContext(); |
||||
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; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue