Created
March 20, 2019 15:55
-
-
Save viluon/8a6dd71e1bd96068a4dd4ddfefbc9116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.ebean.EbeanServer; | |
import io.ebean.annotation.SoftDelete; | |
import me.factorify.server.common.MapperTools; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import javax.persistence.*; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Optional; | |
public class EbeanSoftDeleteTest { | |
@Autowired | |
private EbeanServer ebeanServer; | |
@Entity | |
public static class EntityRoot { | |
@Id | |
private long id; | |
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) | |
private EntityFoo foo; | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public EntityFoo getFoo() { | |
return foo; | |
} | |
public void setFoo(EntityFoo foo) { | |
this.foo = foo; | |
} | |
} | |
@Entity | |
public static class EntityFoo { | |
@Id | |
private long id; | |
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) | |
@JoinColumn(name = "foo_id") | |
private List<EntityBar> children; | |
// Possible second bug: removing this property | |
// throws an SQL syntax error upon saving | |
private String dummy = "dummy"; | |
public List<EntityBar> getChildren() { | |
return children; | |
} | |
public void setChildren(List<EntityBar> children) { | |
this.children = children; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getDummy() { | |
return dummy; | |
} | |
public void setDummy(String dummy) { | |
this.dummy = dummy; | |
} | |
} | |
@Entity | |
public static class EntityBar { | |
@Id | |
private long id; | |
@SoftDelete | |
private boolean deleted; | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public boolean isDeleted() { | |
return deleted; | |
} | |
public void setDeleted(boolean deleted) { | |
this.deleted = deleted; | |
} | |
} | |
@Before | |
public void setUp() { | |
EntityFoo entityFoo = new EntityFoo(); | |
entityFoo.setChildren(Arrays.asList(new EntityBar(), new EntityBar())); | |
EntityRoot root = new EntityRoot(); | |
root.setFoo(entityFoo); | |
ebeanServer.save(root); | |
} | |
@Test | |
public void testEbeanBug() throws IOException { | |
EntityRoot root = ebeanServer.find(EntityRoot.class, 1); | |
Assert.assertNotNull(root); | |
Assert.assertEquals(2, root.getFoo().getChildren().size()); | |
// MapperTools provides a jackson ObjectMapper | |
String json = MapperTools.getObjectMapper().writeValueAsString(root); | |
EntityRoot deserialized = MapperTools.getObjectMapper().readValue(json, EntityRoot.class); | |
EntityBar removed = deserialized.getFoo().getChildren().remove(0); | |
ebeanServer.update(deserialized); | |
EntityRoot saved = ebeanServer.find(EntityRoot.class, 1); | |
Assert.assertNotNull(saved); | |
Assert.assertEquals(1, saved.getFoo().getChildren().size()); | |
Optional<EntityBar> softDeleted = ebeanServer.find(EntityBar.class) | |
.setIncludeSoftDeletes() | |
.where() | |
.idEq(removed.getId()) | |
.findOneOrEmpty(); | |
Assert.assertTrue(softDeleted.isPresent()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- database setup for PostgreSQL | |
create table entity_foo ( | |
id bigserial primary key, | |
dummy text | |
); | |
create table entity_bar ( | |
id bigserial primary key, | |
foo_id bigint references entity_foo, | |
deleted boolean not null default false | |
); | |
create table entity_root ( | |
id bigserial primary key, | |
foo_id bigint references entity_foo | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment