From 3b00102514a8962372166e2ecbafeaeb43e82010 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 16 Jul 2019 21:56:51 +0200 Subject: [PATCH] DATAJPA-1535 - Additional test for entities with assigned identifiers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added test case for the new optimized way of handling deletes of non persisted instances which use manually assigned identifiers. This test originally broke as manually marking the entity as new was done in a @PostPersist callback. This needed to be changed to happen in @PrePersist as the actual persistence operation might not have been flushed and the entity might return in new state from CrudRepository.save(…). --- .../domain/sample/EntityWithAssignedId.java | 61 +++++++++++++++++++ .../EntityWithAssignedIdIntegrationTests.java | 48 +++++++++++++++ .../EntityWithAssignedIdRepository.java | 28 +++++++++ src/test/resources/META-INF/persistence.xml | 1 + src/test/resources/META-INF/persistence2.xml | 2 + 5 files changed, 140 insertions(+) create mode 100644 src/test/java/org/springframework/data/jpa/domain/sample/EntityWithAssignedId.java create mode 100644 src/test/java/org/springframework/data/jpa/repository/EntityWithAssignedIdIntegrationTests.java create mode 100644 src/test/java/org/springframework/data/jpa/repository/sample/EntityWithAssignedIdRepository.java diff --git a/src/test/java/org/springframework/data/jpa/domain/sample/EntityWithAssignedId.java b/src/test/java/org/springframework/data/jpa/domain/sample/EntityWithAssignedId.java new file mode 100644 index 000000000..d9a5c7765 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/domain/sample/EntityWithAssignedId.java @@ -0,0 +1,61 @@ +/* + * Copyright 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 + * + * 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 org.springframework.data.jpa.domain.sample; + +import java.util.UUID; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.PostLoad; +import javax.persistence.PrePersist; +import javax.persistence.Transient; + +import org.springframework.data.domain.Persistable; + +/** + * @author Oliver Drotbohm + */ +@Entity +public class EntityWithAssignedId implements Persistable { + + private @Id UUID id = UUID.randomUUID(); + + private @Transient boolean isNew = true; + + /* + * (non-Javadoc) + * @see org.springframework.data.domain.Persistable#getId() + */ + @Override + public UUID getId() { + return id; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.domain.Persistable#isNew() + */ + @Override + public boolean isNew() { + return isNew; + } + + @PrePersist + @PostLoad + public void markNotNew() { + this.isNew = false; + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/EntityWithAssignedIdIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/EntityWithAssignedIdIntegrationTests.java new file mode 100644 index 000000000..772d67c27 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/EntityWithAssignedIdIntegrationTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 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 + * + * 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 org.springframework.data.jpa.repository; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jpa.domain.sample.EntityWithAssignedId; +import org.springframework.data.jpa.repository.sample.EntityWithAssignedIdRepository; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author Oliver Drotbohm + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:config/namespace-application-context.xml") +@Transactional +public class EntityWithAssignedIdIntegrationTests { + + @Autowired EntityWithAssignedIdRepository repository; + + @Test // DATAJPA-1535 + public void deletesEntityJustCreated() { + + EntityWithAssignedId entityWithAssignedId = repository.save(new EntityWithAssignedId()); + + repository.deleteById(entityWithAssignedId.getId()); + + assertThat(repository.existsById(entityWithAssignedId.getId())).isFalse(); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/EntityWithAssignedIdRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/EntityWithAssignedIdRepository.java new file mode 100644 index 000000000..26a1bab14 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/sample/EntityWithAssignedIdRepository.java @@ -0,0 +1,28 @@ +/* + * Copyright 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 + * + * 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 org.springframework.data.jpa.repository.sample; + +import java.util.UUID; + +import org.springframework.data.jpa.domain.sample.EntityWithAssignedId; +import org.springframework.data.repository.CrudRepository; + +/** + * @author Oliver Drotbohm + */ +public interface EntityWithAssignedIdRepository extends CrudRepository { + +} diff --git a/src/test/resources/META-INF/persistence.xml b/src/test/resources/META-INF/persistence.xml index 2360ed5d1..b5176b4f8 100644 --- a/src/test/resources/META-INF/persistence.xml +++ b/src/test/resources/META-INF/persistence.xml @@ -16,6 +16,7 @@ org.springframework.data.jpa.domain.sample.ConcreteType2 org.springframework.data.jpa.domain.sample.CustomAbstractPersistable org.springframework.data.jpa.domain.sample.Customer + org.springframework.data.jpa.domain.sample.EntityWithAssignedId org.springframework.data.jpa.domain.sample.EmbeddedIdExampleEmployeePK org.springframework.data.jpa.domain.sample.EmbeddedIdExampleEmployee org.springframework.data.jpa.domain.sample.EmbeddedIdExampleDepartment diff --git a/src/test/resources/META-INF/persistence2.xml b/src/test/resources/META-INF/persistence2.xml index a2164a9f0..70f95e3da 100644 --- a/src/test/resources/META-INF/persistence2.xml +++ b/src/test/resources/META-INF/persistence2.xml @@ -8,6 +8,7 @@ org.springframework.data.jpa.domain.sample.AuditableUser org.springframework.data.jpa.domain.sample.Category org.springframework.data.jpa.domain.sample.CustomAbstractPersistable + org.springframework.data.jpa.domain.sample.EntityWithAssignedId org.springframework.data.jpa.domain.sample.Item org.springframework.data.jpa.domain.sample.ItemSite org.springframework.data.jpa.domain.sample.MailMessage @@ -27,6 +28,7 @@ org.springframework.data.jpa.domain.sample.AuditableRole org.springframework.data.jpa.domain.sample.Category org.springframework.data.jpa.domain.sample.CustomAbstractPersistable + org.springframework.data.jpa.domain.sample.EntityWithAssignedId org.springframework.data.jpa.domain.sample.Item org.springframework.data.jpa.domain.sample.ItemSite org.springframework.data.jpa.domain.sample.MailMessage