You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by "mr.abanjo" <mr...@gmail.com> on 2007/07/07 00:12:57 UTC

How to create cayenne datamap on the fly

Hi all,
i'm writing an application that try to be indipendent from the business
logic required.
The main idea behind this project is to use reflection to create on the fly
the bean (CayenneDataObject) that normally i must write for map the databese
structure.
What i need is:
1) load cayenne data map xml from a URL. Basically this URL point to a
servlet that generate it on the fly. Normally this is the configuration in
cayenne.xml:

<map name="datamap-foo" location="META-INF/cayenne-datamap-foo.map.xml" />

whant i need is something like this:

 <map name="datamap-foo" location="
http://somedomain/cayenne-datamap-foo.map.xml" />

is it possible?

2) i need to generate some DataObject on the fly. So i'm thinking to use
Reflection. Do you think that is possible?  The main idea is that at runtime
i don't know the structure of my entity. For example, "user" can be composed
only by "name" and "surname" attribute... or... have also "address".. and so
on. I don't want to create every time the corresponding DataObject. I want
to implements an engine that create them on demand.

I don't know if this is a right approch on the problem. Maybe cayenne has
some API for doing this type of activity.

Thanks in advance, for help.

D.

Re: How to create cayenne datamap on the fly

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Jul 8, 2007, at 7:17 PM, mr.abanjo wrote:

> So.. i think this is the main difference from
> the "normal" datamap file and the one required for my scope. Is it  
> the only
> one...

Yes - that's exactly the thing that hints Cayenne to use generic class.

Andrus


Re: How to create cayenne datamap on the fly

Posted by "mr.abanjo" <mr...@gmail.com>.
Hi Andrus,
thanks for the sample code.
Sorry for my stupid question :-D... yes.. i try to generate datamap on the
fly via servlet.
About point 2... i noticed (in the sample) that datamap xml file (obj-entity)
hasn't "className" attribute. So.. i think this is the main difference from
the "normal" datamap file and the one required for my scope. Is it the only
one... or i must take care about other differences?

Thanks again for you support.

D.


On 7/8/07, Andrus Adamchik <an...@objectstyle.org> wrote:
>
>
> On Jul 8, 2007, at 2:10 PM, mr.abanjo wrote:
>
> > Hi Andrus,
> > few further questions for you:
> >
> > 1) The web page "
> > http://cayenne.apache.org/doc20/generic-persistent-class.html" has
> > a link to
> > a sample code.. but it desn't work. A sample,is for me, a great
> > help for
> > speed up my work. Is possible to have it?
>
> Good catch, here is the correct link:
>
> http://cwiki.apache.org/CAY/generic-dataobjects-example.html
>
>
> > 2) The datamap xml file, that should map java object to database
> > object, is
> > still required? I usually write in it, the package and the name of the
> > concrete class..
>
> Isn't it what you were going to generate via a servlet dynamically?
> Of course whatever the servlet generates has to look no different
> from what the modeler would generate, so yes - entries for obj-
> entities and db-entities are required.
>
>
> > 3) If the datamap xml file is no more required, how Cayenne know which
> > table  are connected to my java classes, how it knows which is the
> > primary
> > key.. ecc...?
>
> It doesn't, see above.
>
> Andrus
>

Re: How to create cayenne datamap on the fly

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Jul 8, 2007, at 2:10 PM, mr.abanjo wrote:

> Hi Andrus,
> few further questions for you:
>
> 1) The web page "
> http://cayenne.apache.org/doc20/generic-persistent-class.html" has  
> a link to
> a sample code.. but it desn't work. A sample,is for me, a great  
> help for
> speed up my work. Is possible to have it?

Good catch, here is the correct link:

http://cwiki.apache.org/CAY/generic-dataobjects-example.html


> 2) The datamap xml file, that should map java object to database  
> object, is
> still required? I usually write in it, the package and the name of the
> concrete class..

Isn't it what you were going to generate via a servlet dynamically?  
Of course whatever the servlet generates has to look no different  
from what the modeler would generate, so yes - entries for obj- 
entities and db-entities are required.


> 3) If the datamap xml file is no more required, how Cayenne know which
> table  are connected to my java classes, how it knows which is the  
> primary
> key.. ecc...?

It doesn't, see above.

Andrus

Re: How to create cayenne datamap on the fly

Posted by "mr.abanjo" <mr...@gmail.com>.
Hi Andrus,
few further questions for you:

1) The web page "
http://cayenne.apache.org/doc20/generic-persistent-class.html" has a link to
a sample code.. but it desn't work. A sample,is for me, a great help for
speed up my work. Is possible to have it?

2) The datamap xml file, that should map java object to database object, is
still required? I usually write in it, the package and the name of the
concrete class... now what i must insert?


<db-entity name="FOOENTITY">
<db-attribute name="DBFIELD1" type="INTEGER" isPrimaryKey="true"/>
<db-attribute name="DBFIELD2" type="VARCHAR"/>
...
...
</db-entity>

<obj-entity name="FooEntity" readOnly="false" className="*
it.foo.type.FooEntityType*" dbEntityName="FOOENTITY">
<obj-attribute name="DbFiled1" type="java.lang.Long"
db-attribute-path="DBFIELD1" />
<obj-attribute name="DbFiled2" type="java.lang.String"
db-attribute-path="DBFIELD2" />
...
...
</obj-entity>


3) If the datamap xml file is no more required, how Cayenne know which
table  are connected to my java classes, how it knows which is the primary
key.. ecc...?


Thanks
D.



On 7/8/07, mr. abanjo <mr...@gmail.com> wrote:
>
> Hi Andrus,
> thanks for your help!
> Every time i need something, Cayenne solve my problem :D
>
> D.
>
>
>
> On 7/7/07, Andrus Adamchik <an...@objectstyle.org> wrote:
> >
> > Hi there,
> >
> > Actually Cayenne is one of the few ORM engines where you can define
> > both your metadata, and persistent objects on the fly if you want to.
> > So...
> >
> > 1. While I never tried loading DataMap XML from a remote URL, it
> > should work. Just subclass
> > 'org.objectstyle.cayenne.conf.DefaultConfiguration', overriding
> > 'getMapConfiguration' method to open a remote URL. You may find this
> > page useful when doing that:
> >
> > http://cayenne.apache.org/doc20/customizing-configuration.html
> >
> > 2. Yes, you can totally do that using generic persistent classes:
> >
> > http://cayenne.apache.org/doc20/generic-persistent-class.html
> >
> > The main trick is to access the objects via DataObject interface
> > methods instead of concrete setters and getters.
> >
> > Andrus
> >
> >
> > On Jul 7, 2007, at 1:12 AM, mr.abanjo wrote:
> >
> > > Hi all,
> > > i'm writing an application that try to be indipendent from the
> > > business
> > > logic required.
> > > The main idea behind this project is to use reflection to create on
> > > the fly
> > > the bean (CayenneDataObject) that normally i must write for map the
> > > databese
> > > structure.
> > > What i need is:
> > > 1) load cayenne data map xml from a URL. Basically this URL point to a
> > > servlet that generate it on the fly. Normally this is the
> > > configuration in
> > > cayenne.xml:
> > >
> > > <map name="datamap-foo" location="META-INF/cayenne-datamap-
> > > foo.map.xml" />
> > >
> > > whant i need is something like this:
> > >
> > > <map name="datamap-foo" location="
> > > http://somedomain/cayenne-datamap-foo.map.xml" />
> > >
> > > is it possible?
> > >
> > > 2) i need to generate some DataObject on the fly. So i'm thinking
> > > to use
> > > Reflection. Do you think that is possible?  The main idea is that
> > > at runtime
> > > i don't know the structure of my entity. For example, "user" can be
> > > composed
> > > only by "name" and "surname" attribute... or... have also
> > > "address".. and so
> > > on. I don't want to create every time the corresponding DataObject.
> > > I want
> > > to implements an engine that create them on demand.
> > >
> > > I don't know if this is a right approch on the problem. Maybe
> > > cayenne has
> > > some API for doing this type of activity.
> > >
> > > Thanks in advance, for help.
> > >
> > > D.
> >
> >
>

Re: How to create cayenne datamap on the fly

Posted by "mr.abanjo" <mr...@gmail.com>.
Hi Andrus,
thanks for your help!
Every time i need something, Cayenne solve my problem :D

D.



On 7/7/07, Andrus Adamchik <an...@objectstyle.org> wrote:
>
> Hi there,
>
> Actually Cayenne is one of the few ORM engines where you can define
> both your metadata, and persistent objects on the fly if you want to.
> So...
>
> 1. While I never tried loading DataMap XML from a remote URL, it
> should work. Just subclass
> 'org.objectstyle.cayenne.conf.DefaultConfiguration', overriding
> 'getMapConfiguration' method to open a remote URL. You may find this
> page useful when doing that:
>
> http://cayenne.apache.org/doc20/customizing-configuration.html
>
> 2. Yes, you can totally do that using generic persistent classes:
>
> http://cayenne.apache.org/doc20/generic-persistent-class.html
>
> The main trick is to access the objects via DataObject interface
> methods instead of concrete setters and getters.
>
> Andrus
>
>
> On Jul 7, 2007, at 1:12 AM, mr.abanjo wrote:
>
> > Hi all,
> > i'm writing an application that try to be indipendent from the
> > business
> > logic required.
> > The main idea behind this project is to use reflection to create on
> > the fly
> > the bean (CayenneDataObject) that normally i must write for map the
> > databese
> > structure.
> > What i need is:
> > 1) load cayenne data map xml from a URL. Basically this URL point to a
> > servlet that generate it on the fly. Normally this is the
> > configuration in
> > cayenne.xml:
> >
> > <map name="datamap-foo" location="META-INF/cayenne-datamap-
> > foo.map.xml" />
> >
> > whant i need is something like this:
> >
> > <map name="datamap-foo" location="
> > http://somedomain/cayenne-datamap-foo.map.xml" />
> >
> > is it possible?
> >
> > 2) i need to generate some DataObject on the fly. So i'm thinking
> > to use
> > Reflection. Do you think that is possible?  The main idea is that
> > at runtime
> > i don't know the structure of my entity. For example, "user" can be
> > composed
> > only by "name" and "surname" attribute... or... have also
> > "address".. and so
> > on. I don't want to create every time the corresponding DataObject.
> > I want
> > to implements an engine that create them on demand.
> >
> > I don't know if this is a right approch on the problem. Maybe
> > cayenne has
> > some API for doing this type of activity.
> >
> > Thanks in advance, for help.
> >
> > D.
>
>

Re: How to create cayenne datamap on the fly

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi there,

Actually Cayenne is one of the few ORM engines where you can define  
both your metadata, and persistent objects on the fly if you want to.  
So...

1. While I never tried loading DataMap XML from a remote URL, it  
should work. Just subclass  
'org.objectstyle.cayenne.conf.DefaultConfiguration', overriding  
'getMapConfiguration' method to open a remote URL. You may find this  
page useful when doing that:

http://cayenne.apache.org/doc20/customizing-configuration.html

2. Yes, you can totally do that using generic persistent classes:

http://cayenne.apache.org/doc20/generic-persistent-class.html

The main trick is to access the objects via DataObject interface  
methods instead of concrete setters and getters.

Andrus


On Jul 7, 2007, at 1:12 AM, mr.abanjo wrote:

> Hi all,
> i'm writing an application that try to be indipendent from the  
> business
> logic required.
> The main idea behind this project is to use reflection to create on  
> the fly
> the bean (CayenneDataObject) that normally i must write for map the  
> databese
> structure.
> What i need is:
> 1) load cayenne data map xml from a URL. Basically this URL point to a
> servlet that generate it on the fly. Normally this is the  
> configuration in
> cayenne.xml:
>
> <map name="datamap-foo" location="META-INF/cayenne-datamap- 
> foo.map.xml" />
>
> whant i need is something like this:
>
> <map name="datamap-foo" location="
> http://somedomain/cayenne-datamap-foo.map.xml" />
>
> is it possible?
>
> 2) i need to generate some DataObject on the fly. So i'm thinking  
> to use
> Reflection. Do you think that is possible?  The main idea is that  
> at runtime
> i don't know the structure of my entity. For example, "user" can be  
> composed
> only by "name" and "surname" attribute... or... have also  
> "address".. and so
> on. I don't want to create every time the corresponding DataObject.  
> I want
> to implements an engine that create them on demand.
>
> I don't know if this is a right approch on the problem. Maybe  
> cayenne has
> some API for doing this type of activity.
>
> Thanks in advance, for help.
>
> D.