-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Demonstrating use of composite ids. #696
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
== Spring Data JDBC Composite Id | ||
|
||
=== EmployeeTest | ||
|
||
Demonstrates saving an entity with composite id. | ||
|
||
Once by using a direct insert, via a custom `insert` method in the repository, backed by `JdbcAggregateTemplate.insert` and once by a custom id generating callback. | ||
See `CompositeConfiguration.idGeneration()`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-data-jdbc-composite-ids</artifactId> | ||
|
||
<parent> | ||
<groupId>org.springframework.data.examples</groupId> | ||
<artifactId>spring-data-jdbc-examples</artifactId> | ||
<version>2.0.0.BUILD-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<name>Spring Data JDBC - Examples using composite ids</name> | ||
<description>Sample project demonstrating Spring Data JDBCs support for custom ids</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.data</groupId> | ||
<artifactId>spring-data-jdbc</artifactId> | ||
<version>4.0.0-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.data</groupId> | ||
<artifactId>spring-data-relational</artifactId> | ||
<version>4.0.0-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; | ||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; | ||
import org.springframework.data.relational.core.mapping.event.BeforeConvertCallback; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
|
||
/** | ||
* Configuration for the demonstration of composite ids. | ||
* | ||
* Registers a {@link BeforeConvertCallback} for generating ids. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
@Configuration | ||
@EnableJdbcRepositories | ||
public class CompositeConfiguration extends AbstractJdbcConfiguration { | ||
|
||
@Bean | ||
BeforeConvertCallback<Employee> idGeneration() { | ||
return new BeforeConvertCallback<>() { | ||
AtomicLong counter = new AtomicLong(); | ||
|
||
@Override | ||
public Employee onBeforeConvert(Employee employee) { | ||
if (employee.id == null) { | ||
employee.id = new EmployeeId(Organization.RND, counter.addAndGet(1)); | ||
} | ||
return employee; | ||
} | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.annotation.PersistenceCreator; | ||
|
||
/** | ||
* A simple entity sporting a compostite id. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
class Employee { | ||
|
||
@Id | ||
EmployeeId id; | ||
|
||
String name; | ||
|
||
@PersistenceCreator | ||
Employee(EmployeeId id, String name) { | ||
|
||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
Employee(String name) { | ||
this.name = name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
/** | ||
* Composite id for {@link Employee} instances. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
record EmployeeId( | ||
Organization organization, | ||
Long employeeNumber) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
import org.springframework.data.repository.ListCrudRepository; | ||
|
||
|
||
/** | ||
* Repositories for {@link Employee} instances. | ||
* | ||
* @author Jens Schauder | ||
* @see InsertRepository | ||
* @see InsertRepositoryImpl | ||
*/ | ||
interface EmployeeRepository extends ListCrudRepository<Employee, EmployeeId>, InsertRepository<Employee> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using fewer dependencies (i.e. just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
|
||
/** | ||
* Interface for repositories supporting an {@literal insert} operation, that always performs an insert on the database | ||
* and does not check the instance. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
interface InsertRepository<E> { | ||
E insert(E employee); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.jdbc.core.JdbcAggregateTemplate; | ||
|
||
/** | ||
* Fragment implementing the {@literal insert} operation using a {@link JdbcAggregateTemplate}. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
class InsertRepositoryImpl<E> implements InsertRepository<E> { | ||
@Autowired | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Please use constructor injection. |
||
private JdbcAggregateTemplate template; | ||
@Override | ||
public E insert(E employee) { | ||
return template.insert(employee); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
/** | ||
* Just an enum to be part of the composite id, to demonstrate that one may use various datatypes. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
enum Organization { | ||
RND, | ||
SALES, | ||
MARKETING, | ||
PURCHASING | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@NonNullApi | ||
package example.springdata.jdbc.compositeid; | ||
|
||
import org.springframework.lang.NonNullApi; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
logging.level.org.springframework.data=INFO | ||
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
create table employee | ||
( | ||
organization varchar(20), | ||
employee_number int, | ||
name varchar(100) | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* | ||
* https://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 example.springdata.jdbc.compositeid; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
/** | ||
* Test demonstrating the use of composite ids. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
@SpringBootTest(classes = CompositeConfiguration.class) | ||
@AutoConfigureDataJdbc | ||
class EmployeeTests { | ||
|
||
@Autowired | ||
EmployeeRepository repository; | ||
|
||
@Test | ||
void employeeDirectInsert() { | ||
|
||
Employee employee = repository.insert(new Employee(new EmployeeId(Organization.RND, 23L), "Jens Schauder")); | ||
|
||
Employee reloaded = repository.findById(employee.id).orElseThrow(); | ||
|
||
assertThat(reloaded.name).isEqualTo(employee.name); | ||
} | ||
|
||
@Test | ||
void employeeIdGeneration() { | ||
|
||
Employee employee = repository.save(new Employee("Mark Paluch")); | ||
|
||
assertThat(employee.id).isNotNull(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.