Home » Support » Forum » Bug: ClassCastException by retrieval

Bug: ClassCastException by retrieval


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


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://;user=admin;password=admin");
  EntityManager em = emf.createEntityManager();
  EntityTransaction trans = em.getTransaction();

  //* block-1
  tNode tn = new tNode();
  System.out.printf("Original : %s %n",tn.getTa());
  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();

  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();


class Node<TN> {

int id;
private TN[] ta;

public TN[] getTa() {
  return ta;

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

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]);

public String toString() {
  return Arrays.toString(getTa());


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)
Institute of Computer Technologies

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:

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)
Institute of Computer Technologies

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
ObjectDB - Fast Object Database for Java (JPA/JDO)

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

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

Hi, Thank you for so rapid patch!

Institute of Computer Technologies

Post Reply

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