Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is the article at the link below about implementing custom identity generator (see: <strong>Part 1: Inheriting from TableGenerator class</strong>) but the example throws the exception for SQLite database (<strong>"SQLite errorr no such table: hibernate_unique_key"</strong>). Thus as regard SQLite there is no possibility to gain current id key from a table. It uses class <strong>TableGenerator</strong> from NHibernate API (NHibernate.Id);</p> <p><a href="http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx" rel="nofollow">http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx</a></p> <p>To avoid the exception I implemented another solution (especially the way of getting current Id). It takes advantage of Fluent-NHibernate API (<strong>GeneratedBy.Custom()</strong>). Look at the following source code:</p> <pre><code>public class MyAutoincrement&lt;T&gt; : IIdentifierGenerator where T : IId { #region IIdentifierGenerator Members public object Generate(ISessionImplementor session, object obj) { NHibernate.ISession s = (NHibernate.ISession)session; int seedValue = 1000; int maxId = -1;//start autoincrement from zero! (fluent nhibernate start from 1 as default) List&lt;T&gt; recs = s.Query&lt;T&gt;().ToList&lt;T&gt;(); if (recs.Count &gt; 0) { maxId = recs.Max(x =&gt; x.getId()); } return seedValue + maxId + 1; } #endregion } //Interface for access to current Id of table public interface IId { int getId(); } //Entity public class MyEntity : IId { public virtual int Id { get; protected set; } public virtual string MyField1 { get; set; } public virtual string MyField2 { get; set; } #region IId Members public virtual int getId() { return this.Id; } #endregion } //Entity Mapping public class MyEntityMap : ClassMap&lt;MyEntity&gt; { public MyEntityMap() { Id(x =&gt; x.Id).GeneratedBy.Custom&lt;MyAutoincrement&lt;MyEntity&gt;&gt;(); Map(x =&gt; x.MyField1); Map(x =&gt; x.MyField1); } } </code></pre> <p>It works with SQLite database and involves custom identity seed.</p> <p>Regards Bronek</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload