How do I configure NHibernate to create the db schema with a column like this:
create_dt datetime not null default getdate()
I have this in the mapping file:
<property name="create_dt" update="false" insert="false" generated="insert" not-null="true" />
Is there anyway I can inject the sql server specific default getdate(). The documentation for generated properties even mentions this is how you handle a create_date field. I'm just not sure how to make my db schema generate properly. Will I have to edit the create table scripts manually?
Similar question.
EDIT: I figured out I can always change the table schema like so:
<database-object>
<create>ALTER TABLE Report ADD CONSTRAINT DF_report_create_dt DEFAULT getdate() FOR create_dt;</create>
<drop></drop>
</database-object>
and I could add a trigger in the same way for an update_dt type of field. This seems better than supplying explicit insert and update statements that use getdate().
I alway prefer to use the NHibernate Event system to set my audit properties like created date or update date. (See event system documentation here).
I prefer this approach because it keeps the logic out of my database layer but also it gives me the ability to have a single location in my code that is responsible for setting these values. And if I have a common base class for all my entities then I can even guarantee consistent behavior throughout my domain.
this is an answer on a thread for Hibernate... it should port over to nHibernate without changing it...
https://forum.hibernate.org/viewtopic.php?f=25&t=996901&view=previous
please see the last post.
Failing that, i always generate the "date created" of an object in the constructor of the class:
public class MyClass
{
private DateTime createdDate;
public MyClass()
{
createdDate = DateTime.Now;
}
}
Related
I'm getting back into NHibernate and I've noticed a new configuration property being used in examples: SchemaAutoAction. I cant seem to find documentation on what the various settings mean. The settings / my guesses as to what they mean are:
Recreate -- Drop and recreate the schema every time
Create -- If the schema does not exist create it
Update -- issue alter statements to make the existing schema match
the model
Validate -- Blow up if the schema differs from the model
Is this correct?
SchemaAutoAction is the same as schema-action mapping attribute.
As per docs:
The new 'schema-action' is set to none, this will prevent NHibernate
from including this mapping in its schema export, it would otherwise
attempt to create a table for this view
Similar, but not quite. The SchemaAutoAction is analogous to the configuration property hbm2ddl.auto, and its values are:
Create: always create the database when a session factory is created;
Validate: when a session factory is created check if the database matches the mappings and throw an exception otherwise;
Update: when a session factory is created issues DDL commands to update the database if it doesn't match the mappings;
Recreate: always creates the database and drop it when the session factory is disposed.
I have a persistent entity in ColdFusion and I need to update a property
property name="createdDateTime" ormtype="date";
to
property name="createdDateTime" ormtype="timestamp";
before, I use to delete the table then reload ORM. However,now I have data in my table I cannot just delete it. Is there anyway I can update this field in ORM without dropping the whole table?
Thanks
Yes, you should be able to just change the property and do ormReload(). Try it in a test environment first but the ormtype is not directly tied to the database type.
in your Application.cfc
this.ormSettings.dbCreate = "Update";
Anyway, in your case (date -> timestamp), the underlying SQL type should be the same (at least in SQL Server, which is datetime)
I have an existing NHibernate 2.1.2.4000 mapping that uses the mapping syntax:
<database-object>
<create>
CREATE VIEW View_Register AS ... (truncated for example)
</create>
<drop>
DROP VIEW View_Register
</drop>
</database-object>
When the database schema gets created, I use:
new SchemaExport(_configuration).Execute(true, true, false, aSession.Connection, tw);
The third parameter in the method above is 'dropOnly', which is obviously set to false.
In the past, my schema is created and the 'View_Register' is created successfully. Now, I'm re-running the schema generation, and the DROP is called at the beginning of the schema generation, but the CREATE is never called. I've confirmed this with multiple SQL profilers including the NH profiler.
Using explicit dialect declarations doesn't seem to work either:
<dialect-scope name="NHibernate.Dialect.MsSql2000Dialect"/>
<dialect-scope name="NHibernate.Dialect.MsSql2005Dialect"/>
<dialect-scope name="NHibernate.Dialect.MsSql2008Dialect"/>
Any ideas folks?
Thanks,
David
I'm a complete idiot...
My View_Register mapping/view was completely fine, and was not my problem.
A different view was failing to create BEFORE View_Register because of a renamed table. Apparently, subsequent database-object/create calls are aborted on any ADO exception thrown by a previous create.
Good to know, but I feel silly for answering my own question!
Can I specify the initial value for an IDENTITY column using Hibernate/JPA annotations on top of an HSQLDB database? The relevant source code looks like this so far:
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
The generated DDL looks like this:
id bigint generated by default as identity (start with 1)
What I'd like to do is make the ID start with a value of 10,000 via annotations.
Note: This is a new application so I'm using the latest versions of Hibernate and HSQLDB.
As this question was not initially tagged as Hibernate, it was probably missed by Hibernate experts. I just noticed the Hibernate HSQLDialect returns the (start with 1) for all identity creation. Therefore there is probably no way to override this.
You should be looking into how to execute an SQL statement to reset the identity AFTER the table is created but before data is added. The SQL looks like:
ALTER TABLE hibernate_generated_table_name ALTER COLUMN id RESTART WITH 10000
Am writing av analytical standard solution register from the doping laboratory that i work for and got stuck on the problem how to get the ipaddress of the client in to the audit table.
I have found a straight forward method for actually get the ipaddress on the web and my question is not about that.
I got triggers on every table inserting records in to the audit table and I do not want to wright the inserting of the ipaddress manualy in every trigger. I would like to have something like a DEFAULT Constraint do the actual insertion of the ipaddress, but when I try I get errors about sub Queries not allowed.
Here is the way to get the address,
SELECT client_net_address
FROM sys.dm_exec_connections
WHERE (session_id = ##SPID)
You can use a SQL function as the DEFAULT constraint
http://msdn.microsoft.com/en-us/library/ms186755.aspx.
If using SQL isnt enough, you can use a CLR function as a default constraint.
http://msdn.microsoft.com/en-us/library/ms186755.aspx
The documentation specifies that a DEFAULT constraint can be "a constant, NULL, or a system function". Since there is no system function that returns the net address of the client, you cannot do what you want directly.
A trigger is the obvious solution here. Audit triggers are often created and maintained automatically from a template anyway, because they are usually identical on all tables, and if you aren't doing this already then perhaps this would be a good opportunity to start. That would avoid your issue with manually adding the code everywhere.