How to create dynamic Entity class.

#1

Hello,

     I was creating dynamic object to store data inside database. I am using javassist API to create dynamic class. Here i have written some code which is create dynamic class as well as object. Class and object successfully created. And insert data also now problem is only once object is created and insert data only first time. when i am execute is it next time it throws following exception.:

Exception in thread "main" [ObjectDB 2.7.5] javax.persistence.RollbackException
Failed to commit transaction: Failed to get reference value of field field DClass.Field_1 using enhanced method (error 613)
    at com.objectdb.jpa.EMImpl.commit(EMImpl.java:289)
    at packone.DMain.main(DMain.java:53)
Caused by: javax.persistence.PersistenceException: com.objectdb.o.UserException: Failed to get reference value of field field DClass.Field_1 using enhanced method
    at com.objectdb.o._PersistenceException.a(_PersistenceException.java:47)
    at com.objectdb.o.JPE.d(JPE.java:147)
    at com.objectdb.o.JPE.d(JPE.java:80)
    ... 4 more
Caused by: com.objectdb.o.UserException: Failed to get reference value of field field DClass.Field_1 using enhanced method
    at com.objectdb.o.MSG.a(MSG.java:77)
    at com.objectdb.o.UMR.s(UMR.java:936)
    at com.objectdb.o.UMR.g(UMR.java:721)
    at com.objectdb.o.ENT.j(ENT.java:458)
    at com.objectdb.o.ENT.j(ENT.java:388)
    at com.objectdb.o.STA.L(STA.java:875)
    at com.objectdb.o.STM.j(STM.java:598)
    at com.objectdb.o.OBM.aC(OBM.java:1086)
    at com.objectdb.o.OBM.aS(OBM.java:867)
    at com.objectdb.o.OBM.aG(OBM.java:755)
    at com.objectdb.jpa.EMImpl.commit(EMImpl.java:286)
    ... 1 more
Caused by: java.lang.ClassCastException: DClass cannot be cast to com.objectdb.spi.TrackableUserType
    at com.objectdb.o.UMR.h(UMR.java:750)
    at com.objectdb.o.UMR.g(UMR.java:715)
    ... 9 more

Thank you.

#2

The ClassCastException is probably due to a ClassLoader conflict.

If you provide a complete minimal test case (see posting instructions for required format) we may be able to explore the cause.

ObjectDB Support
#3

Hi,

 I understood your criteria for posting objectDB forum, i will obey all the rules at the time of posting the doubts.

So, I am adding that test case, where i am facing the problems, i have attach source code below. Please see my source code.

#4

Hi,

I am depending upon your solution. Any solution regarding to my question ?.

#5

A ClassCastException is thrown because of a ClassLoader conflict, i.e. the same class is defined by both your class loader and ObjectDB class loader (when instances of the class are already stored in the database).

Use  Thread.currentThread().setContextClassLoader(...) before connecting to ObjectDB to guide ObjectDB to use your class loader.

Changing the code in your test main as follows seems to solve the exception:

    String dbUrl = "$objectdb/db/dtest_db.odb";

    MyClassLoader dcl = new MyClassLoader(DMain.class.getClassLoader());
    Class myCls = dcl.findClass("DClass");

    Thread.currentThread().setContextClassLoader(dcl);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(dbUrl);
    EntityManager _entyMgr = emf.createEntityManager();

    Object entity = myCls.newInstance();
ObjectDB Support
#6

Now it is working.

     Thank You.

Reply