我的这个自定义的Generator设置如下:
<
generator
class
="HYLQ.Core.Domain.StreamGenerator, HYLQ.Core"
>
< param name ="ObjectName" > Child </ param >
</ generator >
这样我会为每个ObjectName都会产生一个顺序的流水号。
< param name ="ObjectName" > Child </ param >
</ generator >
StreamGenerator的代码:
using
System;
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;
}
}
}
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
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
希望对大家能有所帮助!