NHibernate Notes3_How to set a default value of column
2011-09-25 22:50
435 查看
To set a default value of column we need to take the advantage of setting the default value of the column within the native DB. In NHibernate we should the set the attritute "generated" to be "alway" or "insert" which will tell NHibernate to fullfill the value of the column with the value assigned from the native DB. Actually there are three value options for the "generated" attribute.
As it described in "NHibernate Reference Documentation". They are:
1. never (the default) - means that the given property value is not generated within the database.
2. insert - states that the given property value is generated on insert, but is not regenerated on subsequent updates. Things like created-date would fall into this category. Note that even though Section 5.1.7, “version (optional)” and Section 5.1.8, “timestamp (optional)” properties can be marked as generated, this option is not available there...
3. always - states that the property value is generated both on insert and on update.
And there is some sample code to illerstrate how to use it:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="EasyNH.Model" namespace="EasyNH.Model">
<class name="Product">
<id name="Id">
<generator class="guid.comb" />
</id>
<property name="Name" not-null="true" />
<property name="Description" />
<property name="UnitPrice" not-null="true" type="Currency" />
<property name="ServerTime" generated="always" type="DateTime" ></property>
</class>
</hibernate-mapping>
This is very useful when we want to set column value at the DB side. For example, if you want to set the update/insert time as precise service DB server time of a record in a table, you can set the column default value as "getdate()", and then set configuration described above in NHibernate.
And you can download my sample project from the link below. You can run the Unit Test to build the Test DB with method "Can_generate_schema" and test it with method "AddOrderTest" in "TestNH" project. You can only prove the dafault funciton with running it with Product entity but not others, because I did not finish that mapping whole correctly yet. Just enjoy it! :)
EasyNH.zip
As it described in "NHibernate Reference Documentation". They are:
1. never (the default) - means that the given property value is not generated within the database.
2. insert - states that the given property value is generated on insert, but is not regenerated on subsequent updates. Things like created-date would fall into this category. Note that even though Section 5.1.7, “version (optional)” and Section 5.1.8, “timestamp (optional)” properties can be marked as generated, this option is not available there...
3. always - states that the property value is generated both on insert and on update.
And there is some sample code to illerstrate how to use it:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="EasyNH.Model" namespace="EasyNH.Model">
<class name="Product">
<id name="Id">
<generator class="guid.comb" />
</id>
<property name="Name" not-null="true" />
<property name="Description" />
<property name="UnitPrice" not-null="true" type="Currency" />
<property name="ServerTime" generated="always" type="DateTime" ></property>
</class>
</hibernate-mapping>
This is very useful when we want to set column value at the DB side. For example, if you want to set the update/insert time as precise service DB server time of a record in a table, you can set the column default value as "getdate()", and then set configuration described above in NHibernate.
And you can download my sample project from the link below. You can run the Unit Test to build the Test DB with method "Can_generate_schema" and test it with method "AddOrderTest" in "TestNH" project. You can only prove the dafault funciton with running it with Product entity but not others, because I did not finish that mapping whole correctly yet. Just enjoy it! :)
EasyNH.zip
相关文章推荐
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- HOW TO CHANGE THE DEFAULT KEY-VALUE SEPARATOR OF A MAPREDUCE JOB
- How-to increase the default timeout value of vSphere Web Client
- How to set default value for a field
- Cognos How to set the default email sender to a specific value
- django: BooleanField, how to set the default value to true? - Stack Overflow
- How to get position of key/value in LinkedHashMap using its key
- How to get Hidden Column Value in GridView(ASP.net GridView如何获取隐藏列的值)
- How to workaround the IE default behavior of re-posting the entire form when pressing "刷新"(or F5)
- How to change the default run level of a RedHat 9.0 or Fedora Core Linux system
- Default value for column cannot be changed to NULL
- A value is trying to be set on a copy of a slice from a DataFrame
- Cannot insert explicit value for identity column in table ‘table’ when IDENTITY_INSERT is set to OFF
- How to set custom colors in amCharts Column Chart
- how to set value for cmake arguement in command
- understanding positions and screen resolution, how to properly set position of object?
- How to set colors of HTML tables
- How to get request_id of previous request in a request set?
- How to add,edit and delete key-value in localStorage of Browse
- How to use pysphere set VM cpu or memory reservationrese value