mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#604 - @SoftDelete with deletePermanent() on lazy loaded @OneToOne
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.avaje.tests.model.onetoone.album;
|
||||
|
||||
import com.avaje.ebean.Model;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class Album extends BaseModel {
|
||||
|
||||
public static final Model.Finder<Long, Album> find = new Model.Finder<Long, Album>(Album.class);
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
|
||||
private Cover cover;
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Cover getCover() {
|
||||
return this.cover;
|
||||
}
|
||||
|
||||
public void setCover(Cover cover) {
|
||||
this.cover = cover;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.avaje.tests.model.onetoone.album;
|
||||
|
||||
import com.avaje.ebean.Model;
|
||||
import com.avaje.ebean.annotation.SoftDelete;
|
||||
import com.avaje.ebean.annotation.WhenCreated;
|
||||
import com.avaje.ebean.annotation.WhenModified;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseModel extends Model {
|
||||
|
||||
@Id
|
||||
protected Long id;
|
||||
|
||||
@SoftDelete
|
||||
@Column(name = "deleted", nullable = false, columnDefinition = "BOOLEAN DEFAULT FALSE")
|
||||
protected boolean deleted;
|
||||
|
||||
@WhenCreated
|
||||
protected DateTime createdAt;
|
||||
|
||||
@WhenModified
|
||||
protected DateTime lastUpdate;
|
||||
|
||||
protected BaseModel() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public DateTime getCreatedAt() {
|
||||
return this.createdAt;
|
||||
}
|
||||
|
||||
public DateTime getLastUpdate() {
|
||||
return this.lastUpdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this entry is soft deleted.
|
||||
*/
|
||||
public boolean isDeleted() {
|
||||
return this.deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete this entry permanently.
|
||||
*/
|
||||
public boolean deletePermanent() {
|
||||
return db().deletePermanent(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.avaje.tests.model.onetoone.album;
|
||||
|
||||
import com.avaje.ebean.Model;
|
||||
import com.avaje.ebean.annotation.SoftDelete;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.PreRemove;
|
||||
|
||||
@Entity
|
||||
public class Cover extends Model {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Cover.class);
|
||||
|
||||
public static final Finder<Long, Cover> find = new Finder<Long, Cover>(Cover.class);
|
||||
|
||||
@Id
|
||||
protected Long id;
|
||||
|
||||
@SoftDelete
|
||||
@Column(name = "deleted", nullable = false, columnDefinition = "BOOLEAN DEFAULT FALSE")
|
||||
protected boolean deleted;
|
||||
|
||||
protected String s3Url;
|
||||
|
||||
public String getS3Url() {
|
||||
return this.s3Url;
|
||||
}
|
||||
|
||||
public void setS3Url(String s3Url) {
|
||||
this.s3Url = s3Url;
|
||||
}
|
||||
|
||||
@PreRemove
|
||||
public void deleteRemoteFile() {
|
||||
logger.debug("Cover::deleteRemoteFile() --> Remove file from Amazon S3");
|
||||
}
|
||||
|
||||
public boolean isDeleted() {
|
||||
return this.deleted;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.avaje.tests.model.onetoone.album;
|
||||
|
||||
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestOneToOneHardDelete {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Cover cover = new Cover();
|
||||
cover.setS3Url("http://foo");
|
||||
cover.save();
|
||||
|
||||
Album album = new Album();
|
||||
album.setName("BlackWhite");
|
||||
album.setCover(cover);
|
||||
album.save();
|
||||
|
||||
Album found1 = Album.find.where().findUnique();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// ---------------------
|
||||
// SOFT DELETE
|
||||
// ---------------------
|
||||
found1.delete();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(3);
|
||||
// assert we loaded the missing/unloaded foreign key
|
||||
assertThat(sql.get(0)).contains("select t0.id c0, t0.cover_id c1 from album t0 where t0.id = ?");
|
||||
// assert soft delete cascaded
|
||||
assertThat(sql.get(1)).contains("update album set deleted=?, last_update=? where id=?");
|
||||
assertThat(sql.get(2)).contains("update cover set deleted=? where id=?");
|
||||
|
||||
Album found2 = Album.find.where().includeSoftDeletes().findUnique();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// ---------------------
|
||||
// HARD DELETE
|
||||
// ---------------------
|
||||
found2.deletePermanent();
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(3);
|
||||
// assert we loaded the missing/unloaded foreign key
|
||||
assertThat(sql.get(0)).contains("select t0.id c0, t0.cover_id c1 from album t0 where t0.id = ?");
|
||||
// assert hard delete cascaded
|
||||
assertThat(sql.get(1)).contains("delete from album where");
|
||||
assertThat(sql.get(2)).contains("delete from cover where");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user