Thursday, October 13, 2005

Sequence = Firebird Generator in NHibernate 1.0

Code below shows how to use "sequence" to have an auto-incremental field in NHibernate 1.0 with Firebird.
I use Generator(Class="sequence") to make a field to be autoinc, and if you want to specify a generator name, you can use a Param(Name="sequence", Content) attribute.


namespace TestNHibernate
{
[Serializable]
[Class(NameType=typeof(TestClass))]
public class TestClass {

private int _id;
private string _name;

public TestClass() {
}

[Id(1, Name="Id")]
[Generator(2, Class="sequence")]
[Param(3, Name="sequence", Content="TestClass_Id_Gen")]

public int Id {
get{ return _id;}
set{ _id = value; }
}

[Property(Name="Name", Length=10)]
public string Name {
get { return _name; }
set { _name = value; }
}
}
}

2 comments:

Pierre Henri Kuat? said...

Hi,

That's a nice sample :)

Little precision:
NHMA can guess the type/name in Class and Propery; so you can write:

[Class]
public class TestClass [...]

and

[Property(Length=10)]
public string Name

chanwit said...

Thanks Pierre,
I'll try what you suggested.