Browse Source

DATAJPA-1535 - Additional test for entities with assigned identifiers.

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(…).
2.1.x
Oliver Drotbohm 7 years ago
parent
commit
3b00102514
  1. 61
      src/test/java/org/springframework/data/jpa/domain/sample/EntityWithAssignedId.java
  2. 48
      src/test/java/org/springframework/data/jpa/repository/EntityWithAssignedIdIntegrationTests.java
  3. 28
      src/test/java/org/springframework/data/jpa/repository/sample/EntityWithAssignedIdRepository.java
  4. 1
      src/test/resources/META-INF/persistence.xml
  5. 2
      src/test/resources/META-INF/persistence2.xml

61
src/test/java/org/springframework/data/jpa/domain/sample/EntityWithAssignedId.java

@ -0,0 +1,61 @@ @@ -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<UUID> {
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;
}
}

48
src/test/java/org/springframework/data/jpa/repository/EntityWithAssignedIdIntegrationTests.java

@ -0,0 +1,48 @@ @@ -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();
}
}

28
src/test/java/org/springframework/data/jpa/repository/sample/EntityWithAssignedIdRepository.java

@ -0,0 +1,28 @@ @@ -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<EntityWithAssignedId, UUID> {
}

1
src/test/resources/META-INF/persistence.xml

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
<class>org.springframework.data.jpa.domain.sample.ConcreteType2</class>
<class>org.springframework.data.jpa.domain.sample.CustomAbstractPersistable</class>
<class>org.springframework.data.jpa.domain.sample.Customer</class>
<class>org.springframework.data.jpa.domain.sample.EntityWithAssignedId</class>
<class>org.springframework.data.jpa.domain.sample.EmbeddedIdExampleEmployeePK</class>
<class>org.springframework.data.jpa.domain.sample.EmbeddedIdExampleEmployee</class>
<class>org.springframework.data.jpa.domain.sample.EmbeddedIdExampleDepartment</class>

2
src/test/resources/META-INF/persistence2.xml

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
<class>org.springframework.data.jpa.domain.sample.AuditableUser</class>
<class>org.springframework.data.jpa.domain.sample.Category</class>
<class>org.springframework.data.jpa.domain.sample.CustomAbstractPersistable</class>
<class>org.springframework.data.jpa.domain.sample.EntityWithAssignedId</class>
<class>org.springframework.data.jpa.domain.sample.Item</class>
<class>org.springframework.data.jpa.domain.sample.ItemSite</class>
<class>org.springframework.data.jpa.domain.sample.MailMessage</class>
@ -27,6 +28,7 @@ @@ -27,6 +28,7 @@
<class>org.springframework.data.jpa.domain.sample.AuditableRole</class>
<class>org.springframework.data.jpa.domain.sample.Category</class>
<class>org.springframework.data.jpa.domain.sample.CustomAbstractPersistable</class>
<class>org.springframework.data.jpa.domain.sample.EntityWithAssignedId</class>
<class>org.springframework.data.jpa.domain.sample.Item</class>
<class>org.springframework.data.jpa.domain.sample.ItemSite</class>
<class>org.springframework.data.jpa.domain.sample.MailMessage</class>

Loading…
Cancel
Save