ObjectDB ObjectDB

Need help to make this test work and define proper annotations for entities Lists

#1

Hi,

I can't get this simple test case to work, i know it is probably due to the lack of proper annotations on entities list, could anyone help me to define them?

NB in this test case I didn't set all entities methods for the sake of clarity, also in actual application no field are public nor static and no entity is static. 

 

package testodb;

import java.io.File;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

public class TestODB implements Serializable {
    
    public static void main(String[] args) {
        Tester t = new Tester();
        t.test();      
    }

    public static class Tester {
       
        EntityManagerFactory emf;
        EntityManager em;
       
        public void test(){

            openDB();
            em = emf.createEntityManager();
            em.getTransaction().begin();
            OrderDat ord = new OrderDat(new CustomerDat("customer name"),null);
            System.out.println("order.name: "+ord.customer.name);
            em.persist(ord);
            em.getTransaction().commit();
            em.flush(); 
            em.close();           
            //closeDB();
           
            //openDB();         
            em = emf.createEntityManager();
            TypedQuery<OrderDat> query = em.createQuery("SELECT ord FROM OrderDat ord", OrderDat.class);           
            System.out.println("result.order.name: "+query.getResultList().get(0).customer.name);                       
            em.close();
            closeDB();
        }
       
        void openDB(){ 
            //System.setProperty("objectdb.conf",
            //        System.getProperty("user.home")+"objectdb.conf");
            emf = Persistence.createEntityManagerFactory(
                    "objectdb:$objectdb/db/test3.tmp"); //;drop 
        }
       
        void closeDB(){
            emf.close();
            new File("objectdb:$objectdb/db/test3.tmp").delete();
        }       
      
        @Entity public static class OrderDat implements Serializable {
            @Id @GeneratedValue private long id;
            public List<ItemDat> items;
            public CustomerDat customer;
            public OrderDat(CustomerDat customer,List<ItemDat> items){
                this.customer = customer;
                this.items = items;
            }
            public long getId() {
                return id;
            }
            public void setId(long id) {
                this.id = id;
            }
        }
       
        @Entity public static class ItemDat implements Serializable {
            @Id @GeneratedValue private long id;
            public List<String> image;
            public List<ApiIDDat> apiId;
            public ItemDat(List<ApiIDDat> apiId){
                this.apiId = apiId;
            }
            public long getId() {
                return id;
            }
            public void setId(long id) {
                this.id = id;
            }
        }
       
        @Entity public static class CustomerDat implements Serializable {
            @Id @GeneratedValue private long id;
            public List<ApiIDDat> apiId;
            public String name;       
            public CustomerDat(String name){
                this.name = name;
            }
            public long getId() {
                return id;
            }
            public void setId(long id) {
                this.id = id;
            }
        }
       
        @Entity public static class ApiIDDat implements Serializable {
            @Id @GeneratedValue private long id;
            public String identifier;
            public String apiName;
            public ApiIDDat(String apiNam,String ident){          
                apiName = apiNam;
                identifier = ident;
            }
            public long getId() {
                return id;
            }
            public void setId(long id) {
                this.id = id;
            }
        }   
    }
}
edit
delete
#2

It would help to provide some description of the problem (something more specific than "it doesn't work") when you ask for help in the forum. Anyway, an attempt to run your program fails with the following error message:

order.name: customer name
Exception in thread "main" [ObjectDB 2.x] SELECT ord FROM  ==> OrderF <==  ord
javax.persistence.PersistenceException
Type OrderF is not found (error 301)
(position 16)

which is expected, as there is no entity class OrderF in your program, so the query that includes "FROM OrderF" is invalid.

ObjectDB Support
edit
delete
#3

sorry the name refactoring did not apply in the query, i correct it here.

The problem is simple, customer name is not retrieved (null) after it has been persisted.

Thank you.

 

output 

order.name: customer name
result.order.name: null
edit
delete
#4

Cascading of the persist operation from OrderDat to CustomerDat is missing.

Try:

    @Entity public static class OrderDat implements Serializable {
        ...
        @ManyToOne(cascade=CascadeType.PERSIST)
        public CustomerDat customer;
        ...

 

ObjectDB Support
edit
delete
#5

Thank you!

This annotation also fixed some update issue had I with the actual application entities.

edit
delete

Reply

To post on this website please sign in.