Browse Source
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
5 changed files with 140 additions and 0 deletions
@ -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; |
||||
} |
||||
} |
||||
@ -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(); |
||||
} |
||||
} |
||||
@ -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> { |
||||
|
||||
} |
||||
Loading…
Reference in new issue