You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Gerlex <ge...@de.ibm.com> on 2009/02/19 16:21:54 UTC

Problem with merge

Hello together,

I have one big problem!!!

I want to update an Persistent-Entity with the merge-method and it doesn't
update the attributes, 
which I have set to null.

For example:

1. I load one Object from the database 
2. I set one attribute for example the date or a String to null
3. I merge this object 
4. The database doesn't update the date

When I load this object again, there is the same date than before.

the attributes are nullable 


The Environment:

Websphere 6
Database DB2
 




 
-- 
View this message in context: http://n2.nabble.com/Problem-with-merge-tp2353521p2353521.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Re: Problem with merge

Posted by Gerlex <ge...@de.ibm.com>.
WAS 6.1.0.19
OpenJPA 1.03


Gerlex wrote:
> 
> Hello together,
> 
> I have one big problem!!!
> 
> I want to update an Persistent-Entity with the merge-method and it doesn't
> update the attributes, 
> which I have set to null.
> 
> For example:
> 
> 1. I load one Object from the database 
> 2. I set one attribute for example the date or a String to null
> 3. I merge this object 
> 4. The database doesn't update the date
> 
> When I load this object again, there is the same date than before.
> 
> the attributes are nullable 
> 
> 
> The Environment:
> 
> Websphere 6
> Database DB2
>  
> 
> 
> 
> 
>  
> 

-- 
View this message in context: http://n2.nabble.com/Problem-with-merge-tp2353521p2354124.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Re: Problem with merge

Posted by Gerlex <ge...@de.ibm.com>.
Thank you,

It works!!!

gerlex


