AnantLabs / codesmith

Automatically exported from code.google.com/p/codesmith
1 stars 0 forks source link

NHibernate generation problem on composite IDs #334

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = 
OBJECT_ID(N'[log].[Event]') AND type in (N'U'))
BEGIN
CREATE TABLE [log].[Event](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[DeleteDate] [int] NOT NULL,
[Environment] [varchar](32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[AsOfDate] [datetime] NULL,
[GenerationDate] [datetime] NOT NULL,
[LogTimestamp] [datetime] NOT NULL,
[Kind] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Section] [varchar](1024) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SubSection] [varchar](1024) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SubSubSection] [varchar](1024) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Message] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[GUID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ContextName] [varchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Contributor] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Project] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[EntryAssembly] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CurrentThread] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[UserName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MachineName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_Event] PRIMARY KEY NONCLUSTERED 
(
[ID] ASC,
[DeleteDate] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = 
OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = 
OBJECT_ID(N'[log].[Event]') AND name = N'Event_MainIndex')
CREATE CLUSTERED INDEX [Event_MainIndex] ON [log].[Event] 
(
[GUID] ASC,
[DeleteDate] ASC,
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = 
OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)

What is the expected output? What do you see instead?

public override string Id
{
get
{
System.Text.StringBuilder uniqueId = new System.Text.StringBuilder();
uniqueId.Append(_id.ToString());
uniqueId.Append("^");
uniqueId.Append(_deleteDate.ToString());
return uniqueId.ToString();
}
}

public virtual long Id
        {
            get { return _id; }
set
{
OnIdChanging();
_id = value;
OnIdChanged();
}
        }
So there is 2 times the ID property ..... which doesn't compile of course.

Please use labels and text to provide additional information.

This table errors because you have a composite primary key and one of the 
columns is the same name as our composite key ID. Also could you please log 
that this table doesn't generate under VB as well.

As a temporary work around you could use the CS_Alias extended property on 
the ID column to rename that column to a different name.

Original issue reported on code.google.com by metash...@gmail.com on 6 Apr 2010 at 8:05

GoogleCodeExporter commented 9 years ago
Hello,

Currently we do not support Composite keys in the NHibernate templates. We are 
revamping a huge 2.0 release in the future which will include this support. You 
can 
get around this ID bug by setting a CS_Alias extended property on the ID column 
in 
your database with the value being the new name of this column (E.G. 
Identification).

Thanks
-Blake Niemyjski

Original comment by bniemyjski on 14 Apr 2010 at 3:46

GoogleCodeExporter commented 9 years ago
Hi. I have the same problem.
I have a master table with a Unique key field called "IdPrimaNota".
This table has a relation with a Detail with composite key which is 
"IdPrimaNota -
NumeroRiga".
the result i wish to get is a hbm file with a <bag> with a reference to the 
detail
table. Likewise I wish to obtain a class with a IList<Detail> property.
When will you release an update?
I tried to use your suggest work aournd but the result is the same.

I have another problem.
To resolve this issue i've tried to modify NHibernateHelper Solution.
I compile the solution and then i put the generated dll in the 
Samples\v5.2\Templates\Frameworks\NHibernate\Common\NHibernateHelper 
directory.
I have a strange behaviour.
The modfies i have made don't have any effect.
I had the doubt that i have placed the new dll in a wrong path but i suppose is
correct because if i delete the dll, generate process give me an error.

If i raname a method (Init for example) generate process give me an error
but if i modify a method inserting an exception everything is ok !!
I have inserted in a method some code that write a file to a  disk but never 
happens!!

So the question is: Until I'm waiting for your release..
How can i try to modify the generation process?

Original comment by m.cec...@gmail.com on 13 May 2010 at 9:13

GoogleCodeExporter commented 9 years ago
Today i've tried to modify NHibernateHelper project and use the dll in my 
CodeSmith
project.
i don't know why but now my code has effect.

I resolve composite key generation only putting a comment in one row of code of
GetToMany() method of Entity Manager.cs
At the end of procedure there is this code:
   else if (!column.IsPrimaryKeyMember)
I've changed it in
   else //if (!column.IsPrimaryKeyMember)

this is the entire method

protected void GetToMany()
        {            
            foreach (TableKeySchema tks in _sourceTable.PrimaryKeys)
            {
                if (tks.ForeignKeyMemberColumns.Count > 1)
                {
                    GetMembers(_sourceTable.ForeignKeyColumns);
                }
                else
                {
                    ColumnSchema column = tks.ForeignKeyMemberColumns[0];

                    if (_associationMap.ContainsKey(column) ||
NHibernateHelper.IsExcludedColumn(column.Name))
                        return;

                    if (NHibernateHelper.IsManyToMany(column.Table))
                    {
                        TableSchema foreignTable = GetToManyTable(column.Table,
_sourceTable);
                        if (!_excludedTables.Contains(foreignTable))
                        {
                            EntityAssociation association = new
EntityAssociation(AssociationTypeEnum.ManyToMany, foreignTable, column);
                            _associationMap.Add(column, association);
                        }
                        else
                        {
                            GetMember(column);
                        }
                    }
                    else //if (!column.IsPrimaryKeyMember)
                    {
                        if (!_excludedTables.Contains(column.Table))
                        {
                            EntityAssociation association = new
EntityAssociation(AssociationTypeEnum.OneToMany, column.Table, column);
                            _associationMap.Add(column, association);
                        }
                        else
                        {
                            GetMember(column);
                        }
                    }
                }
            }                        
        }

Original comment by m.cec...@gmail.com on 13 May 2010 at 2:07