Bug: ClassCastException by retrieval

#1

Hi, when a generic array put into ObjectDB, its type changed to Object[]!

codes:

package test;

import java.util.Arrays;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Persistence;

public class TestGeneric {

/**
  * @param args
  */
public static void main(String[] args) {
  EntityManagerFactory emf = Persistence.createEntityManagerFactory("objectdb://127.0.0.1/trie.odb;user=admin;password=admin");
  EntityManager em = emf.createEntityManager();
  EntityTransaction trans = em.getTransaction();

  //* block-1
  trans.begin();
  tNode tn = new tNode();
  tn.setsData("HelloWorld2");
  em.persist(tn);
  System.out.printf("Original : %s %n",tn.getTa());
  trans.commit();//*/
 
  tNode tn2 = em.find(tNode.class, 1);
  System.out.printf("find instancely : %s %n",tn2.getTa());
  System.out.printf("Arrays : %s %n",Arrays.toString(tn2.getTa()));
  Character[] la = tn2.getTa();
  System.out.println(Arrays.toString(la));

  em.clear();
 
  tNode tn3 = em.find(tNode.class, 1);
  System.out.printf("find by retrieval : %s %n",tn3.getTa());
  System.out.printf("Arrays : %s %n",Arrays.toString(tn3.getTa()));
  Character[] la3 = tn3.getTa();
  System.out.println(Arrays.toString(la3));

  em.close();
  emf.close();
}
}

@Entity
class Node<TN> {

@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
private TN[] ta;

public TN[] getTa() {
  return ta;
}

public void setTa(TN[] ta) {
  this.ta = ta;
}
}

@Entity
class tNode extends Node<Character> {

String sData;

public String getsData() {
  return sData;
}

public void setsData(String sData) {
  this.sData = sData;
  char[] ca = sData.toCharArray();
  int iLen = ca.length;
  Character[] la = new Character[iLen];
  for (int ii = 0; ii < iLen; ii++) {
   la[ii] = new Character(ca[ii]);
  }
  setTa(la);
}

@Override
public String toString() {
  return Arrays.toString(getTa());
}
}

Result:

Original : H
find instancely : H
Arrays : [H, e, l, l, o, W, o, r, l, d, 2]
[H, e, l, l, o, W, o, r, l, d, 2]
find by retrieval : H
Arrays : [H, e, l, l, o, W, o, r, l, d, 2]
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Character;
at test.TestGeneric.main(TestGeneric.java:43)
#2

Hi, I believe the ObjectDB stores temporary setting for each class. Sometimes I create and retrieve generic arrays ok, but fail when I use Enhancer.

[ObjectDB 2.2.8_06 Enhancer]
2 persistable types have been enhanced:
    fpg.Nodes
    org.ict.trie.Node

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Long;
        at org.ict.trie.TrieMapNumber.add(TrieMapNumber.java:35)
        at org.ict.trie.TrieMapNumber.addArray(TrieMapNumber.java:9)
        at test.ExecMap.addchars(ExecMap.java:147)
        at test.ExecMap.addchars(ExecMap.java:154)
        at test.ExecMap.addchars(ExecMap.java:154)
        at test.ExecMap.addchars(ExecMap.java:154)
        at test.ExecMap.NormTest(ExecMap.java:233)
        at test.ExecMap.<init>(ExecMap.java:32)
        at test.ExecMap.main(ExecMap.java:304)
Error opening zip file or JAR manifest missing: /E:/projects/objectdb/bin/objectdb.jar
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
#3

It seems as a problem with persistent array fields that are defined using generics.

Generics in definition of entity classes (i.e. parameterized entity class) is only partly supported.

I will check if supporting this specific case could be added in one of the next builds.

ObjectDB Support
#4

Build 2.2.8_07 adds support of using array of parameterized type for persistent fields.

ObjectDB Support
#5

Hi, Thank you for so rapid patch!

Reply