views:

270

answers:

1

I am using Fluent NHibernate on a project that has an Oracle 10g database. I am also using SQLite with a schema generated from my mapping for testing. The problem I am having is that, since the primary keys on my tables are generated by Oracle Sequences, to get my Add methods to work correctly I had to add .GeneratedBy.Sequence({sequence name}) to the Id field of each of my mappings so that the mappings look something like this:

public CustomerMap()
{
    Id(x => x.Id)
    .Column("Cust_Id")
    .GeneratedBy.Sequence("SEQ_CONTNT_AREA");
    ...
    {More Mapping Code}
    ...
}

Once I added the GeneratedBy.Sequece to my mappings all of my tests began to fail giving me the following errors:

----> NHibernate.MappingException : could not instantiate id generator
----> NHibernate.MappingException : Dialect does not support sequences

These errors make sense to me since SQLite does not use Oracle Sequences for Primary Key generation. However, if I change my mapping to use GeneratedBy.Native(), all of my tests pass, but when I run the application I get the following error when trying to insert a new record:

InnerException = {System.Data.OracleClient.OracleException: ORA-02289: sequence does not exist
    at System.Data.OracleClient.OracleConnection.CheckError(OciErrorHandle errorHandle, Int32 rc)
    at System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle stateme...

Message: "could not get next sequence value[SQL: select hibernate_sequence.nextval from dual]"  string

This error message indicates to me that NHibernate either couldn't find the sequence needed to create the primary key for this table or it didn't even look for it. In either case, it appears to be trying to use 'hibernate_sequence' to get the value which it obviously cannot find since it doesn't exist.

I figure I'm probably missing something pretty simple due to my being pretty new to NHibernate. It is pretty important that I get both the tests and code working soon, as I will be handing this project over to someone else who as even less NHibernate experience than I do when I leave for my new job next week.

A: 

I know this is a huge hack, but could you do something like this?

public CustomerMap()
{
    if (SomeStaticClass.IsRunningUnitTests)
    {
        Id(x => x.Id)
        .Column("Cust_Id")
        .GeneratedBy.Native();
    }
    else
    {
        Id(x => x.Id)
        .Column("Cust_Id")
        .GeneratedBy.Sequence("SEQ_CONTNT_AREA");
    }

    ...
    {More Mapping Code}
    ...
}
Jon Kruger
You're right, its a big hack, but it's better than nothing.
Hamman359