You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Tim Hanson <th...@bea.com> on 2007/07/31 18:43:12 UTC

When does merge() update

I somewhat confused by the behaviour of merge(). It seems that if I just
new up an entity and set its fields, including the primary key, then I
call em.merge(), INSERT is called instead of UPDATE. Is this true? 

I am using JPA with JSF. I have a backing bean with a reference to an
entity. When I try to edit the entity, on save JSF fills in the fields
of the entity from the request and then I call em.merge(), but like I
said, that always calls INSERT. The only way I was able to make this
work was by scoping the backing bean to the session, such that my entity
was alive between calls. That seems like a strange thing to do.

The other possibility would be to find the entity and then copy the
fields. That also seems like a hack.

I guess if merge() is not really UPDATE, is there something else I can
call?

Thanks,
Tim

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.

Re: When does merge() update

Posted by Patrick Linskey <pl...@gmail.com>.
Note that there is already an OpenJPA wiki at
http://openjpa.apache.org -- that site is an HTML export of the
OpenJPA section of the Apache wiki. So, you can easily add any content
to that area.

-Patrick

On 8/2/07, Mauricio Salatino <sa...@gmail.com> wrote:
> i good like to contribute with that..
> but we need people to do a wiki
>
> On 8/2/07, Gene Wu <ge...@hotmail.com> wrote:
> >
> > I often use Mind map to facilitate something I want to keep in my memory. So
> > do I work on project and technical research. Also, there is a open source
> > mind map tool, named Freemind. Maybe someone can create one in OpenJPA wiki.
> >
> > Gene
> >
> >
> > Craig L Russell wrote:
> > >
> > > Sorry. I don't have anything like that. For this kind of issue I'd
> > > first go through the tutorials that the vendors have put a lot of
> > > work into.
> > >
> > > Anyone else with an opinion on the best tutorials available?
> > >
> > > Craig
> > >
> > > On Jul 31, 2007, at 11:24 AM, Mauricio Salatino wrote:
> > >
> > >> craig do you have any presentation (powerpoint or something) about
> > >> good and best practice in jpa? (openjpa)
> > >>
> > >
> > >
> > > Craig Russell
> > > Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> > > 408 276-5638 mailto:Craig.Russell@sun.com
> > > P.S. A good JDO? O, Gasp!
> > >
> > >
> > >
> > >
> >
> > --
> > View this message in context: http://www.nabble.com/When-does-merge%28%29-update-tf4194235.html#a11959806
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
> >
>
>
> --
>  - Salatino Mauricio -
>


-- 
Patrick Linskey
202 669 5907

Re: When does merge() update

Posted by Mauricio Salatino <sa...@gmail.com>.
i good like to contribute with that..
but we need people to do a wiki

On 8/2/07, Gene Wu <ge...@hotmail.com> wrote:
>
> I often use Mind map to facilitate something I want to keep in my memory. So
> do I work on project and technical research. Also, there is a open source
> mind map tool, named Freemind. Maybe someone can create one in OpenJPA wiki.
>
> Gene
>
>
> Craig L Russell wrote:
> >
> > Sorry. I don't have anything like that. For this kind of issue I'd
> > first go through the tutorials that the vendors have put a lot of
> > work into.
> >
> > Anyone else with an opinion on the best tutorials available?
> >
> > Craig
> >
> > On Jul 31, 2007, at 11:24 AM, Mauricio Salatino wrote:
> >
> >> craig do you have any presentation (powerpoint or something) about
> >> good and best practice in jpa? (openjpa)
> >>
> >
> >
> > Craig Russell
> > Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> > 408 276-5638 mailto:Craig.Russell@sun.com
> > P.S. A good JDO? O, Gasp!
> >
> >
> >
> >
>
> --
> View this message in context: http://www.nabble.com/When-does-merge%28%29-update-tf4194235.html#a11959806
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>


-- 
 - Salatino Mauricio -

Re: When does merge() update

Posted by Gene Wu <ge...@hotmail.com>.
I often use Mind map to facilitate something I want to keep in my memory. So
do I work on project and technical research. Also, there is a open source
mind map tool, named Freemind. Maybe someone can create one in OpenJPA wiki.

Gene


