-->
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: Version field not updated when using SaveOrUpdateCopy
PostPosted: Fri Apr 28, 2006 6:07 am 
Newbie

Joined: Fri Apr 28, 2006 5:57 am
Posts: 2
The version field is not updated When using SaveOrUpdateCopy

With SaveOrUpdate we see this expected behaviour:
// obj.version is 3.
session.SaveOrUpdate(obj);
session.Flush();
// obj.version is 4.

However, when using SaveOrUpdateCopy we see this behaviour:
// obj.version is 3.
session.SaveOrUpdateCopy(obj);
session.Flush();
// obj.version is still 3, but 4 in database.

Is this expected?

Hibernate version: 1.0.2
Name and version of the database you are using:Sql Server 2005


Top
 Profile  
 
 Post subject: Re: Version field not updated when using SaveOrUpdateCopy
PostPosted: Fri Apr 28, 2006 6:56 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mg wrote:
The version field is not updated When using SaveOrUpdateCopy

With SaveOrUpdate we see this expected behaviour:
// obj.version is 3.
session.SaveOrUpdate(obj);
session.Flush();
// obj.version is 4.

However, when using SaveOrUpdateCopy we see this behaviour:
// obj.version is 3.
session.SaveOrUpdateCopy(obj);
session.Flush();
// obj.version is still 3, but 4 in database.

Is this expected?


From ISession.cs file, documentation comment for SaveOrUpdateCopy
Quote:
/// <summary>
/// Copy the state of the given object onto the persistent object with the same
/// identifier. If there is no persistent instance currently associated with
/// the session, it will be loaded. Return the persistent instance. If the
/// given instance is unsaved or does not exist in the database, save it and
/// return it as a newly persistent instance. Otherwise, the given instance
/// does not become associated with the session.
/// </summary>

So, the answer depends or whether there was an object with given Id in database or not.

Gert


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 7:42 am 
Newbie

Joined: Fri Apr 28, 2006 5:57 am
Posts: 2
Thanks for the reply. There already is an instance in the database. The code below should illustrate the problem. Using SaveOrUpdateCopy the test fails for the code in session 2. If it is modified to use SaveOrUpdate it passes.

What I found from this test is that the return value of SaveOrUpdateCopy has updated version fields, while the input still has the old version. However, when the initial version of the object is saved (in session 1) the database id of the input is updated, which leads me to believe that the behaviour is unexpected.

Any thoughts?

Regards
Martin


Code:
        [TestMethod]
        public void UsingSaveOrUpdateCopy()
        {
            // Session 1:
            BidirectionalParent parent = new BidirectionalParent(0, "Paul");
            BidirectionalChild child = parent.AddChild("Simone");

            using (ISession session = this.factory.OpenSession())
            {
                session.SaveOrUpdateCopy(parent);
                session.Flush();
            }

            Assert.AreNotEqual(0, parent.Id, "Expected parent id to be set");
            Assert.AreEqual(0, parent.Version, "Expected parent version to be 0");
            Assert.AreNotEqual(0, child.Id, "Expected child id to be set");
            Assert.AreEqual(0, child.Version, "Expected child version to be 0");

            Int32 parentID = parent.Id;
            Int32 childID = child.Id;

            // Session 2:
            parent.Name = "Frank";
            child.Name = "Maria";

            BidirectionalParent result;
            using (ISession session = this.factory.OpenSession())
            {
                result = (BidirectionalParent)session.SaveOrUpdateCopy(parent);
                session.Flush();
            }

            Assert.AreEqual(parentID, result.Id, "Expected parent id to be unchanged after edit (return value)");
            Assert.AreEqual(1, result.Version, "Expected parent version to be 1 after edit (return value)");
            Assert.AreEqual(childID, result.GetChild(0).Id, "Expected child id to be unchanged after edit (return value)");
            Assert.AreEqual(1, result.GetChild(0).Version, "Expected child version to be 1 after edit (return value)");

            Assert.AreEqual(parentID, parent.Id, "Expected parent id to be unchanged after edit (input value)");

            // This assertion fails:
            Assert.AreEqual(1, parent.Version, "Expected parent version to be 1 after edit (input value)");
            Assert.AreEqual(childID, child.Id, "Expected child id to be unchanged after edit (input value)");
            Assert.AreEqual(1, child.Version, "Expected child version to be 1 after edit (input value)");
        }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 9:45 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mg wrote:
Thanks for the reply. There already is an instance in the database. The code below should illustrate the problem. Using SaveOrUpdateCopy the test fails for the code in session 2. If it is modified to use SaveOrUpdate it passes.

What I found from this test is that the return value of SaveOrUpdateCopy has updated version fields, while the input still has the old version. However, when the initial version of the object is saved (in session 1) the database id of the input is updated, which leads me to believe that the behaviour is unexpected.


AFAIK You description is exactly what the documentation comment in my previous post says. So, it works as designed.

Gert


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.