ObjectDB,写入字段值失败(错误363)

发布于 2024-12-27 17:41:18 字数 3089 浏览 1 评论 0原文

我正在尝试制作一个应用程序,可以在 ObjectDB 数据库(嵌入)中存储包含文件和文件夹的树结构,但即使进行简单的测试,我仍然会收到错误...

实体类

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,问题似乎就会消失。

我在这里缺少什么?

I'm trying to make an application that can store a tree structure with files and folders in an ObjectDB database (embedde), but even with a simple test I keep getting an error...

Entity classes

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;
    }
}

Calling code

//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();

And that's where it crashes with an exception:

[ObjectDB 2.3.5_07] javax.persistence.PersistenceException
Failed to write the value of field field FolderNode.children using reflection (error 363)

The problem seems to go away if I don't close and reopen the EntityManagerFactory and EntityManager before fetching the object.

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄离 2025-01-03 17:41:18

您的代码演示了处理映射超类时的问题。如果您将 @MappedSuperclass 注释替换为 @Entity(对于 Node 类),您的测试将运行良好。

ObjectDB 应该将映射的超类作为实体类处理,因此按照您的帖子 - 此问题已得到修复,并且您的测试(使用 @MappedSuperclass)应该适用于构建 2.3.6_14。

Your code demonstrates an issue in handling mapped super classes. If you replace the @MappedSuperclass annotation with @Entity (for the Node class) your test will work well.

ObjectDB should handle mapped super classes as entity classes, so following your post - this issue has been fixed and your test (with @MappedSuperclass) should work with build 2.3.6_14.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文