Browse Source
The decision was made not to offer `insert` and `update` as part of the repository interface. It stays available as part of the `JdbcAggregateTemplate` which may be used directly or inside of a custom method implementation.pull/108/head
4 changed files with 3 additions and 171 deletions
@ -1,38 +0,0 @@ |
|||||||
package org.springframework.data.jdbc.core; |
|
||||||
|
|
||||||
import org.springframework.data.repository.Repository; |
|
||||||
|
|
||||||
/** |
|
||||||
* Jdbc repository for dedicated insert(), update() and upsert() sql functions. Other than |
|
||||||
* {@link org.springframework.data.jdbc.repository.support.SimpleJdbcRepository} there should be bypassing of the isNew |
|
||||||
* check. |
|
||||||
* |
|
||||||
* @author Thomas Lang |
|
||||||
* @since 1.1 |
|
||||||
*/ |
|
||||||
public interface JdbcRepository<T, ID> extends Repository<T, ID> { |
|
||||||
|
|
||||||
/** |
|
||||||
* Dedicated insert function. This skips the test if the aggregate root is new and makes an insert. |
|
||||||
* <p> |
|
||||||
* This is useful if the client provides an id for new aggregate roots. |
|
||||||
* </p> |
|
||||||
* |
|
||||||
* @param aggregateRoot the aggregate root to be saved in the database. Must not be {@code null}. |
|
||||||
* @param <S> Type of the aggregate root. |
|
||||||
* @return the saved aggregate root. If the provided aggregate root was immutable and a value needed changing, e.g. |
|
||||||
* the id this will be a new instance. |
|
||||||
*/ |
|
||||||
<S extends T> S insert(S aggregateRoot); |
|
||||||
|
|
||||||
/** |
|
||||||
* Dedicated update function. This skips the test if the aggregate root is new or not and always performs an update |
|
||||||
* operation. |
|
||||||
* |
|
||||||
* @param aggregateRoot the aggregate root to be saved in the database. Must not be {@code null}. |
|
||||||
* @param <S> Type of the aggregate root. |
|
||||||
* @return the saved aggregate root. If the provided aggregate root was immutable and a value needed changing, e.g. |
|
||||||
* the id this will be a new instance. |
|
||||||
*/ |
|
||||||
<S extends T> S update(S aggregateRoot); |
|
||||||
} |
|
||||||
@ -1,102 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2017-2019 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.assertj.core.api.Assertions.*; |
|
||||||
|
|
||||||
import lombok.Data; |
|
||||||
|
|
||||||
import org.junit.ClassRule; |
|
||||||
import org.junit.Rule; |
|
||||||
import org.junit.Test; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.context.annotation.Bean; |
|
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
import org.springframework.context.annotation.Import; |
|
||||||
import org.springframework.data.annotation.Id; |
|
||||||
import org.springframework.data.jdbc.core.JdbcRepository; |
|
||||||
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; |
|
||||||
import org.springframework.data.jdbc.testing.TestConfiguration; |
|
||||||
import org.springframework.data.repository.CrudRepository; |
|
||||||
import org.springframework.jdbc.core.JdbcTemplate; |
|
||||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
|
||||||
import org.springframework.test.context.ContextConfiguration; |
|
||||||
import org.springframework.test.context.junit4.rules.SpringClassRule; |
|
||||||
import org.springframework.test.context.junit4.rules.SpringMethodRule; |
|
||||||
import org.springframework.test.jdbc.JdbcTestUtils; |
|
||||||
import org.springframework.transaction.annotation.Transactional; |
|
||||||
|
|
||||||
/** |
|
||||||
* Very simple use cases for creation and usage of JdbcRepositories. |
|
||||||
* |
|
||||||
* @author Jens Schauder |
|
||||||
*/ |
|
||||||
@ContextConfiguration |
|
||||||
@Transactional |
|
||||||
public class JdbcRepositoryInsertExistingIntegrationTests { |
|
||||||
|
|
||||||
@Configuration |
|
||||||
@Import(TestConfiguration.class) |
|
||||||
static class Config { |
|
||||||
|
|
||||||
@Autowired JdbcRepositoryFactory factory; |
|
||||||
|
|
||||||
@Bean |
|
||||||
Class<?> testClass() { |
|
||||||
return JdbcRepositoryInsertExistingIntegrationTests.class; |
|
||||||
} |
|
||||||
|
|
||||||
@Bean |
|
||||||
DummyEntityRepository dummyEntityRepository() { |
|
||||||
return factory.getRepository(DummyEntityRepository.class); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@ClassRule public static final SpringClassRule classRule = new SpringClassRule(); |
|
||||||
@Rule public SpringMethodRule methodRule = new SpringMethodRule(); |
|
||||||
|
|
||||||
@Autowired NamedParameterJdbcTemplate template; |
|
||||||
@Autowired DummyEntityRepository repository; |
|
||||||
|
|
||||||
@Test // DATAJDBC-282
|
|
||||||
public void insertAnExistingEntity() { |
|
||||||
|
|
||||||
DummyEntity existingDummyEntity = createDummyEntity(); |
|
||||||
existingDummyEntity.idProp = 123L; |
|
||||||
|
|
||||||
DummyEntity entity = repository.insert(existingDummyEntity); |
|
||||||
|
|
||||||
assertThat(JdbcTestUtils.countRowsInTableWhere((JdbcTemplate) template.getJdbcOperations(), "dummy_entity", |
|
||||||
"id_Prop = " + existingDummyEntity.getIdProp())).isEqualTo(1); |
|
||||||
} |
|
||||||
|
|
||||||
private static DummyEntity createDummyEntity() { |
|
||||||
|
|
||||||
DummyEntity entity = new DummyEntity(); |
|
||||||
entity.setName("Entity Name"); |
|
||||||
return entity; |
|
||||||
} |
|
||||||
|
|
||||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long>, JdbcRepository<DummyEntity, Long> {} |
|
||||||
|
|
||||||
@Data |
|
||||||
static class DummyEntity { |
|
||||||
|
|
||||||
String name; |
|
||||||
@Id private Long idProp; |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue