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