我的這個自定義的Generator設置如下:
<param name="ObjectName">Child</param>
</generator>
StreamGenerator的代碼:
using System.Data;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using NHibernate.Id;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
using NHibernate.Type;
using NHibernate.Util;
using NHibernate.Dialect;
using NHibernate.Mapping;
namespace HYLQ.Core.Domain
{
class StreamGenerator : IPersistentIdentifierGenerator, IConfigurable
{
/**//// <summary></summary>
public const string ObjectNameTarget = "ObjectName";
public const string procNameTarget = "ProcedureName";
private string objectName;
private string procName = "usp_GetID";
private long next;
private System.Type returnClass;
/**//// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="d"></param>
public void Configure(IType type, IDictionary parms, Dialect d)
{
objectName = parms[ObjectNameTarget] as string;
if (parms[procNameTarget] != null)
{
procName = parms[procNameTarget] as string;
}
returnClass = type.ReturnedClass;
}
/**//// <summary>
///
/// </summary>
/// <param name="session"></param>
/// <param name="obj"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.Synchronized )]
public object Generate(ISessionImplementor session, object obj)
{
if (objectName != null)
{
getNext(session);
}
return IdentifierGeneratorFactory.CreateNumber(next, returnClass);
}
private void getNext(ISessionImplementor session)
{
SqlConnection conn = (SqlConnection)session.Factory.OpenConnection();
SqlCommand qps = conn.CreateCommand();
qps.CommandText = procName;
qps.CommandType = CommandType.StoredProcedure;
qps.Parameters.Add(new SqlParameter("@ObjectName", SqlDbType.VarChar,10));
qps.Parameters["@ObjectName"].Value = objectName;
try
{
next = int.Parse(qps.ExecuteScalar().ToString());
}
catch
{
throw;
}
finally
{
session.Factory.CloseConnection( conn );
}
}
/**//// <summary>
///
/// </summary>
/// <param name="dialect"></param>
/// <returns></returns>
public string[] SqlCreateStrings(Dialect dialect)
{
return new string[0];
}
/**//// <summary>
///
/// </summary>
/// <param name="dialect"></param>
/// <returns></returns>
public string SqlDropString(Dialect dialect)
{
return null;
}
/**//// <summary></summary>
public object GeneratorKey()
{
return objectName;
}
}
}
這裏是調用一個存儲過程來生成流水號的。
存儲過程和表的定義如下:
create table Stream
(
ObjectName varchar(10) not null, --對象名
MaxID int not null, --流水號
primary key(ObjectName)
)
go
--流水號發生器
if exists(select * from sysobjects where type='p' and name='usp_GetID')
drop proc usp_GetID
go
create proc usp_GetID
(
@ObjectName varchar(10) --對象名
)
as
begin
declare @MaxID int
SET NOCOUNT ON
if exists(select * from Stream where ObjectName=@ObjectName)
begin
select @MaxID=MaxID from Stream where ObjectName=@ObjectName
update Stream set MaxID=MaxID+1 where ObjectName=@ObjectName
end
else
begin
set @MaxID=1
insert into Stream values(@ObjectName,2)
end
select @MaxID as MaxID
end
go
希望對大家能有所幫助!