-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Why does replacing save() with saveOrUpdate() lead to error?
PostPosted: Wed Oct 08, 2003 4:37 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
From the descriptions in the doc and from eyeballing the code I thought saveOrUpdate() would always do at least the same as save(). In other words it would be a subset. Not true!

I had a bug where I was using saveOrUpdate() and I was getting an error:

"Another object was associated with this id (the object with the given id was already loaded): [de.nava.informa.impl.hibernate.Item#0]"

When I changed that back to just save() the problem goes away.

Can you give a concise description of the circumstances when using saveOrUpdate() insread of save() would give an error like this?

Thanks a 10^6

Pito


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 12:54 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Pito, post the mapping file for the object you are trying to save - the primary key is what is of interest.

Did you load this object in a previous session?

This is from section 7.5 in the reference document:
Quote:
The usage and semantics of saveOrUpdate() seems to be confusing for new users. Firstly, so long as you are
not trying to use instances from one session in another new session, you should not need to use update() or saveOrUpdate(). Some whole applications will never use either of these methods.

Usually update() or saveOrUpdate() are used in the following scenario:


Top
 Profile  
 
 Post subject: Here is more info
PostPosted: Thu Oct 09, 2003 11:16 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
Yes, I did read that section of the documentation (several times :-) and I guess I still am confused. If it helps, are the relevant mapping files.

Please let me know if you something suspicious.

Also please can you tell me in general if there are circumstances where saveOrUpdate() gives an error where save() doesn't?

Thanks

Pito

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="de.nava.informa.impl.hibernate.ChannelGroup"
        table="CHANNEL_GROUPS"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="intId"
            column="CHANNEL_GROUP_ID"
            type="integer"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="title"
            type="java.lang.String"
            update="true"
            insert="true"
            column="TITLE"
            not-null="true"
        />

        <bag
            name="channels"
            lazy="true"
            inverse="false"
            cascade="none"
            order-by="CHANNEL_GROUP"
        >

              <key
                  column="CHANNEL_GROUP"
              />

              <one-to-many
                  class="de.nava.informa.impl.hibernate.Channel"
              />
        </bag>

        <many-to-one
            name="parent"
            class="de.nava.informa.impl.hibernate.ChannelGroup"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="PARENT_ID"
        />

        <bag
            name="children"
            lazy="true"
            inverse="false"
            cascade="none"
            order-by="CHANNEL_GROUP_ID"
        >

              <key
                  column="PARENT_ID"
              />

              <one-to-many
                  class="de.nava.informa.impl.hibernate.ChannelGroup"
              />
        </bag>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-ChannelGroup.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="de.nava.informa.impl.hibernate.Item"
        table="ITEMS"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="intId"
            column="ITEM_ID"
            type="integer"
        >
            <generator class="native">
            </generator>
        </id>

        <many-to-one
            name="channel"
            class="de.nava.informa.impl.hibernate.Channel"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="CHANNEL_ID"
            not-null="true"
        />

        <property
            name="title"
            type="java.lang.String"
            update="true"
            insert="true"
            column="TITLE"
            not-null="true"
        />

        <property
            name="description"
            type="java.lang.String"
            update="true"
            insert="true"
            column="DESCRIPTION"
        />

        <property
            name="link"
            type="java.net.URL"
            update="true"
            insert="true"
            column="LINK"
        />

        <bag
            name="categories"
            table="CAT_ITEM_LINK"
            lazy="true"
            inverse="false"
            cascade="none"
        >

              <key
                  column="ITEM_ID"
              />

              <many-to-many
                  class="de.nava.informa.impl.hibernate.Category"
                  column="CATEGORY_ID"
                  outer-join="auto"
              />

        </bag>

        <property
            name="creator"
            type="java.lang.String"
            update="true"
            insert="true"
            column="CREATOR"
        />

        <property
            name="subject"
            type="java.lang.String"
            update="true"
            insert="true"
            column="SUBJECT"
        />

        <property
            name="date"
            type="java.util.Date"
            update="true"
            insert="true"
            column="DATE"
        />

        <property
            name="found"
            type="java.util.Date"
            update="true"
            insert="true"
            column="FOUND"
        />

        <many-to-one
            name="guid"
            class="de.nava.informa.impl.hibernate.ItemGuid"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="GUID"
        />

        <property
            name="comments"
            type="java.net.URL"
            update="true"
            insert="true"
            column="COMMENTS"
        />

        <many-to-one
            name="source"
            class="de.nava.informa.impl.hibernate.ItemSource"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="SOURCE"
        />

        <many-to-one
            name="enclosure"
            class="de.nava.informa.impl.hibernate.ItemEnclosure"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="ENCLOSURE"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Item.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>
[/code]


Top
 Profile  
 
 Post subject: Re: Why does replacing save() with saveOrUpdate() lead to er
PostPosted: Thu Oct 09, 2003 11:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
pitosalas wrote:
[b]Can you give a concise description of the circumstances when using saveOrUpdate() insread of save() would give an error like this?[/b

save() won't try to use the unsaved-value parameter. saveOrUpdate() does.
Explicitly set the unsaved-value parameter.
Read the 4.1.4 section of Hibernate reference guide for more tips on it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.