You can use Serp  -- a bytecode manipulation library --
(http://serp.sourceforge.net/). OpenJPA itself uses it to enhance class
files.

To enhance the dynamically defined class and register it to OpenJPA
persistence unit requires few considerations. The attached example
demonstrates these steps with a simple example that 
a) defines a class dynamically
b) enhances
c) defines database mapping for it
d) pouplates instance of the defined class 
and finally persists it.




Pinaki Poddar
BEA Systems
415.402.7317  


-----Original Message-----
From: James Hang [mailto:[EMAIL PROTECTED] 
Sent: Monday, May 14, 2007 1:18 PM
To: open-jpa-dev@incubator.apache.org
Subject: RE: Entity creation at Runtime

Thanks for the reply Marc.

Is there a third party tool that can generate classes with annotations?
Or is there something in OpenJPA that can do that?  I noticed a
CodeGenerator class that can generate classes from metadata.  Can I use
that?

I'll take a look at the PCClassFileTransformer class like you suggested.

Thanks for your help!

James



-----Original Message-----
From: Marc Prud'hommeaux [mailto:[EMAIL PROTECTED] On Behalf Of
Marc Prud'hommeaux
Sent: Monday, May 14, 2007 10:30 AM
To: open-jpa-dev@incubator.apache.org
Subject: Re: Entity creation at Runtime

James-

It should be possible. The easiest way (and the most implementation-
agnostic) would be to simply generate the classes with the appropriate
mapping annotations, and then just load them at runtime.

The somewhat tricky part would be to ensure they get enhanced by the
system when they are loaded. You could just run the -
javaagent:openjpa.jar flag with no arguments, and I believe it will scan
all loaded classes for annotations, but that can slow things down quite
a bit. A faster solution would be to run your generated classes through
the enhancer.

Take a look at the code for
org.apache.openjpa.enhance.PCClassFileTransformer ... if you can't use
that directly, then it might be good to use as a starting point.



On May 14, 2007, at 10:00 AM, James Hang wrote:

> Hi,
>
> I'm curious if it's possible to create and map new entities at 
> Runtime?
> We have an application which allows users to design new data models at

> runtime.  When new tables are being created in the database, we would 
> like to make OpenJPA aware of them.  We are currently doing this in 
> Hibernate by generating the POJO classes dynamically using CGLIB, 
> adding dynamically generated xml mappings to the Hibernate runtime 
> configuration and re-initializing the SessionFactory.  Is there a way 
> to do something similar in OpenJPA?
>
> Thanks,
>
> James
>
>
> Notice:  This email message, together with any attachments, may  
> contain information  of  BEA Systems,  Inc.,  its subsidiaries   
> and  affiliated entities,  that may be confidential,  proprietary,   
> copyrighted  and/or legally privileged, and is intended solely for the

> use of the individual or entity named in this message. If you are not 
> the intended recipient, and have received this message in error, 
> please immediately return this by email and then delete it.


Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.

Notice:  This email message, together with any attachments, may contain 
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated 
entities,  that may be confidential,  proprietary,  copyrighted  and/or legally 
privileged, and is intended solely for the use of the individual or entity 
named in this message. If you are not the intended recipient, and have received 
this message in error, please immediately return this by email and then delete 
it.

Reply via email to