I’m trying to write small program that will generate my DB tables, sequences and trigger from code, using NHibernate.
With SchemaExport.Create() method I was able to create all the table and relevant sequences, but I was not able to create triggers. Therefore I tried to use session.CreateSQLQuery() to run command that adds the trigger to the DB.
My code looks like this:
string createTriggerQuery= @"create or replace trigger table_insert_trigger before insert on Table for each row begin select TableSequence.nextval into :new.ID from dual; end;";
var query = session.CreateSQLQuery(createTriggerQuery);
query.ExecuteUpdate();
The query works as I run it on the Oracle SQL Developer, but as I execute my code I’m getting this exception:
Could not execute native bulk manipulation query:create or replace trigger... [SQL: SQL not available]
I also tried to use HBM queries to create the query. I add XML file to the project as embedded resource and on it I add the following code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<database-object>
<create>
create or replace
trigger table_insert_trigger before insert on table
for each row
when (new.ID is NULL)
begin
select tableSequence.nextval into :new.ID from dual;
end;
</create>
and in my FluentConfiguration member I configured it to add hbm mapping:
_fluentConfiguration.Mappings(m => m.HbmMappings.AddFromAssemblyOf<DbContextFactory>());
but that was not working either.
Does anyone know how can I add the trigger to my DB from code?
The problem in this particular instance is the query you are trying to execute against the database, specifically this part:
The colon (
:) is being interpreted by NHibernate as an indicator of a parameter, and substituting it with?(since you aren’t actually passing a parameter). So it is actually passing an SQL that looks like this,At which point Oracle complains.
Currently, there is no way for NHibernate to escape the
:and pass it literally to the database, as noted here.Under a similar constraint, the solution was to use an
ORMother than NHibernate. If this is the required purpose, maybe NHibernate is not the best option.