-->
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.  [ 7 posts ] 
Author Message
 Post subject: column X appears more than once - discriminators
PostPosted: Sun Mar 15, 2009 6:35 am 
Beginner
Beginner

Joined: Fri Jun 01, 2007 4:55 pm
Posts: 24
Hibernate version:
1.2.0.4000

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="GMBPDefinition.Classes.MatchStrings.BaseMatchString, GMBPDefinition" table="MatchingStrings">
      <id name="Id" column="ID" unsaved-value="0" type="Int32">
         <generator class="identity" />
      </id>
      <discriminator column="Type"/>
      
      <property name="MatchString"></property>
      <property name="isRegEx" column="isRegEx"></property>
      <property name="Type" column="Type"></property>

      <subclass name="GMBPDefinition.Classes.MatchStrings.TruncationString, GMBPDefinition"
               discriminator-value="1">
      </subclass>
      <subclass name="GMBPDefinition.Classes.MatchStrings.OutOfOfficeString, GMBPDefinition"
               discriminator-value="2">
      </subclass>
      <subclass name="GMBPDefinition.Classes.MatchStrings.BlackListString, GMBPDefinition"
               discriminator-value="3">
      </subclass>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
Public Sub Save(ByVal MatchString As GMBPDefinition.Interfaces.IMatchString)
        Try
            MyBase.BeginTransaction()
            Session.Save(MatchString)
            MyBase.CommitTransaction()
        Catch ex As Exception
            MyBase.RollBackTransaction()
            Throw ex
        End Try
    End Sub



I am not sure why the Type column is being included twice. Any help would be greatly appreciated!

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 15, 2009 6:41 am 
Beginner
Beginner

Joined: Fri Jun 01, 2007 4:55 pm
Posts: 24
Sorry, meant to paste the error in too!

Exception
"could not insert: [GMBPDefinition.Classes.MatchStrings.TruncationString][SQL: INSERT INTO MatchingStrings (MatchString, isRegEx, Type, Type) VALUES (?, ?, ?, '1')]"

inner exception:
"Column name 'Type' appears more than once in the result column list."

stack trace

" at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session)"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 3:36 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Normally the descriminator isn't used in the code. It's just fpr hibernate to distinguish the classes. If you absolutely need to map the column, try

<property name="Type" column="Type" update="false" insert="false"/>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 4:03 am 
Beginner
Beginner

Joined: Fri Jun 01, 2007 4:55 pm
Posts: 24
That seemed to work well.

The way I load the class is a bit of a hack, while it is working, you might know a more elegant solution.

The trouble is, I do not know the type of the class when I load. It is just Load MatchString number 5 (for example) and this could be one of three classes.

My hack is to create a criterian object and list those with Id of 5. While this works, using the Load() method must be the correct way of doing it and getting back the correct concrete class.

Many thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 4:25 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
My hack is to create a criterian object and list those with Id of 5. While this works, using the Load() method must be the correct way of doing it and getting back the correct concrete class.


I'm not sure what you mean. Can you post the criteria and the code for loading the objects ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 6:05 am 
Beginner
Beginner

Joined: Fri Jun 01, 2007 4:55 pm
Posts: 24
Sure, here is my load method:

Code:
Dim Criteria As NHibernate.ICriteria
        Criteria = MyBase.Session.CreateCriteria(GetType(GMBPDefinition.Classes.MatchStrings.BaseMatchString))
        Criteria.Add(NHibernate.Expression.Expression.Eq("Id", Id))
        Dim list As IList(Of GMBPDefinition.Interfaces.IMatchString) = Criteria.List(Of GMBPDefinition.Interfaces.IMatchString)()

        If list.Count > 0 Then
            Return list(0)
        Else
            Return Nothing
        End If


Now you can see it is not useing the Session.Load() method.

I don't know which version of the class it will be (because that is controlled by the disciminator) so I can't pass in an object to load method.

I have tried variations, such as :

Code:
'Dim result As GMBPDefinition.Interfaces.IMatchString
        ''result = New GMBPDefinition.Classes.MatchStrings.BaseMatchString()

        'result = MyBase.Session.Load(Of GMBPDefinition.Classes.MatchStrings.BaseMatchString)(Id)
        ''MyBase.Session.Load(result, Id)
        'Return result


It did not like the passing of a Base Match String, and when it did load, the object could not be saved again as it was some NHibernate object that had not been initialised.

It is working, but there must be a better way to do it.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 7:59 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You don't have to pass a object:

BaseMatchString str =session.Load<BaseMatchString>(id);

_________________
--Wolfgang


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