Home » Eclipse Projects » EclipseLink » JPA Dynamic Models
JPA Dynamic Models [message #664133] |
Thu, 07 April 2011 15:03 |
rpvilao Messages: 2 Registered: April 2011 |
Junior Member |
|
|
Hi all,
I'm trying to get dynamic models to work in eclipse link but I'm having some problems and some questions as well. Let's start with the questions:
1) Is it possible using DynamicEntities to insert in the database? i.e. call em.persist() using a newly created object a new entry in the database. It seems to me that the answer is yes, but I haven't seen any example on how to do it.
2) When I create this dynamic class and add it to the classloader will it create the schema in the database? I want to add some class dynamically and "sync" the database schema, in this case, create the table, sequence etc if it doesn't exist.
Now what I've been trying:
In my persistence.xml I've the connection do the database properties.
Then I'm creating the dynamic entity like the following:
DynamicClassLoader dcl = new DynamicClassLoader(thiz.getClass().getClassLoader());
Class<?> testClass = dcl.createDynamicClass("org.persistence.Test");
JPADynamicTypeBuilder test = new JPADynamicTypeBuilder(testClass, null, "TEST");
test.addDirectMapping("id", long.class, "T_ID");
test.addDirectMapping("col1", long.class, "T_COL1");
test.addDirectMapping("col2", int.class, "T_COL2");
test.addDirectMapping("col3", String.class, "T_COL3");
test.addDirectMapping("col4", String.class, "T_COL4");
test.addDirectMapping("col5", double.class, "T_COL5");
test.addDirectMapping("col6", double.class, "T_COL6");
test.configureSequencing("TEST_SEQ", "T_ID");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
properties.put(PersistenceUnitProperties.WEAVING, "static");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("default", properties);
EntityManager em = emf.createEntityManager();
And I know that the class is being compiled and in fact is in the classloader, but when I try to create an instance:
DynamicHelper helper = new JPADynamicHelper(em);
DynamicEntity newDynamicEntity = helper.newDynamicEntity("org.persistence.Test");
I get
Exception in thread "main" java.lang.IllegalArgumentException: DynamicHelper.createQuery: Dynamic type not found: org.persistence.Test
at org.eclipse.persistence.dynamic.DynamicHelper.newDynamicEntity(DynamicHelper.java:122)
at com.company.pulse.workshop.ebeantest.EclipseLinkOracle.main(EclipseLinkOracle.java:78)
Does anyone have a clue on how can I getg this to work? Some tutorial explaining how to configure and persist new DynamicEntities and not only how to fetch from the database?
Thanks in advance,
Rui
|
|
|
Re: JPA Dynamic Models [message #666844 is a reply to message #664133] |
Sat, 23 April 2011 07:19 |
Eclipse User |
|
|
|
Originally posted by: gregor.karl.frey.t-online.de
You find a full example on how to use the dynamic features of
eclipselink here:
http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/br anches/2.1/trunk/examples/jpa.employee/eclipselink.example.j pa.employee.dynamic/src/example/
The important thing to note are the line 58, 59 in Main.java:
JPADynamicHelper helper = new JPADynamicHelper(emf);
helper.addTypes(true, true, types);
You must add the DynamicTypes to the session.
Gregor
On 07.04.2011 17:03, rpvilao wrote:
> Hi all,
>
> I'm trying to get dynamic models to work in eclipse link but I'm having
> some problems and some questions as well. Let's start with the questions:
>
> 1) Is it possible using DynamicEntities to insert in the database? i.e.
> call em.persist() using a newly created object a new entry in the
> database. It seems to me that the answer is yes, but I haven't seen any
> example on how to do it.
>
> 2) When I create this dynamic class and add it to the classloader will
> it create the schema in the database? I want to add some class
> dynamically and "sync" the database schema, in this case, create the
> table, sequence etc if it doesn't exist.
>
> Now what I've been trying:
>
> In my persistence.xml I've the connection do the database properties.
>
> Then I'm creating the dynamic entity like the following:
>
>
> DynamicClassLoader dcl = new
> DynamicClassLoader(thiz.getClass().getClassLoader());
>
>
> Class<?> testClass = dcl.createDynamicClass("org.persistence.Test");
> JPADynamicTypeBuilder test = new JPADynamicTypeBuilder(testClass, null,
> "TEST");
> test.addDirectMapping("id", long.class, "T_ID");
> test.addDirectMapping("col1", long.class, "T_COL1");
> test.addDirectMapping("col2", int.class, "T_COL2");
> test.addDirectMapping("col3", String.class, "T_COL3");
> test.addDirectMapping("col4", String.class, "T_COL4");
> test.addDirectMapping("col5", double.class, "T_COL5");
> test.addDirectMapping("col6", double.class, "T_COL6");
> test.configureSequencing("TEST_SEQ", "T_ID");
>
> Map<String, Object> properties = new HashMap<String, Object>();
>
> properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
> properties.put(PersistenceUnitProperties.WEAVING, "static");
>
>
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("default", properties);
> EntityManager em = emf.createEntityManager();
>
>
> And I know that the class is being compiled and in fact is in the
> classloader, but when I try to create an instance:
>
>
> DynamicHelper helper = new JPADynamicHelper(em);
> DynamicEntity newDynamicEntity =
> helper.newDynamicEntity("org.persistence.Test");
>
>
> I get
>
>
> Exception in thread "main" java.lang.IllegalArgumentException:
> DynamicHelper.createQuery: Dynamic type not found: org.persistence.Test
> at
> org.eclipse.persistence.dynamic.DynamicHelper.newDynamicEnti ty(DynamicHelper.java:122)
>
> at
> com.company.pulse.workshop.ebeantest.EclipseLinkOracle.main( EclipseLinkOracle.java:78)
>
>
>
> Does anyone have a clue on how can I getg this to work? Some tutorial
> explaining how to configure and persist new DynamicEntities and not only
> how to fetch from the database?
>
> Thanks in advance,
>
> Rui
|
|
|
Goto Forum:
Current Time: Fri Apr 26 22:58:16 GMT 2024
Powered by FUDForum. Page generated in 0.03507 seconds
|