Craig L Russell wrote:
> 
> Sorry. I don't have anything like that. For this kind of issue I'd  
> first go through the tutorials that the vendors have put a lot of  
> work into.
> 
> Anyone else with an opinion on the best tutorials available?
> 
> Craig
> 
> On Jul 31, 2007, at 11:24 AM, Mauricio Salatino wrote:
> 
>> craig do you have any presentation (powerpoint or something) about
>> good and best practice in jpa? (openjpa)
>>
> 
> 
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
> 
> 
>  
> 

-- 
View this message in context: http://www.nabble.com/When-does-merge%28%29-update-tf4194235.html#a11959806
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: When does merge() update

Posted by Craig L Russell <Cr...@Sun.COM>.
Sorry. I don't have anything like that. For this kind of issue I'd  
first go through the tutorials that the vendors have put a lot of  
work into.

Anyone else with an opinion on the best tutorials available?

Craig

On Jul 31, 2007, at 11:24 AM, Mauricio Salatino wrote:

> craig do you have any presentation (powerpoint or something) about
> good and best practice in jpa? (openjpa)
>
> On 7/31/07, Craig L Russell <Cr...@sun.com> wrote:
>> This technique is almost correct.
>>
>>> Object o=em.find(Object.class,pk);
>>> then change/ set new values (not the pk)
>>>
>>> =em.merge(o);
>>
>> You don't want to merge here. The find will put the instance into the
>> PersistenceContext.
>>
>> Just commit or flush changes to the database.
>>
>> Craig
>>
>> On Jul 31, 2007, at 10:29 AM, Mauricio Salatino wrote:
>>
>>> you shouldn't change the pk if you want to do an update..
>>> but if you are with a new object you must attach the object to the
>>> session.
>>> so.. the sequence be like this:
>>>
>>> if you know the pk of the object
>>>
>>> Object o=em.find(Object.class,pk);
>>> then change/ set new values (not the pk)
>>>
>>> o=em.merge(o);
>>>
>>>
>>>
>>> On 7/31/07, Tim Hanson <th...@bea.com> wrote:
>>>> I somewhat confused by the behaviour of merge(). It seems that if
>>>> I just
>>>> new up an entity and set its fields, including the primary key,
>>>> then I
>>>> call em.merge(), INSERT is called instead of UPDATE. Is this true?
>>>>
>>>> I am using JPA with JSF. I have a backing bean with a reference  
>>>> to an
>>>> entity. When I try to edit the entity, on save JSF fills in the
>>>> fields
>>>> of the entity from the request and then I call em.merge(), but  
>>>> like I
>>>> said, that always calls INSERT. The only way I was able to make  
>>>> this
>>>> work was by scoping the backing bean to the session, such that my
>>>> entity
>>>> was alive between calls. That seems like a strange thing to do.
>>>>
>>>> The other possibility would be to find the entity and then copy the
>>>> fields. That also seems like a hack.
>>>>
>>>> I guess if merge() is not really UPDATE, is there something else I
>>>> can
>>>> call?
>>>>
>>>> Thanks,
>>>> Tim
>>>>
>>>> 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.
>>>>
>>>
>>>
>>> --
>>>  - Salatino Mauricio -
>>
>> Craig Russell
>> Architect, Sun Java Enterprise System http://java.sun.com/products/ 
>> jdo
>> 408 276-5638 mailto:Craig.Russell@sun.com
>> P.S. A good JDO? O, Gasp!
>>
>>
>>
>
>
> -- 
>  - Salatino Mauricio -

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: When does merge() update

Posted by Mauricio Salatino <sa...@gmail.com>.
craig do you have any presentation (powerpoint or something) about
good and best practice in jpa? (openjpa)

