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

  • 相关阅读:
    Nginx配置,请求到tomcat中
    读取远程数据库的视图(包括如何连接其他的数据库)
    slf4j日志的使用-学习笔记
    Freemarker生成word文档的时的一些&,>,<报错
    freemarker的replace的使用
    防止表单重复的常用几种方式
    两种表复制语句(SQL)
    总结:如何判断一个对象是否可被回收
    24种设计模式优缺点及适用场景#抽象工厂模式
    24种设计模式优缺点及适用场景#工厂方法模式
  • 原文地址:https://www.cnblogs.com/haokaibo/p/2190551.html
Copyright © 2011-2022 走看看