Michael Dick wrote:
> 
> Hi Gerlex,
> 
> This has to do with the way OpenJPA determines whether a field was set to
> null or whether it was never loaded from the database. The default
> configuration assumes that a null value was never loaded, unless there's a
> DetachedStateManager (OpenJPA object that tracks the state of your entity)
> present in the entity that you're merging.
> 
> There are a couple of ways to resolve this issue. One is to ensure that a
> detached state manager is present in the entity when you merge it. This
> should be the case unless you've serialized the entity, or if you're
> trying
> to merge in a new copy of the entity class.
> 
> Another option is to change the assumptions that OpenJPA makes. This can
> be
> done by setting the openjpa.DetachState configuration option to
> "fetch-groups" or "all". These settings may have other effects on the
> application (ie setting it to all means that when the object is detached -
> em.clear() you'll load all the fields from the database, fetch-groups
> loads
> the current fetch-group).
> 
> I can't say which option is best for you without understanding your
> application, you might want to consult the OpenJPA manual to see what it
> has
> to say. Good places to start :
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_detach_graph
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#detached-state-field
> 
> Hope this helps
> 
> -mike
> 
> 
> 
> On Thu, Feb 19, 2009 at 10:35 AM, Gerlex <ge...@de.ibm.com> wrote:
> 
>>
>> I load the objects with my own adopted query.
>>
>>
>> public List<Auftrag> getAuftragsliste(Auftragsfilter auftragsfilter)
>> throws
>> HermesRuntimeException{
>>                List<Auftrag> listeauftraege=new LinkedList<Auftrag>();
>>
>>              // ..... build my query
>>
>>
>>
>> try{
>>                        Query query =em.createQuery(abfrage.toString());
>>                // fill my placeholder
>>                        for (Map.Entry<String, Object> s :
>> filtermap.entrySet()) {
>>                                query.setParameter(s.getKey(),
>> s.getValue());
>>                                System.out.println(s.getKey() + " " +
>> s.getValue());
>>                        }
>>                        for(Map.Entry<String, Date> s :
>> filterdatum.entrySet() ){
>>                                query.setParameter(s.getKey(),
>> s.getValue(),
>> TemporalType.DATE);
>>                        }
>>
>>                        List resultList = query.getResultList();
>>                        listeauftraege.addAll(resultList);
>>                }
>>                catch(IllegalArgumentException e){
>>                        e.printStackTrace();
>>                        throw new
>> HermesRuntimeException("AuftragsverwaltungBean/getAuftragslliste:
>> Auragsliste konnte nicht geladen werden, da Query ungültig");
>>                }
>>
>>
>>                return listeauftraege;
>>                   }
>>
>>
>>
>> Yes, WAS61 with FixPack
>>
>>
>> this is the code for my update:
>> public void updateAuftrag(Auftrag auftrag) throws HermesRuntimeException
>> {
>>                // begin-user-code
>>
>>                try {
>>                        Auftrag a=em.merge(auftrag);
>>                        em.flush();
>>                } catch (Exception e) {
>>                        e.printStackTrace();
>>                        throw new HermesRuntimeException(
>>                         "AuftragsverwaltungBean/updateAuftrag: Auftrag
>> konnte nicht aktualisiert werden");
>>                }
>>                // end-user-code
>>        }
>>
>> the updates, where I change the value to an other value are
>> successfull...,except the changes to null
>>
>>
>> Rick Curtis wrote:
>> >
>> > How did you load an Object from the database? You are running WAS6, is
>> > that WAS61 with the FP? A code snippet may help.
>> >
>> > If you use OpenJPA APIs to load data from your database, you shouldn't
>> > need to call merge on that object. Is it possible that your transaction
>> > isn't being committed and that is why your changes aren't being
>> persisted?
>> >
>> > -Rick
>> >
>> >
>> >
>> > Gerlex wrote:
>> >>
>> >> Hello together,
>> >>
>> >> I have one big problem!!!
>> >>
>> >> I want to update an Persistent-Entity with the merge-method and it
>> >> doesn't update the attributes,
>> >> which I have set to null.
>> >>
>> >> For example:
>> >>
>> >> 1. I load one Object from the database
>> >> 2. I set one attribute for example the date or a String to null
>> >> 3. I merge this object
>> >> 4. The database doesn't update the date
>> >>
>> >> When I load this object again, there is the same date than before.
>> >>
>> >> the attributes are nullable
>> >>
>> >>
>> >> The Environment:
>> >>
>> >> Websphere 6
>> >> Database DB2
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://n2.nabble.com/Problem-with-merge-tp2353521p2353921.html
>> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
>>
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/Problem-with-merge-tp2353521p2357696.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Re: Problem with merge

Posted by Michael Dick <mi...@gmail.com>.
Hi Gerlex,

This has to do with the way OpenJPA determines whether a field was set to
null or whether it was never loaded from the database. The default
configuration assumes that a null value was never loaded, unless there's a
DetachedStateManager (OpenJPA object that tracks the state of your entity)
present in the entity that you're merging.

There are a couple of ways to resolve this issue. One is to ensure that a
detached state manager is present in the entity when you merge it. This
should be the case unless you've serialized the entity, or if you're trying
to merge in a new copy of the entity class.

Another option is to change the assumptions that OpenJPA makes. This can be
done by setting the openjpa.DetachState configuration option to
"fetch-groups" or "all". These settings may have other effects on the
application (ie setting it to all means that when the object is detached -
em.clear() you'll load all the fields from the database, fetch-groups loads
the current fetch-group).

I can't say which option is best for you without understanding your
application, you might want to consult the OpenJPA manual to see what it has
to say. Good places to start :
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_detach_graph
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#detached-state-field

Hope this helps

-mike



On Thu, Feb 19, 2009 at 10:35 AM, Gerlex <ge...@de.ibm.com> wrote:

>
> I load the objects with my own adopted query.
>
>
> public List<Auftrag> getAuftragsliste(Auftragsfilter auftragsfilter) throws
> HermesRuntimeException{
>                List<Auftrag> listeauftraege=new LinkedList<Auftrag>();
>
>              // ..... build my query
>
>
>
> try{
>                        Query query =em.createQuery(abfrage.toString());
>                // fill my placeholder
>                        for (Map.Entry<String, Object> s :
> filtermap.entrySet()) {
>                                query.setParameter(s.getKey(),
> s.getValue());
>                                System.out.println(s.getKey() + " " +
> s.getValue());
>                        }
>                        for(Map.Entry<String, Date> s :
> filterdatum.entrySet() ){
>                                query.setParameter(s.getKey(), s.getValue(),
> TemporalType.DATE);
>                        }
>
>                        List resultList = query.getResultList();
>                        listeauftraege.addAll(resultList);
>                }
>                catch(IllegalArgumentException e){
>                        e.printStackTrace();
>                        throw new
> HermesRuntimeException("AuftragsverwaltungBean/getAuftragslliste:
> Auragsliste konnte nicht geladen werden, da Query ungültig");
>                }
>
>
>                return listeauftraege;
>                   }
>
>
>
> Yes, WAS61 with FixPack
>
>
> this is the code for my update:
> public void updateAuftrag(Auftrag auftrag) throws HermesRuntimeException {
>                // begin-user-code
>
>                try {
>                        Auftrag a=em.merge(auftrag);
>                        em.flush();
>                } catch (Exception e) {
>                        e.printStackTrace();
>                        throw new HermesRuntimeException(
>                         "AuftragsverwaltungBean/updateAuftrag: Auftrag
> konnte nicht aktualisiert werden");
>                }
>                // end-user-code
>        }
>
> the updates, where I change the value to an other value are
> successfull...,except the changes to null
>
>
> Rick Curtis wrote:
> >
> > How did you load an Object from the database? You are running WAS6, is
> > that WAS61 with the FP? A code snippet may help.
> >
> > If you use OpenJPA APIs to load data from your database, you shouldn't
> > need to call merge on that object. Is it possible that your transaction
> > isn't being committed and that is why your changes aren't being
> persisted?
> >
> > -Rick
> >
> >
> >
> > Gerlex wrote:
> >>
> >> Hello together,
> >>
> >> I have one big problem!!!
> >>
> >> I want to update an Persistent-Entity with the merge-method and it
> >> doesn't update the attributes,
> >> which I have set to null.
> >>
> >> For example:
> >>
> >> 1. I load one Object from the database
> >> 2. I set one attribute for example the date or a String to null
> >> 3. I merge this object
> >> 4. The database doesn't update the date
> >>
> >> When I load this object again, there is the same date than before.
> >>
> >> the attributes are nullable
> >>
> >>
> >> The Environment:
> >>
> >> Websphere 6
> >> Database DB2
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/Problem-with-merge-tp2353521p2353921.html
> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
>
>

Re: Problem with merge

Posted by Gerlex <ge...@de.ibm.com>.
I load the objects with my own adopted query.


public List<Auftrag> getAuftragsliste(Auftragsfilter auftragsfilter) throws
HermesRuntimeException{
                List<Auftrag> listeauftraege=new LinkedList<Auftrag>();
                
              // ..... build my query 
                


try{
                        Query query =em.createQuery(abfrage.toString());
                // fill my placeholder
                        for (Map.Entry<String, Object> s :
filtermap.entrySet()) {
                                query.setParameter(s.getKey(),
s.getValue());
                                System.out.println(s.getKey() + " " +
s.getValue());
                        }
                        for(Map.Entry<String, Date> s :
filterdatum.entrySet() ){
                                query.setParameter(s.getKey(), s.getValue(),
TemporalType.DATE);
                        }
                
                        List resultList = query.getResultList();
                        listeauftraege.addAll(resultList);
                }
                catch(IllegalArgumentException e){
                        e.printStackTrace();
                        throw new
HermesRuntimeException("AuftragsverwaltungBean/getAuftragslliste:
Auragsliste konnte nicht geladen werden, da Query ungültig");
                }
           

                return listeauftraege;
                   }



Yes, WAS61 with FixPack


this is the code for my update:
public void updateAuftrag(Auftrag auftrag) throws HermesRuntimeException {
                // begin-user-code
                
                try {
                        Auftrag a=em.merge(auftrag);
                        em.flush();
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new HermesRuntimeException(
                         "AuftragsverwaltungBean/updateAuftrag: Auftrag
konnte nicht aktualisiert werden");
                }
                // end-user-code
        }

the updates, where I change the value to an other value are
successfull...,except the changes to null


Rick Curtis wrote:
> 
> How did you load an Object from the database? You are running WAS6, is
> that WAS61 with the FP? A code snippet may help.
> 
> If you use OpenJPA APIs to load data from your database, you shouldn't
> need to call merge on that object. Is it possible that your transaction
> isn't being committed and that is why your changes aren't being persisted?
> 
> -Rick
> 
> 
> 
> Gerlex wrote:
>> 
>> Hello together,
>> 
>> I have one big problem!!!
>> 
>> I want to update an Persistent-Entity with the merge-method and it
>> doesn't update the attributes, 
>> which I have set to null.
>> 
>> For example:
>> 
>> 1. I load one Object from the database 
>> 2. I set one attribute for example the date or a String to null
>> 3. I merge this object 
>> 4. The database doesn't update the date
>> 
>> When I load this object again, there is the same date than before.
>> 
>> the attributes are nullable 
>> 
>> 
>> The Environment:
>> 
>> Websphere 6
>> Database DB2
>>  
>> 
>> 
>> 
>> 
>>  
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/Problem-with-merge-tp2353521p2353921.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Re: Problem with merge

Posted by Rick Curtis <cu...@gmail.com>.
How did you load an Object from the database? You are running WAS6, is that
WAS61 with the FP? A code snippet may help.

If you use OpenJPA APIs to load data from your database, you shouldn't need
to call merge on that object. Is it possible that your transaction isn't
being committed and that is why your changes aren't being persisted?

-Rick



Gerlex wrote:
> 
> Hello together,
> 
> I have one big problem!!!
> 
> I want to update an Persistent-Entity with the merge-method and it doesn't
> update the attributes, 
> which I have set to null.
> 
> For example:
> 
> 1. I load one Object from the database 
> 2. I set one attribute for example the date or a String to null
> 3. I merge this object 
> 4. The database doesn't update the date
> 
> When I load this object again, there is the same date than before.
> 
> the attributes are nullable 
> 
> 
> The Environment:
> 
> Websphere 6
> Database DB2
>  
> 
> 
> 
> 
>  
> 

-- 
View this message in context: http://n2.nabble.com/Problem-with-merge-tp2353521p2353791.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.