mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Failing unit test to demonstrate the bug that deleting an entity via … (#1445)
* Failing unit test to demonstrate the bug that deleting an entity via an Ebean query does not respect the cascade setting. (here a mapped optional OneToOne entity is not deleted using an Ebean query even if the CascadeType.ALL was used) * moved init and cleanup code to @Before and @After JUnit methods and added another test that shows Ebean.delete() works as expected
This commit is contained in:
committed by
Rob Bygrave
parent
f6047d3b73
commit
68649d0e98
@@ -0,0 +1,76 @@
|
||||
package org.tests.basic.delete;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.tests.model.onetoone.OtoUser;
|
||||
import org.tests.model.onetoone.OtoUserOptional;
|
||||
|
||||
public class TestDeleteCascadeByQuery extends BaseTestCase {
|
||||
|
||||
private OtoUser testUser;
|
||||
private OtoUserOptional userOptional;
|
||||
private Query<OtoUserOptional> userOptionalQuery = Ebean.find(OtoUserOptional.class);
|
||||
private Query<OtoUser> userQuery = Ebean.find(OtoUser.class);
|
||||
|
||||
/**
|
||||
* Init each test. Delete all existing beans. Then create OtoUser, add OtoUserOptional and save.
|
||||
*/
|
||||
@Before
|
||||
public void init() {
|
||||
Ebean.deleteAll(userQuery.findList());
|
||||
Ebean.deleteAll(userOptionalQuery.findList());
|
||||
|
||||
userOptional = new OtoUserOptional();
|
||||
Ebean.save(userOptional);
|
||||
testUser = new OtoUser();
|
||||
testUser.setOptional(userOptional);
|
||||
Ebean.save(testUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that validates deleting a bean using Ebean.delete() respects the CascadeType.DELETE
|
||||
* setting.
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteCascadeByEbeanDelete() {
|
||||
|
||||
assertThat(Ebean.delete(testUser)).isTrue();
|
||||
|
||||
assertThat(userOptionalQuery.findCount())
|
||||
.overridingErrorMessage("Entity OtoUserOptional found. Ebean.delete() on the user "
|
||||
+ "did not delete the OneToOne mapped entity as set with CascadeType.ALL")
|
||||
.isEqualTo(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that validates deleting a bean with OneToOne mapping with a query respects the
|
||||
* CascadeType.DELETE setting.
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteCascadeByQuery() {
|
||||
|
||||
assertThat(userQuery.delete()).isEqualTo(1);
|
||||
|
||||
assertThat(userOptionalQuery.findCount())
|
||||
.overridingErrorMessage("Entity OtoUserOptional found. Ebean query delete() on the user "
|
||||
+ "did not delete the OneToOne mapped entity as set with CascadeType.ALL")
|
||||
.isEqualTo(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup - Delete all existing beans for the next test.
|
||||
*/
|
||||
@After
|
||||
public void cleanup() {
|
||||
Ebean.deleteAll(userQuery.findList());
|
||||
Ebean.deleteAll(userOptionalQuery.findList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.tests.model.BaseModel;
|
||||
|
||||
@Entity
|
||||
@Table(name = "oto_user_model")
|
||||
public class OtoUser extends BaseModel {
|
||||
|
||||
String name;
|
||||
|
||||
@OneToOne(optional = true, cascade = CascadeType.ALL)
|
||||
OtoUserOptional userOptional;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setOptional(OtoUserOptional userOptional) {
|
||||
this.userOptional = userOptional;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.tests.model.onetoone;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.tests.model.BaseModel;
|
||||
|
||||
@Entity
|
||||
@Table(name = "oto_user_model_optional")
|
||||
public class OtoUserOptional extends BaseModel {
|
||||
|
||||
String optional;
|
||||
|
||||
public void setPassword(final String optional) {
|
||||
this.optional = optional;
|
||||
}
|
||||
|
||||
String getOptional() {
|
||||
return optional;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user