Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b931dcf

Browse files
committedDec 23, 2024··
Add support for composite ids.
Entities may be annotated with `@Id` and `@Embedded`, resulting in a composite id on the database side. The full embedded entity is considered the id, and therefore the check for determining if an aggregate is considered a new aggregate requiring an insert or an existing one, asking for an update is based on that entity, not its elements. Most use cases will require a custom `BeforeConvertCallback` to set the id for new aggregate. For an entity with `@Embedded` id, the back reference used in tables for referenced entities consists of multiple columns, each named by a concatenation of <table-name> + `_` + <column-name>. E.g. the back reference to a `Person` entity, with a composite id with the properties `firstName` and `lastName` will consist of the two columns `PERSON_FIRST_NAME` and `PERSON_LAST_NAME`. This holds for directly referenced entities as well as `List`, `Set` and `Map`. Closes #574
1 parent 16250a8 commit b931dcf

File tree

44 files changed

+2403
-1044
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2403
-1044
lines changed
 

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public <T> Object insert(T instance, Class<T> domainType, Identifier identifier,
115115
public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) {
116116

117117
Assert.notEmpty(insertSubjects, "Batch insert must contain at least one InsertSubject");
118+
118119
SqlIdentifierParameterSource[] sqlParameterSources = insertSubjects.stream()
119120
.map(insertSubject -> sqlParametersFactory.forInsert(insertSubject.getInstance(), domainType,
120121
insertSubject.getIdentifier(), idValueSource))
@@ -160,7 +161,7 @@ public <S> boolean updateWithVersion(S instance, Class<S> domainType, Number pre
160161
public void delete(Object id, Class<?> domainType) {
161162

162163
String deleteByIdSql = sql(domainType).getDeleteById();
163-
SqlParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType, ID_SQL_PARAMETER);
164+
SqlParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType);
164165

165166
operations.update(deleteByIdSql, parameter);
166167
}
@@ -181,7 +182,7 @@ public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previou
181182

182183
RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
183184

184-
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forQueryById(id, domainType, ID_SQL_PARAMETER);
185+
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forQueryById(id, domainType);
185186
parameterSource.addValue(VERSION_SQL_PARAMETER, previousVersion);
186187
int affectedRows = operations.update(sql(domainType).getDeleteByIdAndVersion(), parameterSource);
187188

@@ -201,8 +202,7 @@ public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentPro
201202

202203
String delete = sql(rootEntity.getType()).createDeleteByPath(propertyPath);
203204

204-
SqlIdentifierParameterSource parameters = sqlParametersFactory.forQueryById(rootId, rootEntity.getType(),
205-
ROOT_ID_PARAMETER);
205+
SqlIdentifierParameterSource parameters = sqlParametersFactory.forQueryById(rootId, rootEntity.getType());
206206
operations.update(delete, parameters);
207207
}
208208

@@ -236,7 +236,7 @@ public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> prope
236236
public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainType) {
237237

238238
String acquireLockByIdSql = sql(domainType).getAcquireLockById(lockMode);
239-
SqlIdentifierParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType, ID_SQL_PARAMETER);
239+
SqlIdentifierParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType);
240240

241241
operations.query(acquireLockByIdSql, parameter, ResultSet::next);
242242
}
@@ -262,7 +262,7 @@ public long count(Class<?> domainType) {
262262
public <T> T findById(Object id, Class<T> domainType) {
263263

264264
String findOneSql = sql(domainType).getFindOne();
265-
SqlIdentifierParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType, ID_SQL_PARAMETER);
265+
SqlIdentifierParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType);
266266

267267
try {
268268
return operations.queryForObject(findOneSql, parameter, getEntityRowMapper(domainType));
@@ -329,7 +329,7 @@ public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
329329
public <T> boolean existsById(Object id, Class<T> domainType) {
330330

331331
String existsSql = sql(domainType).getExists();
332-
SqlParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType, ID_SQL_PARAMETER);
332+
SqlParameterSource parameter = sqlParametersFactory.forQueryById(id, domainType);
333333

334334
Boolean result = operations.queryForObject(existsSql, parameter, Boolean.class);
335335
Assert.state(result != null, "The result of an exists query must not be null");

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcBackReferencePropertyValueProvider.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.