Skip to content

Commit ae4e2a7

Browse files
schaudergregturn
authored andcommitted
DATAJDBC-158 - BasicJdbcPersistentEntityInformation honors Persistable implementations.
1 parent 08691d6 commit ae4e2a7

File tree

4 files changed

+109
-11
lines changed

4 files changed

+109
-11
lines changed

src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@
2424
import org.springframework.dao.EmptyResultDataAccessException;
2525
import org.springframework.dao.InvalidDataAccessApiUsageException;
2626
import org.springframework.dao.NonTransientDataAccessException;
27-
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
28-
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
29-
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
30-
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
31-
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
27+
import org.springframework.data.jdbc.mapping.model.*;
3228
import org.springframework.data.jdbc.support.JdbcUtil;
3329
import org.springframework.data.mapping.PropertyHandler;
3430
import org.springframework.data.mapping.PropertyPath;
@@ -233,9 +229,10 @@ private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersisten
233229
return parameters;
234230
}
235231

232+
@SuppressWarnings("unchecked")
236233
private <S, ID> ID getIdValueOrNull(S instance, JdbcPersistentEntity<S> persistentEntity) {
237234

238-
EntityInformation<S, ID> entityInformation = new BasicJdbcPersistentEntityInformation<>(persistentEntity);
235+
EntityInformation<S, ID> entityInformation = (EntityInformation<S, ID>) context.getRequiredPersistentEntityInformation(persistentEntity.getType());
239236

240237
ID idValue = entityInformation.getId(instance);
241238

src/main/java/org/springframework/data/jdbc/mapping/model/BasicJdbcPersistentEntityInformation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.springframework.data.jdbc.mapping.model;
1717

18+
import org.springframework.data.domain.Persistable;
1819
import org.springframework.data.repository.core.support.PersistentEntityInformation;
20+
import org.springframework.lang.Nullable;
1921

2022
/**
2123
* @author Jens Schauder
@@ -33,6 +35,18 @@ public BasicJdbcPersistentEntityInformation(JdbcPersistentEntity<T> persistentEn
3335
this.persistentEntity = persistentEntity;
3436
}
3537

38+
@Override
39+
public boolean isNew(T entity) {
40+
return entity instanceof Persistable ? ((Persistable) entity).isNew() : super.isNew(entity);
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
@Nullable
45+
@Override
46+
public ID getId(T entity) {
47+
return entity instanceof Persistable ? ((Persistable<ID>)entity).getId() : super.getId(entity);
48+
}
49+
3650
/*
3751
* (non-Javadoc)
3852
* @see org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation#setId(java.lang.Object, java.util.Optional)

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import org.springframework.context.ApplicationEventPublisher;
1919
import org.springframework.data.jdbc.core.DataAccessStrategy;
2020
import org.springframework.data.jdbc.core.JdbcEntityTemplate;
21-
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
2221
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
23-
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
2422
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
2523
import org.springframework.data.jdbc.repository.SimpleJdbcRepository;
2624
import org.springframework.data.repository.core.EntityInformation;
@@ -50,9 +48,7 @@ public JdbcRepositoryFactory(ApplicationEventPublisher publisher, JdbcMappingCon
5048
@SuppressWarnings("unchecked")
5149
@Override
5250
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
53-
54-
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(aClass);
55-
return new BasicJdbcPersistentEntityInformation<>((JdbcPersistentEntity<T>) persistentEntity);
51+
return (EntityInformation<T, ID>) context.getRequiredPersistentEntityInformation(aClass);
5652
}
5753

5854
@SuppressWarnings("unchecked")
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.model;
17+
18+
import static org.assertj.core.api.Java6Assertions.assertThat;
19+
20+
import org.junit.Test;
21+
import org.springframework.data.annotation.Id;
22+
import org.springframework.data.domain.Persistable;
23+
import org.springframework.lang.Nullable;
24+
25+
/**
26+
* @author Jens Schauder
27+
*/
28+
public class BasicJdbcPersistentEntityInformationUnitTests {
29+
30+
JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy(), cs -> {});
31+
private DummyEntity dummyEntity = new DummyEntity();
32+
private PersistableDummyEntity persistableDummyEntity = new PersistableDummyEntity();
33+
34+
@Test // DATAJDBC-158
35+
public void idIsBasedOnIdAnnotatedProperty() {
36+
37+
dummyEntity.id = 42L;
38+
assertThat(context.getRequiredPersistentEntityInformation(DummyEntity.class).getRequiredId(dummyEntity))
39+
.isEqualTo(42L);
40+
}
41+
42+
@Test // DATAJDBC-158
43+
public void idIsBasedOnPersistableGetId() {
44+
45+
assertThat( //
46+
context.getRequiredPersistentEntityInformation(PersistableDummyEntity.class)
47+
.getRequiredId(persistableDummyEntity) //
48+
).isEqualTo(23L);
49+
}
50+
51+
@Test // DATAJDBC-158
52+
public void isNewIsBasedOnIdAnnotatedPropertyBeingNull() {
53+
54+
assertThat(context.getRequiredPersistentEntityInformation(DummyEntity.class).isNew(dummyEntity)).isTrue();
55+
dummyEntity.id = 42L;
56+
assertThat(context.getRequiredPersistentEntityInformation(DummyEntity.class).isNew(dummyEntity)).isFalse();
57+
}
58+
59+
@Test // DATAJDBC-158
60+
public void isNewIsBasedOnPersistableIsNew() {
61+
62+
persistableDummyEntity.isNewFlag = true;
63+
assertThat(
64+
context.getRequiredPersistentEntityInformation(PersistableDummyEntity.class).isNew(persistableDummyEntity))
65+
.isTrue();
66+
67+
persistableDummyEntity.isNewFlag = false;
68+
assertThat(
69+
context.getRequiredPersistentEntityInformation(PersistableDummyEntity.class).isNew(persistableDummyEntity))
70+
.isFalse();
71+
}
72+
73+
private static class DummyEntity {
74+
@Id Long id;
75+
}
76+
77+
private static class PersistableDummyEntity implements Persistable<Long> {
78+
boolean isNewFlag;
79+
80+
@Nullable
81+
@Override
82+
public Long getId() {
83+
return 23L;
84+
}
85+
86+
@Override
87+
public boolean isNew() {
88+
return isNewFlag;
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)