problem with SQLCE4 and NHibernate 3.2.* with mapping Bag collection by code

86 views
Skip to first unread message

KrzysztofZ

unread,
Dec 29, 2011, 7:52:54 AM12/29/11
to nhusers
hi,

I've a strange problem with mapping by code in newest NH (3.2.1
downloaded from Git).
Environment:
I have two tables in my SQLCE 4.0 database: PNP_Project and
PNP_AssemblyBlock. The idea is that PNP_Project has list of
PNP_AssemblyBlocks.

Classes:
[Serializable]
public class BaseDTO<IdType> : IEquatable<IDTO<IdType>>
where IdType : global::System.IEquatable<IdType>
{
public BaseDTO();

public virtual IdType Id { get; set; }

public virtual bool Equals( IDTO<IdType> other ); /* implementation
skipped */
}
[Serializable]
public class Project : BaseDTO<int>
{
public virtual string Name { get; set; }

public virtual IList<AssemblyBlock> AssemblyBlockList { get; set; }
}

[Serializable]
public class AssemblyBlock : BaseDTO<int>
{
public virtual Project Project { get; set; }
}

Tables:
PNP_Project has column Id (int, PK, Not null) and Name (nvarchar(255))
PNP_AssemblyBlock has column Id (int, PK, Not null) and IdProject
(int, not null)

So this is really simple case. When I map the classes by hbm.xml like
this (excerpts):
<mapping for project>
[...]
<!-- Primary Key(s) -->
<id name="Id" column="Id" type="System.Int32">
<generator class="native"/>
</id>

<!-- Properties -->
<property column="Name" type="String" name="Name" not-
null="true" length="255" />

<!-- One-To-Many relations -->
<bag name="AssemblyBlockList" inverse="true" cascade="none"
lazy="false">
<key column="IdProject"/>
<one-to-many
class="PnP.Model.DTO.Core.AssemblyBlock,PnP.Model.DTO.Core" />
</bag>
[...]
</mapping for project>
<mapping for assembly block>
<!-- Primary Key(s) -->
<id name="Id" column="Id" type="System.Int32">
<generator class="native"/>
</id>

<!-- Properties -->
<many-to-one name="Project" column="IdProject"
class="PnP.Model.DTO.Core.Project,PnP.Model.DTO.Core" />
</mapping for assembly block>

Then everything works ok. But when I switch to mapping by code like
this:

public class ProjectMapping : ClassMapping<Project>
{
public ProjectMapping()
{
Lazy( true );
Table( "PNP_Project" );
Id( x => x.Id, map =>
map.Generator( NHibernate.Mapping.ByCode.Generators.Native ) );
Property( x => x.Name );

Bag( x => x.AssemblyBlockList, map =>
{
map.Table( "PNP_AssemblyBlock" );
map.Key( k => k.Column( "IdProject" ) );
map.Cascade( Cascade.None );
map.Lazy( CollectionLazy.NoLazy );
map.Inverse( true );
} );
}
}
public class AssemblyBlockMapping : ClassMapping<AssemblyBlock>
{
public AssemblyBlockMapping()
{
Lazy( true );
Table( "PNP_AssemblyBlock" );
Id( x => x.Id, map =>
map.Generator( NHibernate.Mapping.ByCode.Generators.Native ) );
ManyToOne( x => x.Project, map =>
{
map.Column( "IdProject" );
} );
}
}

Then I get error while retrieving list of projects:

A first chance exception of type 'System.InvalidCastException'
occurred in System.Data.SqlServerCe.dll
Additional information: GetBytes can not be called on a column of type
Int.

and earlier in call stack this is converted to ADOException:

Could not cast the value in field id0_ of type Int32 to the Type
SerializableType. Please check to make sure that the mapping is
correct and that your DataProvider supports this Data Type.

It looks like an error with Bag element, because the field "id0_" is
treated by NH like byte[] (don't know why) or some other type, but not
Int32 which it really is.
The generated SQL:
SELECT assemblybl0_.IdProject as IdProject0_, assemblybl0_.id as id0_
FROM PNP_AssemblyBlock assemblybl0_ WHERE
assemblybl0_.IdProject=@p0;@p0 = 1 [Type: Int32 (0)]

Where's the error?
Please help, because I really like the new mapping by code convention
and wouldn't like to return to xml.

thank,
Chris

hival

unread,
Dec 29, 2011, 1:22:40 PM12/29/11
to nhusers
Try this

Bag( x => x.AssemblyBlockList, map =>
{
map.Table( "PNP_AssemblyBlock" );
map.Key( k => k.Column( "IdProject" ) );
map.Cascade( Cascade.None );
map.Lazy( CollectionLazy.NoLazy );
map.Inverse( true );
},
rm => rm.OneToMany());

KrzysztofZ

unread,
Dec 29, 2011, 6:38:11 PM12/29/11
to nhusers
Thank you, that solved my problem!
Reply all
Reply to author
Forward
0 new messages