mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add test - self referencing relationships: ResourceFile
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.avaje.tests.model.selfref;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseResourceFile {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@Column(name = "id", nullable = false, length = 64)
|
||||
private String id;
|
||||
|
||||
public BaseResourceFile() {
|
||||
this.id = newId();
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
protected void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
protected String newId() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.avaje.tests.model.selfref;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import com.avaje.ebean.annotation.PrivateOwned;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ResourceFile")
|
||||
public class ResourceFile extends BaseResourceFile {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = true)
|
||||
@JoinColumn(name = "parentResourceFileId", nullable = true)
|
||||
private ResourceFile parent;
|
||||
|
||||
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "parent")
|
||||
@PrivateOwned
|
||||
private Set<ResourceFile> alternatives = new HashSet<ResourceFile>();
|
||||
|
||||
@Column(name = "name", length = 128, nullable = false)
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ResourceFile getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(ResourceFile parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Set<ResourceFile> getAlternatives() {
|
||||
return alternatives;
|
||||
}
|
||||
|
||||
public void setAlternatives(Set<ResourceFile> alternatives) {
|
||||
this.alternatives = alternatives;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.avaje.tests.model.selfref;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
|
||||
public class TestResourceFileSelfRef {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
ResourceFile childFile1 = new ResourceFile();
|
||||
childFile1.setName("childFile1");
|
||||
ResourceFile childFile2 = new ResourceFile();
|
||||
childFile2.setName("childFile2");
|
||||
|
||||
ResourceFile parentFile1 = new ResourceFile();
|
||||
parentFile1.setName("parentFile1");
|
||||
|
||||
childFile1.setParent(parentFile1);
|
||||
childFile2.setParent(parentFile1);
|
||||
parentFile1.getAlternatives().add(childFile1);
|
||||
parentFile1.getAlternatives().add(childFile2);
|
||||
|
||||
server.save(parentFile1);
|
||||
server.save(childFile1);
|
||||
server.save(childFile2);
|
||||
|
||||
// As a workaround for the problem, the child objects can be deleted first
|
||||
//server.delete(childFile1);
|
||||
//server.delete(childFile2);
|
||||
server.delete(parentFile1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user