zoukankan      html  css  js  c++  java
  • NHibernate Notes3_How to set a default value of column

    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

  • 相关阅读:
    误操作 rpm -e --nodeps zlib
    Raid阵列之简单介绍
    GpG使用指南
    hadoop系统的端口
    网站日志流量复杂分析
    Flume在企业大数据仓库架构中位置及功能
    Hue的安装与部署
    Hive中的数据倾斜
    Hive的三种Join方式
    如何每日增量加载数据到Hive分区表
  • 原文地址:https://www.cnblogs.com/haokaibo/p/2190551.html
Copyright © 2011-2022 走看看