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: 'the same identifier value was already associated' - hö?
PostPosted: Mon Jan 08, 2007 7:30 am 
Newbie

Joined: Wed Nov 01, 2006 11:19 am
Posts: 3
Location: Switzerland TG
Hibernate version: NHibernate

Mapping documents:

TimeSpan:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class
    name="Cyberline.WorkLogger.Model.TimeSpan, Cyberline.WorkLogger.Model"
    table="TimeSpans"
    optimistic-lock="version">

    <id name="Id" column="Id">
      <generator class="increment" />
    </id>

    <property name="Starttime" type="DateTime"/>

    <property name="Endtime" type="DateTime" />

    <many-to-one foreign-key="int" name="Action" not-null="true"></many-to-one>
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
        public void EditTimeSpan(Cyberline.WorkLogger.Model.TimeSpan oldTs, DateTime start, DateTime end, Action act)
        {
            ISession session = PersistenceLogic.Instance.GetNewSession();
            try
            {
                if (session.CreateCriteria(typeof(Cyberline.WorkLogger.Model.TimeSpan))
                .Add(Expression.Eq("Id", oldTs.Id.ToString()))
                .List()
                .Count < 0)
                {
                    throw new ArgumentException(String.Format("Cannot find the timespan with the Id '{0}'.", oldTs.Id.ToString(), "Id"));
                }
                else
                {
                    oldTs.Endtime = end;
                    oldTs.Starttime = start;
                    oldTs.Action = act;
                    session.Update(oldTs);
                    session.Flush();
                }
            }
            catch
            {
            }
            finally
            {
                session.Close();
            }
        }


Full stack trace of any exception that occurs:

a different object with the same identifier value was already associated with the session: 8, of class: Cyberline.WorkLogger.Model.TimeSpan

bei NHibernate.Impl.SessionImpl.CheckUniqueness(Key key, Object obj)
bei NHibernate.Impl.SessionImpl.DoUpdateMutable(Object obj, Object id, IClassPersister persister)
bei NHibernate.Impl.SessionImpl.DoUpdate(Object obj, Object id, IClassPersister persister)
bei NHibernate.Impl.SessionImpl.Update(Object obj)
bei Cyberline.WorkLogger.Logic.WorkLoggerLogic.EditTimeSpan(TimeSpan oldTs, DateTime start, DateTime end, Action act) in C:\Development\WorkLogger\WorkLogger\Logic\WorkLoggerLogic.cs:Zeile 1321.

Name and version of the database you are using: SQL Server 2005
Database name: WorkLogger
Table: TimeSpans


Problembeschreibung:

Eigentlich will ich nur einen schon bestehenden TimeSpan (oldTs) in der Tabelle Timespans updaten.
Wenn ich beim debuggen jedoch beim Befehl 'session.Update(oldTs)' ankomme, meckert es mir immer:
'a different object with the same identifier value was already associated with the session'
Aber ich bin mir sicher dass ich in der Tabelle keine redundanten Datensätze habe und auch in während dieser Session nicht zwei aufmache. Oder etwa doch?
Kann mir jemand sagen worauf ich achten muss um den Fehler etwas einkreisen zu können?

Danke und Gruss
Whisky


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 8:22 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Hi,

in Deiner Funktion übergibst Du Cyberline.WorkLogger.Model.TimeSpan oldTs, diese Objekt hast Du sicherlich vor geladen und die Session noch nicht geschlossen, wenn Du die Methode EditTimeSpan aufrufst. Entweder fügst Du der Methode als Parameter noch die Session hinzu und machst dann kein PersistenceLogic.Instance.GetNewSession(); oder Du schliesst die Session vor dem EditTimeSpan Aufruf.

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 8:41 am 
Newbie

Joined: Wed Nov 01, 2006 11:19 am
Posts: 3
Location: Switzerland TG
Hi Kaneda

Vielen Dank für die schnelle Antwort.
Ich weiss nicht ob du das so gemeint hast wie ich es nun gemacht habe, aber ich habe mal die erste Abfrage also die If-Bedingung auskommentiert, weil da frage ich ja die TimeSpan ab. Wenn ich also nur den TimeSpan verändere und dann session.Uptdate durchfüre funktioniert es.

Aber wie hast du das genau gemeint mit Parameter übergeben?
Dass ich in einer erste Methode abfrage ob es den TimeSpan überhaupt gibt (was zwar eigentlich allgemein unsinnig ist zu prüfen wie ich meine), dann diese session schliesse, eine neue aufmache, die neue session als Parameter an eine zweite Methode und dort dann das update durchführen?

Gruss
Whisky


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 1:33 pm 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Sorry war wohl etwas schnell über den Code geflogen, daher meine Annahme, daß Du den TimeSpan nochmals lädst. Wenn es jetzt geht, dann würde ich nichts am Design ändern. ;)

_________________
regards

Olaf

vote if it helped


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.