diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..8f9de8fe --- /dev/null +++ b/pom.xml @@ -0,0 +1,208 @@ + + + + 4.0.0 + + org.springframework.data + spring-data-jdbc + 1.0.0.BUILD-SNAPSHOT + + Spring Data JDBC + Spring Data module for JDBC repositories. + http://projects.spring.io/spring-data-jdbc + + + org.springframework.data.build + spring-data-parent + 2.0.0.BUILD-SNAPSHOT + + + + + DATAJDBC + + 2.0.0.BUILD-SNAPSHOT + + reuseReports + 1.8.0.10 + + + + + + release + + + + org.jfrog.buildinfo + artifactory-maven-plugin + false + + + + + + + + + + ${project.groupId} + spring-data-commons + ${springdata.commons} + + + + org.springframework + spring-tx + + + + org.springframework + spring-context + + + + org.springframework + spring-beans + + + + org.springframework + spring-core + + + commons-logging + commons-logging + + + + + + org.hsqldb + hsqldb + 2.2.8 + test + + + org.springframework + spring-jdbc + + + + + + + + + + + org.jacoco + jacoco-maven-plugin + ${jacoco} + + ${jacoco.destfile} + + + + jacoco-initialize + + prepare-agent + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + + org.springframework + spring-instrument + ${spring} + runtime + + + org.hsqldb + hsqldb + ${hsqldb1} + runtime + + + + + default-test + + + **/* + + + + + unit-tests + + test + + test + + + **/*UnitTests.java + + + + + integration-tests + + test + + test + + + **/*IntegrationTests.java + **/*Tests.java + + + **/*UnitTests.java +\ + + -javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco}/org.jacoco.agent-${jacoco}-runtime.jar=destfile=${jacoco.destfile} + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + org.codehaus.mojo + wagon-maven-plugin + + + org.asciidoctor + asciidoctor-maven-plugin + + + + + + + spring-libs-snapshot + https://repo.spring.io/libs-snapshot + + + + + + spring-plugins-snapshot + https://repo.spring.io/plugins-snapshot + + + + \ No newline at end of file diff --git a/src/main/java/org/springframework/data/jdbc/mapping/context/JdbcMappingContext.java b/src/main/java/org/springframework/data/jdbc/mapping/context/JdbcMappingContext.java new file mode 100644 index 00000000..e9fd47d3 --- /dev/null +++ b/src/main/java/org/springframework/data/jdbc/mapping/context/JdbcMappingContext.java @@ -0,0 +1,41 @@ +/* + * 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.context; + +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; +import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty; +import org.springframework.data.mapping.context.AbstractMappingContext; +import org.springframework.data.mapping.model.SimpleTypeHolder; +import org.springframework.data.util.TypeInformation; + +/** + * @author Jens Schauder + */ +public class JdbcMappingContext extends AbstractMappingContext, JdbcPersistentProperty> { + + + @Override + protected JdbcPersistentEntity createPersistentEntity(TypeInformation typeInformation) { + return new JdbcPersistentEntity(typeInformation); + } + + @Override + protected JdbcPersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor, JdbcPersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { + return new JdbcPersistentProperty(field, descriptor, owner, simpleTypeHolder); + } +} diff --git a/src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentEntity.java b/src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentEntity.java new file mode 100644 index 00000000..7e91a166 --- /dev/null +++ b/src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentEntity.java @@ -0,0 +1,29 @@ +/* + * 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 org.springframework.data.mapping.model.BasicPersistentEntity; +import org.springframework.data.util.TypeInformation; + +/** + * @author Jens Schauder + */ +public class JdbcPersistentEntity extends BasicPersistentEntity { + + public JdbcPersistentEntity(TypeInformation information) { + super(information); + } +} diff --git a/src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentProperty.java b/src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentProperty.java new file mode 100644 index 00000000..1bcc1be8 --- /dev/null +++ b/src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentProperty.java @@ -0,0 +1,46 @@ +/* + * 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 java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import org.springframework.data.mapping.Association; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; +import org.springframework.data.mapping.model.SimpleTypeHolder; + +/** + * @author Jens Schauder + */ +public class JdbcPersistentProperty extends AnnotationBasedPersistentProperty { + + /** + * Creates a new {@link AnnotationBasedPersistentProperty}. + * + * @param field must not be {@literal null}. + * @param propertyDescriptor can be {@literal null}. + * @param owner must not be {@literal null}. + * @param simpleTypeHolder + */ + public JdbcPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { + super(field, propertyDescriptor, owner, simpleTypeHolder); + } + + @Override + protected Association createAssociation() { + return null; + } +} diff --git a/src/main/java/org/springframework/data/jdbc/repository/SimpleJdbcRepository.java b/src/main/java/org/springframework/data/jdbc/repository/SimpleJdbcRepository.java new file mode 100644 index 00000000..45bb7ad9 --- /dev/null +++ b/src/main/java/org/springframework/data/jdbc/repository/SimpleJdbcRepository.java @@ -0,0 +1,102 @@ +/* + * 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.repository; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import javax.sql.DataSource; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.core.EntityInformation; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; + +/** + * @author Jens Schauder + */ +public class SimpleJdbcRepository implements CrudRepository { + + private final EntityInformation entityInformation; + private final NamedParameterJdbcOperations template; + + public SimpleJdbcRepository(EntityInformation entityInformation, DataSource dataSource) { + this.entityInformation = entityInformation; + this.template = new NamedParameterJdbcTemplate(dataSource); + } + + @Override + public S save(S entity) { + Map parameters = new HashMap<>(); + parameters.put("id", entityInformation.getId(entity)); + parameters.put("name", "blah blah"); + + template.update( + "insert into dummyentity (id, name) values (:id, :name)", + parameters); + + return entity; + } + + @Override + public Iterable save(Iterable entities) { + return null; + } + + @Override + public T findOne(ID id) { + return null; + } + + @Override + public boolean exists(ID id) { + return false; + } + + @Override + public Iterable findAll() { + return null; + } + + @Override + public Iterable findAll(Iterable ids) { + return null; + } + + @Override + public long count() { + return 0; + } + + @Override + public void delete(ID id) { + + } + + @Override + public void delete(T entity) { + + } + + @Override + public void delete(Iterable entities) { + + } + + @Override + public void deleteAll() { + + } +} diff --git a/src/main/java/org/springframework/data/jdbc/repository/support/JdbcPersistentEntityInformation.java b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcPersistentEntityInformation.java new file mode 100644 index 00000000..97664acf --- /dev/null +++ b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcPersistentEntityInformation.java @@ -0,0 +1,30 @@ +/* + * 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.repository.support; + +import java.io.Serializable; +import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; +import org.springframework.data.repository.core.support.PersistentEntityInformation; + +/** + * @author Jens Schauder + */ +public class JdbcPersistentEntityInformation extends PersistentEntityInformation { + + public JdbcPersistentEntityInformation(JdbcPersistentEntity persistentEntity) { + super(persistentEntity); + } +} diff --git a/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java new file mode 100644 index 00000000..a92d74d6 --- /dev/null +++ b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java @@ -0,0 +1,55 @@ +/* + * 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.repository.support; + +import java.io.Serializable; +import javax.sql.DataSource; +import org.springframework.data.jdbc.mapping.context.JdbcMappingContext; +import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; +import org.springframework.data.jdbc.repository.SimpleJdbcRepository; +import org.springframework.data.repository.core.EntityInformation; +import org.springframework.data.repository.core.RepositoryInformation; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.core.support.RepositoryFactorySupport; + +/** + * @author Jens Schauder + */ +public class JdbcRepositoryFactory extends RepositoryFactorySupport { + + private final DataSource dataSource; + private final JdbcMappingContext context = new JdbcMappingContext(); + + public JdbcRepositoryFactory(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public EntityInformation getEntityInformation(Class aClass) { + JdbcPersistentEntity persistentEntity = (JdbcPersistentEntity) context.getPersistentEntity(aClass); + return new JdbcPersistentEntityInformation(persistentEntity); + } + + @Override + protected Object getTargetRepository(RepositoryInformation repositoryInformation) { + return new SimpleJdbcRepository(getEntityInformation(repositoryInformation.getDomainType()), dataSource); + } + + @Override + protected Class getRepositoryBaseClass(RepositoryMetadata repositoryMetadata) { + return SimpleJdbcRepository.class; + } +} diff --git a/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java b/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java new file mode 100644 index 00000000..b938925f --- /dev/null +++ b/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java @@ -0,0 +1,93 @@ +/* + * 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.repository; + +import static org.junit.Assert.*; + +import java.sql.SQLException; +import org.junit.After; +import org.junit.Test; +import org.springframework.data.annotation.Id; +import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; +import org.springframework.data.repository.CrudRepository; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import lombok.Data; + +/** + * very simple use cases for creation and usage of JdbcRepositories. + * + * @author Jens Schauder + */ +public class JdbcRepositoryIntegrationTests { + + private final EmbeddedDatabase db = new EmbeddedDatabaseBuilder() + .generateUniqueName(true) + .setType(EmbeddedDatabaseType.HSQL) + .setScriptEncoding("UTF-8") + .ignoreFailedDrops(true) + .addScript("org.springframework.data.jdbc.repository/createTable.sql") + .build(); + + private final NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(db); + + @After + public void afeter() { + db.shutdown(); + } + + + @Test + public void canSaveAnEntity() throws SQLException { + DummyEntityRepository repository = createRepository(); + + DummyEntity entity = new DummyEntity(); + entity.setId(23L); + entity.setName("Entity Name"); + + entity = repository.save(entity); + + int count = template.queryForObject( + "SELECT count(*) FROM dummyentity WHERE id = :id", + new MapSqlParameterSource("id", entity.getId()), + Integer.class); + + assertEquals( + 1, + count); + } + + private DummyEntityRepository createRepository() throws SQLException { + JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(db); + return jdbcRepositoryFactory.getRepository(DummyEntityRepository.class); + } + + private interface DummyEntityRepository extends CrudRepository { + + } + + @Data + private static class DummyEntity { + + @Id + Long id; + String name; + } +} diff --git a/src/test/resources/org.springframework.data.jdbc.repository/createTable.sql b/src/test/resources/org.springframework.data.jdbc.repository/createTable.sql new file mode 100644 index 00000000..6bfdd471 --- /dev/null +++ b/src/test/resources/org.springframework.data.jdbc.repository/createTable.sql @@ -0,0 +1 @@ +CREATE TABLE dummyentity (ID BIGINT, NAME VARCHAR(100), PRIMARY KEY (ID)) \ No newline at end of file