Search code examples
javajpaobjectdb

ObjectDB relation ships


I am trying to add a relationship "OneToMany" to a database i am creating to test if ObjectDB is any good for me.

what I have tried so far:

@Entity
public class Parent {
    @Id
    private int parentID;
    private int childFK;
    Set<Child> children;

    public Parent(int p,int c) {
    this.parentID = p;
    this.childFK = c;
    }

    @OneToMany(orphanRemoval=true)
    @JoinColumn(name="childFK")
    public Set<Child> getChildren(){
        return childern;
    }
}

This child class is:

@Entity
public class Child {
    @Id
    private int childID;
    private String name;

    public Child(int c,String n) {
    this.childID = c;
    this.name = n;
    }
}

My main method is:

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
   EntityManager em = emf.createEntityManager();

   em.getTransaction().begin();

   Parent p = new Parent(01,02);
   em.persist(p);
   Child c = new Child(02,"bob");
   em.persist(c);

   em.getTransaction().commit();

This creates the database no problem but no relationships exist. This is the was the documentations states how to do it.

I have also tried adding the the actual instance of child to the database. This somewhat works as it then knows there is an instance of the database available but it then does not know how to use it.

I have tried the query:

 SELECT p.getChildren() FROM Parent p

This just returns no such method getChildren().

Any ideas?


Solution

  • There are two problems in your test. After fixing these errors the relationship between the parent and the child is created as expected.

    The first problem, in JPA you can use field access or property access, but mixing them is not allowed. If you annotate a field with @Id then your are using field access. In that case, other annotations should also be on fields rather than on property methods.

    Accordingly your Parent class should be defined as:

    @Entity
    public static class Parent {
        @Id
        private int parentID;
        private int childFK;
        @OneToMany(orphanRemoval=true)
        // @JoinColumn(name="childFK") ignored by ObjectDB
        Set<Child> children;
    
        public Parent(int p,int c) {
            this.parentID = p;
            this.childFK = c;
        }
    
        public Set<Child> getChildren(){
            return children;
        }
    }
    

    The second problem is that the main method persists two objects, a parent and a child, but without connecting them. You have to add a line of code for connecting these two objects:

        EntityManager em = emf.createEntityManager();
    
        em.getTransaction().begin();
    
        Parent p = new Parent(01,02);
        em.persist(p);
        Child c = new Child(02,"bob");
        em.persist(c);
        p.children = Collections.singleton(c); // connect the child to the parent
    
        em.getTransaction().commit();
    

    After running the test with these fixes an ObjectDB database is created with the relationship.