ObjectDBデータベースにファイルとフォルダをツリー構造で格納できるアプリケーション(embedde)を作ろうとしているのですが、簡単なテストでもエラーが発生し続けます...
エンティティ クラス
Node.java
@MappedSuperclass
public abstract class Node {
@Column(name = "Name", nullable = false)
private String name;
@Column(name = "Parent_FK")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FolderID", nullable = true)
private FolderNode parent = null;
public Node() {
}
public Node(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean hasParent() {
return (parent != null);
}
public FolderNode getParent() {
return parent;
}
void setParent(FolderNode parent) {
this.parent = parent;
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31).append(name).toHashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj instanceof Label) { // TODO is instanceof enough??
Label other = (Label) obj;
return name.equals(other.getName());
}
return false;
}
}
FolderNode.java
@Entity(name = "Folders")
public class FolderNode extends Node {
@Column(name = "FolderID")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.LAZY, orphanRemoval = true)
private Set<Node> children = new HashSet<Node>();
public FolderNode() {
}
public FolderNode(String name) {
super(name);
}
public Set<Node> getChildren() {
return children;
}
public void setChildren(Set<Node> children) {
this.children = children;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
呼び出しコード
//Init
EntityManagerFactory emf = Persistence.createEntityManagerFactory("database.odb");
EntityManager em = emf.createEntityManager();
//Create object
FolderNode rootFolder = new FolderNode("root");
//Persist
em.getTransaction().begin();
em.persist(rootFolder);
em.getTransaction().commit();
em.close();
emf.close();
emf = Persistence.createEntityManagerFactory("database.odb");
em = emf.createEntityManager();
TypedQuery<FolderNode> query = em.createQuery("SELECT f FROM Folders f", FolderNode.class);
List<FolderNode> result = query.getResultList();
そして、それが例外でクラッシュする場所です:
[ObjectDB 2.3.5_07] javax.persistence.PersistenceException
Failed to write the value of field field FolderNode.children using reflection (error 363)
オブジェクトをフェッチする前に EntityManagerFactory と EntityManager を閉じて再度開かないと、問題は解決するようです。
ここで何が欠けていますか?