2

I've defined a composite-id for my NHibernate database model. It seems to work, but I cannot set a generator for one key-property.

Is it possible to define a generator for a key-propery in a composite-id?

C# NHibernate Model

public const string STR_ID = "Id";
public const string STR_SHARDKEY = "ShardKey";

[CompositeId(-4)]
[KeyProperty(-3, Name = STR_ID, Column = STR_ID)]
[KeyProperty(-2, Name = STR_SHARDKEY , Column = STR_SHARDKEY )]
[Generator(-1, Class = "guid.comb")]
public virtual Guid? Id { get; set; }

[Column(Name = STR_SHARDKEY )]
public virtual int ShardKey{ get; set; }

XML Mapping

<composite-id>
    <key-property name="Id" column="Id" />
    <key-property name="ShardKey" column="ShardKey" />
</composite-id>

Shouldn't there be a generator tag like <generator class="guid.comb" /> somewhere?

annemartijn
  • 1,538
  • 1
  • 23
  • 45
  • compositeids don't have generators. Shouldn't the guid be enough to be unique? – Firo Oct 05 '15 at 13:51
  • Guid/uniqueidentier is normally generated by a function in SQL Server (`dbo.GuidComb()`), but I could do this at the application level. How about models with an auto increment Id? How would these work with composite-ids? – annemartijn Oct 05 '15 at 14:02
  • Having such compositeids is bad idea in the first place. If legacy databases need them then it might be possible to use them, but I'm not aware of a function in NHibernate. – Firo Oct 05 '15 at 14:07
  • It is useful if you are using aligned indices in combination with Partitioned tables. I am not of the same mind about composite-ids being a bad idea, but I do agree that it's hard/impossible to get it to work when using NHibernate. – annemartijn Oct 05 '15 at 14:12
  • Partitioning doesn't need the key to be primary key no? Just have the Shared key as non id property. CompositeIds with NHibernate is not optimal but it supports a lot. – Firo Oct 05 '15 at 14:19
  • Relationships depend on both keys. if they're not queried together the query will not perform. I could just match on the second key in the where clause, but this will not work for LEFT OUTER JOIN. I'll need to have the second key in the on statement for these joins. – annemartijn Oct 05 '15 at 14:25
  • 1
    then you are out of luck. As far as I know compositeIds don't have generators. – Firo Oct 06 '15 at 05:50
  • Did u find some solution to set a generator in the compositeId? – Pedro Franco Jan 14 '16 at 15:10
  • @pedro-franco no. But you could generate the id in code if you're using a `Guid` as id. to make it sequential you could copy this method into your project: [GuidCombGenerator.GenerateComb()](https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Id/GuidCombGenerator.cs#L45). I ended up not using compositeIds. – annemartijn Jan 14 '16 at 15:25

0 Answers0