Home » Support » Forum » Field in Generic Class not persisted?

Field in Generic Class not persisted?



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



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;

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

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

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

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

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;

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

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

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)



Post Reply

To post a reply and/or subscribe to update notifications - please login