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)