368 words

Bug: ClassCastException by retrieval

#1
2011-07-19 13:10

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)
Institute of Computer Technologies
gzdillon
gzdillon's picture
Joined on 2011-05-17
User Post #55
#2
2011-07-19 14:07

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)
Institute of Computer Technologies
gzdillon
gzdillon's picture
Joined on 2011-05-17
User Post #56
#3
2011-07-22 00:13

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)
support
support's picture
Joined on 2010-05-03
User Post #468
#4
2011-07-24 01:21

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)
support
support's picture
Joined on 2010-05-03
User Post #471
#5
2011-07-25 03:26

Hi, Thank you for so rapid patch!

Institute of Computer Technologies
gzdillon
gzdillon's picture
Joined on 2011-05-17
User Post #57

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel