You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Michał Morzywołek <ni...@gmail.com> on 2008/06/05 14:34:45 UTC

Relationships of unsaved object

How should i handle relationships of unsaved objects?

Let's say i got 2 classes: Person and Address. They are connected with
one-to-one relationship.
Problem is that after creating new object: Person (in normal way):

Person p=new Person(); //not registered in cayenne

after doing that i can't set any relationships (like with the address). (1)

After i register Person p in Cayenne i can set up relationships but only
with registered objects. (2)

Is there any way to do such things? (1)(2)
Or it is supposed to be like that?

I'm testing Hibernate, compering it with Cayenne and i miss Hibernate's
cascade savings.
In Hibernate i create:
Person p=new Person();
p.setAddress=new Address();
(...)
(...).save(p);

and p is saved (with the address)

Is there any way do do such things in Cayenne?

Thank you all,
Mike :)

Re: Relationships of unsaved object

Posted by Robert Zeigler <ro...@puregumption.com>.
Hi Mike,

Just to clarify, you can set an unregistered object as the target of a  
relationship without first registering the object; the trick is that  
the other end of the relationship has to be registered. :)

So you can't associated two unregistered objects together, but  
associating an unregistered object with a registered object will  
result in the auto-registration of your unregistered object.

As for DAO vs. cayenne "classic" architecture: Cayenne "classic" was  
modeled a lot after EOF.  So, you're right about different paradigms.

As for the merits of DAO... there are certainly merits to true DAO.   
But there are merits to the "classic cayenne" approach, as well.

As an example, a web application I wrote last year is capable, with  
two "pages/screens/whatever you want to call them" total, of adding,  
editing, and viewing over 30 different entities; the app also  
automatically "picks up" new objects added to the schema, and  
automatically picks up changes made to existing objects in the  
schema.  The "view" page lets you jump back and forth between entity  
relationships, and sorting on arbitrary attributes, and filtering.   
And the sorting all happens on the database side, as long as the  
property in question has an associated database path.  Although you  
could do this with a DAO approach, as well, cayenne's strengths of  
having rich objects, with rich metadata support, really go a long way  
to making this sort of thing doable.

Cheers,

Robert

On Jun 5, 2008, at 6/58:48 AM , Michał Morzywołek wrote:

> Thank you for your help.
>
> Of course i know about the newObject() method and so on :)
> What im really trying to do is to separate Caynne from POJOs so i  
> could set
> up "clear" DAO layer.
> I wanted to have "saving" instructions in the DAO objects - i was  
> going to
> use "registerObject()" method - and this is where the problems with
> relationships begin.
> I just have to registrate object to Cayenne before doing anything.In  
> my
> (maybe dumb) opinion is not exackly how DAO layer should work (have to
> "save" object before doing anything).
>
> As far as i understand Hibernate was created to fit DAO model - and  
> it works
> perfectly.
> Cayenne seems to have very different architecture in this matter.  
> With the
> need of registering the new objects in Cayenne (at the start) it's  
> hard to
> create perfectly "sterile" DAO layer.
> It is possible but its much slower then using newObject() method and  
> just
> "mixing it all".
> Cayenne wasn't created to work with "sterile" DAO layer, was it?:)
>
> Regards,
> Mike
>
> 2008/6/5 Aristedes Maniatis <ar...@ish.com.au>:
>
>>
>> On 05/06/2008, at 10:34 PM, Michał Morzywołek wrote:
>>
>> Person p=new Person(); //not registered in cayenne
>>>
>>> after doing that i can't set any relationships (like with the  
>>> address).
>>> (1)
>>>
>>> After i register Person p in Cayenne i can set up relationships  
>>> but only
>>> with registered objects. (2)
>>>
>>> Is there any way to do such things? (1)(2)
>>> Or it is supposed to be like that?
>>>
>>
>> You will need to create your new object like this:
>>
>> Person p = (Person) context.newObject(Person.class);
>>
>>
>> Cayenne 3 does have the ability to work with POJOs, but traditional  
>> Cayenne
>> API differs from Hibernate in this important way. Have a look at
>> http://cayenne.apache.org/doc/tutorial-dataobjects.html for more
>> information.
>>
>> Ari Maniatis
>>
>>
>>
>> -------------------------->
>> ish
>> http://www.ish.com.au
>> Level 1, 30 Wilson Street Newtown 2042 Australia
>> phone +61 2 9550 5001   fax +61 2 9550 4001
>> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>>
>>
>>


Re: Relationships of unsaved object

Posted by Andrus Adamchik <an...@objectstyle.org>.
As Ari said, we did come from a different world. We will have the  
option in 3.0 to build your graph first, and register it later, but  
currently you have to register objects before setting the  
relationships. (as a side note, I wouldn't yet recommend using POJO  
enhancer - too raw).

Andrus

On Jun 5, 2008, at 5:06 PM, Aristedes Maniatis wrote:

>
> On 05/06/2008, at 11:48 PM, Michał Morzywołek wrote:
>
>> As far as i understand Hibernate was created to fit DAO model - and  
>> it works
>> perfectly.
>> Cayenne seems to have very different architecture in this matter.  
>> With the
>> need of registering the new objects in Cayenne (at the start) it's  
>> hard to
>> create perfectly "sterile" DAO layer.
>> It is possible but its much slower then using newObject() method  
>> and just
>> "mixing it all".
>
>
> Not sure what you mean by sterile, but Cayenne has a different  
> history and different strengths to Hibernate. Registering new  
> objects from the point of instantiation actually does have some  
> advantages and I think it is a pattern you'll quickly grow to like.  
> But yes, it is different and is not the JPA/Hibernate way. It means  
> that objects are always tracked by Cayenne through their entire  
> life, and they are always attached to a context.
>
> If you really want, there is a POJO enhancer available in Cayenne 3  
> as part of the JPA compliance work. Documentation is still a bit  
> thin at this time.
>
> Ari
>
>
> -------------------------->
> ish
> http://www.ish.com.au
> Level 1, 30 Wilson Street Newtown 2042 Australia
> phone +61 2 9550 5001   fax +61 2 9550 4001
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>
>
>


Re: Relationships of unsaved object

Posted by Michał Morzywołek <ni...@gmail.com>.
Thank you for all are advices and comments :) I understand that Cayenne is
different, not worse that Hibernate:) That's why im learning it :)

Regards,
Mike

2008/6/5 Aristedes Maniatis <ar...@ish.com.au>:

>
> On 05/06/2008, at 11:48 PM, Michał Morzywołek wrote:
>
>  As far as i understand Hibernate was created to fit DAO model - and it
>> works
>> perfectly.
>> Cayenne seems to have very different architecture in this matter. With the
>> need of registering the new objects in Cayenne (at the start) it's hard to
>> create perfectly "sterile" DAO layer.
>> It is possible but its much slower then using newObject() method and just
>> "mixing it all".
>>
>
>
> Not sure what you mean by sterile, but Cayenne has a different history and
> different strengths to Hibernate. Registering new objects from the point of
> instantiation actually does have some advantages and I think it is a pattern
> you'll quickly grow to like. But yes, it is different and is not the
> JPA/Hibernate way. It means that objects are always tracked by Cayenne
> through their entire life, and they are always attached to a context.
>
> If you really want, there is a POJO enhancer available in Cayenne 3 as part
> of the JPA compliance work. Documentation is still a bit thin at this time.
>
> Ari
>
>
>
> -------------------------->
> ish
> http://www.ish.com.au
> Level 1, 30 Wilson Street Newtown 2042 Australia
> phone +61 2 9550 5001   fax +61 2 9550 4001
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>
>
>

Re: Relationships of unsaved object

Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 05/06/2008, at 11:48 PM, Michał Morzywołek wrote:

> As far as i understand Hibernate was created to fit DAO model - and  
> it works
> perfectly.
> Cayenne seems to have very different architecture in this matter.  
> With the
> need of registering the new objects in Cayenne (at the start) it's  
> hard to
> create perfectly "sterile" DAO layer.
> It is possible but its much slower then using newObject() method and  
> just
> "mixing it all".


Not sure what you mean by sterile, but Cayenne has a different history  
and different strengths to Hibernate. Registering new objects from the  
point of instantiation actually does have some advantages and I think  
it is a pattern you'll quickly grow to like. But yes, it is different  
and is not the JPA/Hibernate way. It means that objects are always  
tracked by Cayenne through their entire life, and they are always  
attached to a context.

If you really want, there is a POJO enhancer available in Cayenne 3 as  
part of the JPA compliance work. Documentation is still a bit thin at  
this time.

Ari


-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A



Re: Relationships of unsaved object

Posted by Michał Morzywołek <ni...@gmail.com>.
Thank you for your help.

Of course i know about the newObject() method and so on :)
What im really trying to do is to separate Caynne from POJOs so i could set
up "clear" DAO layer.
I wanted to have "saving" instructions in the DAO objects - i was going to
use "registerObject()" method - and this is where the problems with
relationships begin.
I just have to registrate object to Cayenne before doing anything.In my
(maybe dumb) opinion is not exackly how DAO layer should work (have to
"save" object before doing anything).

As far as i understand Hibernate was created to fit DAO model - and it works
perfectly.
Cayenne seems to have very different architecture in this matter. With the
need of registering the new objects in Cayenne (at the start) it's hard to
create perfectly "sterile" DAO layer.
It is possible but its much slower then using newObject() method and just
"mixing it all".
Cayenne wasn't created to work with "sterile" DAO layer, was it?:)

Regards,
Mike

2008/6/5 Aristedes Maniatis <ar...@ish.com.au>:

>
> On 05/06/2008, at 10:34 PM, Michał Morzywołek wrote:
>
>  Person p=new Person(); //not registered in cayenne
>>
>> after doing that i can't set any relationships (like with the address).
>> (1)
>>
>> After i register Person p in Cayenne i can set up relationships but only
>> with registered objects. (2)
>>
>> Is there any way to do such things? (1)(2)
>> Or it is supposed to be like that?
>>
>
> You will need to create your new object like this:
>
> Person p = (Person) context.newObject(Person.class);
>
>
> Cayenne 3 does have the ability to work with POJOs, but traditional Cayenne
> API differs from Hibernate in this important way. Have a look at
> http://cayenne.apache.org/doc/tutorial-dataobjects.html for more
> information.
>
> Ari Maniatis
>
>
>
> -------------------------->
> ish
> http://www.ish.com.au
> Level 1, 30 Wilson Street Newtown 2042 Australia
> phone +61 2 9550 5001   fax +61 2 9550 4001
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>
>
>

Re: Relationships of unsaved object

Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 05/06/2008, at 10:34 PM, Michał Morzywołek wrote:

> Person p=new Person(); //not registered in cayenne
>
> after doing that i can't set any relationships (like with the  
> address). (1)
>
> After i register Person p in Cayenne i can set up relationships but  
> only
> with registered objects. (2)
>
> Is there any way to do such things? (1)(2)
> Or it is supposed to be like that?

You will need to create your new object like this:

Person p = (Person) context.newObject(Person.class);


Cayenne 3 does have the ability to work with POJOs, but traditional  
Cayenne API differs from Hibernate in this important way. Have a look  
at http://cayenne.apache.org/doc/tutorial-dataobjects.html for more  
information.

Ari Maniatis



-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A