-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem with composite-id
PostPosted: Tue May 13, 2008 6:13 am 
Newbie

Joined: Tue May 13, 2008 6:05 am
Posts: 2
Hello together,

i'm facing a strange problem with a class containing a composite key.
Here is my code:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="PBC.Lock,PBC" table="locks" lazy="false">
    <composite-id access="property">
      <key-property column="object_id" name="Id"/>
      <key-property column="object_type" name="ObjectType"/>
    </composite-id>
    <property column="object_id" name="Id" type="Int64" not-null="true" />
    <property column="object_type" type="String" name="ObjectType" not-null="true"/>
    <property column="client_id"  name="ClientId" not-null="true"/>
  </class>
</hibernate-mapping>


Code:
[Serializable]
    public class Lock
    {

        private long _id;

        private string _objectType;

        private long _clientId;


        public Lock(long id, Type objType, long clientId)
        {
            _id = id;
            _objectType = objType.Name;
            _clientId = clientId;
           
        }

        public Lock()
        {
        }

        #region Properties
        public long Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string ObjectType
        {
            get { return _objectType; }
            set { _objectType = value; }
        }

        public long ClientId
        {
            get { return _clientId; }
            set { _clientId = value; }
        }
        #endregion

        #region Equals And HashCode Overrides

        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            if ((obj == null) || (obj.GetType() != this.GetType())) return false;
            Lock castObj = (Lock)obj;
            return (castObj != null) &&
                (this._id == castObj.Id
                && this._objectType == castObj._objectType
                );
        }

        public override int GetHashCode()
        {

            int hash = 57;
            hash = 27 * hash * (_id.GetHashCode() + _objectType.GetHashCode());
            return hash;
        }
        #endregion

    }


When i try to save a Lock Object i get the following Error:
2008-05-13 11:41:09,987 INFO / Class: NHibernate.Loader.Loader L: 0 / GetResultSet (..) -> SELECT lock0_.object_id as object1_12_0_, lock0_.object_type as object2_12_0_, lock0_.client_id as client3_12_0_ FROM locks lock0_ WHERE lock0_.object_id=:p0 and lock0_.object_type=:p1 / File:
2008-05-13 11:41:11,915 WARN / Class: NHibernate.Util.ADOExceptionReporter L: 0 / LogExceptions (..) -> Npgsql.NpgsqlException:
column "object_id" specified more than once
Severity: ERROR
Code: 42701
at Npgsql.NpgsqlConnector.CheckErrors()
at Npgsql.NpgsqlConnector.CheckErrorsAndNotifications()
at Npgsql.NpgsqlCommand.ExecuteCommand()
at Npgsql.NpgsqlCommand.ExecuteNonQuery()
at NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) / File:
2008-05-13 11:41:11,919 ERROR / Class: NHibernate.Util.ADOExceptionReporter L: 0 / LogExceptions (..) -> ERROR: 42701: column "object_id" specified more than once / File:
2008-05-13 11:41:11,923 ERROR / Class: NHibernate.Impl.SessionImpl L: 0 / Execute (..) -> could not synchronize database state with session / File:
NHibernate.ADOException: could not insert: [PBC.Lock#PBC.Lock][SQL: INSERT INTO locks (object_id, object_type, client_id, object_id, object_type) VALUES (?, ?, ?, ?, ?)] ---> Npgsql.NpgsqlException:
column "object_id" specified more than once
Severity: ERROR
Code: 42701
at Npgsql.NpgsqlConnector.CheckErrors()
at Npgsql.NpgsqlConnector.CheckErrorsAndNotifications()
at Npgsql.NpgsqlCommand.ExecuteCommand()
at Npgsql.NpgsqlCommand.ExecuteNonQuery()
at NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
--- End of inner exception stack trace ---
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()

Does anyone know what's responsible for this message?

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 6:47 am 
Newbie

Joined: Tue May 13, 2008 6:05 am
Posts: 2
I think i figured it out myself.
I thought i'd have to specify the properties outside the composite-id element
an inside just tell which properties to use.
But it is enough just to define them inside the composite-id element.
Here is my enhanced mapping-file.

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="PBC.Lock,PBC" table="locks" lazy="false">
    <composite-id access="property">
      <key-property column="object_id" name="Id" type="Int64" />
      <key-property column="object_type" name="ObjectType" type="String"/>
    </composite-id>
    <property column="client_id"  name="ClientId" not-null="true"/>
  </class>
</hibernate-mapping>


Now everything works fine.

Regards.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.