Good Morning All,
I have problem with mapping some tables. I have mapped a 100+ table legacy database using Nhibernate. I have encountered some problems around three table; job_operators, job_operator_groups, job_operator_group_lk.
A lookup table is used to provide the name of the job_operator_groups and is also used by various other tables (omitted for simplicity).
A job_operators can be a member of one or more job_operator_groups.
A job_operator_groups has exactly one job_operator_group_lk.
A job_operator_groups can have one or more job_operators.
I have included the exception from my unit test, the schema for the troublesome three tables and their associated mappings.
Any help in solving this problem would be gratefully received.
Kind regards,
Paul.
Hibernate version:
v2.0.50727
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Digitalk.Gateway.Core.DomainObjects.JobOperatorGroups,Digitalk.Gateway.Core" table="job_operator_groups" lazy="true">
<id name="ID" column="operator_id" type="int">
<generator class="native" />
</id>
<bag name="JobOperators" inverse="false" table="job_operators" lazy="false" cascade="delete">
<key column="id" />
<many-to-many class="Digitalk.Gateway.Core.DomainObjects.JobOperators,Digitalk.Gateway.Core">
<column name="operator_id" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Digitalk.Gateway.Core.DomainObjects.JobOperatorGroupLk,Digitalk.Gateway.Core" table="job_operator_group_lk" lazy="false">
<id name="ID" column="id" type="int">
<generator class="native" />
</id>
<property name="Name" column="name" type="string" />
<bag name="JobDefAlertNotifier" inverse="true" lazy="false" cascade="delete">
<key column="operator_group_lk_id" />
<one-to-many class="Digitalk.Gateway.Core.DomainObjects.JobDefAlertNotifier,Digitalk.Gateway.Core" />
</bag>
<bag name="JobDefAsrNotifier" inverse="true" lazy="false" cascade="delete">
<key column="operator_group_lk_id" />
<one-to-many class="Digitalk.Gateway.Core.DomainObjects.JobDefAsrNotifier,Digitalk.Gateway.Core" />
</bag>
<bag name="JobOperators" inverse="false" table="job_operator_groups" lazy="false" cascade="delete">
<key column="group_lk" />
<many-to-many class="Digitalk.Gateway.Core.DomainObjects.JobOperators,Digitalk.Gateway.Core">
<column name="id" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Digitalk.Gateway.Core.DomainObjects.JobOperators,Digitalk.Gateway.Core" table="job_operators" lazy="false">
<id name="ID" column="id" type="int">
<generator class="native" />
</id>
<property name="Destination" column="destination" type="string" />
<property name="Name" column="name" type="string" />
<property name="Type" column="type" type="int" />
<bag name="JobOperatorGroups" outer-join="true" table="job_operator_groups">
<key column="id"/>
<many-to-many column="operator_id" class="Digitalk.Gateway.Core.DomainObjects.JobOperatorGroups,Digitalk.Gateway.Core"/>
</bag>
</class>
</hibernate-mapping>
--Create job_operator_groups table
CREATE TABLE [job_operator_groups] (
[group_lk] [int] NOT NULL ,
[operator_id] [int] NOT NULL ,
CONSTRAINT [pk_job_operator_groups] PRIMARY KEY NONCLUSTERED
(
[operator_id],
[group_lk]
) WITH FILLFACTOR = 90 ON [PRIMARY] ,
CONSTRAINT [fk_job_operator_groups_job_operator_group_lk] FOREIGN KEY
(
[group_lk]
) REFERENCES [job_operator_group_lk] (
[id]
),
CONSTRAINT [fk_job_operator_groups_job_operators] FOREIGN KEY
(
[operator_id]
) REFERENCES [job_operators] (
[id]
)
) ON [PRIMARY]
GO
--Create job_operator_group_lk table
CREATE TABLE [job_operator_group_lk] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (30) COLLATE Latin1_General_CI_AS NULL ,
CONSTRAINT [pk_job_operator_group_lk] PRIMARY KEY CLUSTERED
(
[id]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO
--Create job_operators table
CREATE TABLE [job_operators] (
[destination] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (30) COLLATE Latin1_General_CI_AS NULL ,
[type] [int] NULL ,
CONSTRAINT [pk_job_operators] PRIMARY KEY CLUSTERED
(
[id]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO
Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Code:
AlertJobOperatorsTest.CanGetOperatorsList : Failed
NHibernate: SELECT top 10000 this_.id as id8_1_, this_.destination as destinat2_8_1_, this_.name as name8_1_, this_.type as type8_1_, joboperato2_.id as id3_, joboperato3_.operator_id as operator1_3_, joboperato3_.operator_id as operator1_9_0_ FROM gateway.dbo.job_operators this_ left outer join gateway.dbo.job_operator_groups joboperato2_ on this_.id=joboperato2_.id left outer join gateway.dbo.job_operator_groups joboperato3_ on joboperato2_.operator_id=joboperato3_.operator_id
System.Data.SqlClient.SqlException: Invalid column name 'id'.
Invalid column name 'id'.
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, 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.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)
at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
Name and version of the database you are using:
MS Sql Server 2000 SP4
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html