On 7/31/07, Craig L Russell <Cr...@sun.com> wrote:
> This technique is almost correct.
>
> > Object o=em.find(Object.class,pk);
> > then change/ set new values (not the pk)
> >
> > =em.merge(o);
>
> You don't want to merge here. The find will put the instance into the
> PersistenceContext.
>
> Just commit or flush changes to the database.
>
> Craig
>
> On Jul 31, 2007, at 10:29 AM, Mauricio Salatino wrote:
>
> > you shouldn't change the pk if you want to do an update..
> > but if you are with a new object you must attach the object to the
> > session.
> > so.. the sequence be like this:
> >
> > if you know the pk of the object
> >
> > Object o=em.find(Object.class,pk);
> > then change/ set new values (not the pk)
> >
> > o=em.merge(o);
> >
> >
> >
> > On 7/31/07, Tim Hanson <th...@bea.com> wrote:
> >> I somewhat confused by the behaviour of merge(). It seems that if
> >> I just
> >> new up an entity and set its fields, including the primary key,
> >> then I
> >> call em.merge(), INSERT is called instead of UPDATE. Is this true?
> >>
> >> I am using JPA with JSF. I have a backing bean with a reference to an
> >> entity. When I try to edit the entity, on save JSF fills in the
> >> fields
> >> of the entity from the request and then I call em.merge(), but like I
> >> said, that always calls INSERT. The only way I was able to make this
> >> work was by scoping the backing bean to the session, such that my
> >> entity
> >> was alive between calls. That seems like a strange thing to do.
> >>
> >> The other possibility would be to find the entity and then copy the
> >> fields. That also seems like a hack.
> >>
> >> I guess if merge() is not really UPDATE, is there something else I
> >> can
> >> call?
> >>
> >> Thanks,
> >> Tim
> >>
> >> 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.
> >>
> >
> >
> > --
> >  - Salatino Mauricio -
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>
>
>


-- 
 - Salatino Mauricio -

Re: When does merge() update

Posted by Craig L Russell <Cr...@Sun.COM>.
This technique is almost correct.

> Object o=em.find(Object.class,pk);
> then change/ set new values (not the pk)
>
> o=em.merge(o);

You don't want to merge here. The find will put the instance into the  
PersistenceContext.

Just commit or flush changes to the database.

Craig

On Jul 31, 2007, at 10:29 AM, Mauricio Salatino wrote:

> you shouldn't change the pk if you want to do an update..
> but if you are with a new object you must attach the object to the  
> session.
> so.. the sequence be like this:
>
> if you know the pk of the object
>
> Object o=em.find(Object.class,pk);
> then change/ set new values (not the pk)
>
> o=em.merge(o);
>
>
>
> On 7/31/07, Tim Hanson <th...@bea.com> wrote:
>> I somewhat confused by the behaviour of merge(). It seems that if  
>> I just
>> new up an entity and set its fields, including the primary key,  
>> then I
>> call em.merge(), INSERT is called instead of UPDATE. Is this true?
>>
>> I am using JPA with JSF. I have a backing bean with a reference to an
>> entity. When I try to edit the entity, on save JSF fills in the  
>> fields
>> of the entity from the request and then I call em.merge(), but like I
>> said, that always calls INSERT. The only way I was able to make this
>> work was by scoping the backing bean to the session, such that my  
>> entity
>> was alive between calls. That seems like a strange thing to do.
>>
>> The other possibility would be to find the entity and then copy the
>> fields. That also seems like a hack.
>>
>> I guess if merge() is not really UPDATE, is there something else I  
>> can
>> call?
>>
>> Thanks,
>> Tim
>>
>> 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.
>>
>
>
> -- 
>  - Salatino Mauricio -

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: When does merge() update

Posted by Mauricio Salatino <sa...@gmail.com>.
you shouldn't change the pk if you want to do an update..
but if you are with a new object you must attach the object to the session.
so.. the sequence be like this:

if you know the pk of the object

Object o=em.find(Object.class,pk);
then change/ set new values (not the pk)

o=em.merge(o);



On 7/31/07, Tim Hanson <th...@bea.com> wrote:
> I somewhat confused by the behaviour of merge(). It seems that if I just
> new up an entity and set its fields, including the primary key, then I
> call em.merge(), INSERT is called instead of UPDATE. Is this true?
>
> I am using JPA with JSF. I have a backing bean with a reference to an
> entity. When I try to edit the entity, on save JSF fills in the fields
> of the entity from the request and then I call em.merge(), but like I
> said, that always calls INSERT. The only way I was able to make this
> work was by scoping the backing bean to the session, such that my entity
> was alive between calls. That seems like a strange thing to do.
>
> The other possibility would be to find the entity and then copy the
> fields. That also seems like a hack.
>
> I guess if merge() is not really UPDATE, is there something else I can
> call?
>
> Thanks,
> Tim
>
> 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.
>


-- 
 - Salatino Mauricio -