Field in Generic Class not persisted?

#1

Hi,

I am trying to persist a class which is extended from an generic class - but fields declared inside the generic class are not persisted - is that expected? I assume it can be done b/c this field can be recovered after serialisation/deserialization.

Here is an code to demonstrate the problem

Thanks

 

package DBbug;

import com.google.common.base.Objects;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.apache.commons.lang3.SerializationUtils;

@Entity
public class OBJ6 extends ABS<Long, OBJ6>{
     @Id @GeneratedValue long id;
    private long valInInstanceClass;

    public OBJ6() {
    }
    public OBJ6(Long l ) {
        super(l);
        valInInstanceClass = l;
    }

    @Override
    public boolean equals(Object x) {
        if( x instanceof OBJ6)
        {
            OBJ6 obj = (OBJ6)x;
            return Objects.equal(obj.valueInGenericClass, valueInGenericClass);
        }
        else
        {
            throw new RuntimeException("wrong type");
        }
    }
    private void print(){
        System.out.println("vals INSTANCE = " + valInInstanceClass + " val AbstractClass = " + valueInGenericClass);
    }
    @Override
    public int hashCode() {
        int hash = 7;
        return hash;
    }

     public static void main(String[] args)
    {
        OBJ6 H = new OBJ6(Long.valueOf("2"));
        H.print();
        byte[] by = SerializationUtils.serialize(H);
        OBJ6 H2 = SerializationUtils.deserialize(by);
        H2.print();
        EntityManager em = Persistence.createEntityManagerFactory("$objectdb/db/TEST.odb").createEntityManager();       
        em.getTransaction().begin();
        em.persist(H);
        em.getTransaction().commit();
        em.clear();
        TypedQuery<OBJ6> q = em.createQuery("SELECT h FROM OBJ6 h", OBJ6.class);
        List<OBJ6> res = q.getResultList();
        OBJ6 H3 = res.get(0);
        H3.print();
    }
}

and the Abstract class

package DBbug;

import java.io.Serializable;


abstract class ABS<Type1,Type2> implements Serializable{
    public Type1 valueInGenericClass;
   
    public ABS(){
    }

    public ABS(Type1 value){
        valueInGenericClass = value;
    }
   
   
    public Type1 getVal(){
        return valueInGenericClass;
    }
   
}
#2

Add the @Entity annotation to the abstract class in order to persist its fields.

ObjectDB Support
#3

You are right - it was a bad attempt from my part to identify a bug 

I just reported a different one (hopefully the right one)

http://www.objectdb.com/database/forum/811

Thanks

Reply