개발 이야기/ORM2012. 4. 15. 12:36

NHibernate xml 파일

NHibernate ORM으로 XML에서 설정을 한다오늘은 NHibernate 프로젝트 관련하여 포함된 Config 파일 및 객체 정의하는xml파일들을 살펴보고자한다지난 세션에서 소개한 NHibernate 사이트에서 NH3.1.0 다운로드르 받으면 xml 파일 템플릿이 제공되고 있다.

 

Configuration_Templates

다운받은 파일의 \NHibernate-3.1.0.GA-bin\Configuration_Templates 폴더에 Database 설정 관련 xml이 존재한다.

17_01.jpg

[그림 17-1] Database 정의 xml

 

MSSQL.cfg.xml 파일을 열어보자. MS SQL Server 연결관련 정보가 설정되어 있다우리는 여기 정보를 프로젝트 app.config web.config에 정의하게 될 것이다.

<?xml version="1.0" encoding="utf-8"?>

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >

             <session-factory name="NHibernate.Test">

                           <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>

                           <property name="connection.connection_string">

                                        Server=(local);initial catalog=nhibernate;Integrated Security=SSPI

                           </property>

                           <property name="adonet.batch_size">10</property>

                           <property name="show_sql">false</property>

                           <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>

                           <property name="use_outer_join">true</property>

                           <property name="command_timeout">60</property>

                           <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>

                           <propertyname="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>

             </session-factory>

</hibernate-configuration>   

 

 

<table>.hbm.xml 정의

NHibernate는 테이블 기준으로 매핑을 합니다하여 테이블마다 xml 파일이 존재한다고 보시면 됩니다샘플로 하나 살펴보겠습니다구조만 확인을 해주세요저희가 자주사용하는 Northwind 데이터베이스 Shippers 테이블을 정의해놓은 파일입니다.Shippers.hbm.xml 으로 파일이 생성됩니다클래스와 데이블간의 매핑을 시켜주는 역할입니다.

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateDemo"assembly="NHibernateDemo">

  <class name="NHibernateDemo.Entities.Shippers" table="Shippers">

    <id name="ShipperID" column="ShipperID" type="Int32" unsaved-value="-1" access="field.camelcase-underscore">

      <generator class="identity" />

    </id>

    <property name="CompanyName" column="CompanyName" type="string" length="40" not-null="true"access="field.camelcase-underscore"/>

    <property name="Phone" column="Phone" type="string" length="24" access="field.camelcase-underscore"/>

 

  </class>

</hibernate-mapping>

 

 

Generators 요소

모든 generators는 NHibernate.Id.IIdentifierGenerator에서 구현됩니다.

l Increment : 다른 프로세스에서 데이터가 추가되지 않는 경우 유니크하게 사용되는 식별자로 클러스터에서는 사용을 금지합니다.

l Identity : DB2, MySQL, MS SQL Server 그리고 Sybase 데이터 베이스에서 지원하는 식별자 컬럼입니다.

l Sequence : DB2, PostgreSQL, Oracle 또는 Firebird의 generator에서 사용되는 순서입니다이 식별자는 Convert.ChangeType속성 타입을 사용하여 변환합니다.

l Hilo : hi/lo 알고리즘 사용으로 integral 타입의 식별자를 효과적으로 생성합니다. hi/lo 알고리즘은 특정 데이터베이스에서 고유 식별자를 생성하기도 합니다.

l Seqhilo : hi/lo 알고리즘을 사용하여 효과적인 integral 타입의 식별자를 생성하며 데이터베이스 순서가 명명됩니다.

l uuid.hex : System.Guid와 ToString 메서드로 문자열 타입의 식별자를 생성합니다문자열 길이는 구성 포맷에 따릅니다.

l uuid.string : 새로운 System.Guid가 문자열로 변환되는 byte[]으로 생성합니다.

l Guid : 새로운 System.Guid 식별자입니다.

l guid.comb : Jimmy Nilsson이 설명한(http://www.informit.com/articles/article.asp?p=25862) 새로운 System.Guid을 생성하는 알고리즘을 사용합니다.

l Native : 기본적인 데이터베이스의 기능에 따라 지정됩니다.

l Assigned : 응용프로그램에서 Save()이 호출되기 전에 객체 식별자를 지정합니다.

l Foreign : 다른 연관된 객체의 식별자를 사용합니다일반적으로 <one-to-one> 기본키가 사용됩니다.

 

NHibernate Type

Xml 파일 컬럼에 타입을 정의할 때 NHibernate Type으로 정의해야한다닷넷 타입과 데이터베이스 타입을 정리해놓은 것이다기본적으로 자주 사용되는 항목만 표시합니다더 자세한 항목은 NHibernate 가이드 문서에 제공되고 있습니다.

NHibernate Type

.NET Type

Database Type

AnsiChar

System.Char

Db - Type.AnsiStringFixedL ength - 1 char

Boolean

System.Boolean

DbType.Boolean

Byte

System.Byte

DbType.Byte

Char

System.Char

Db- Type.StringFixedLengt h - 1 char

DateTime

System.DateTime

DbType.DateTime – ignores the milliseconds

Decimal

System.Decimal

DbType.Decimal

Double

System.Double

DbType.Double

Guid

System.Guid

DbType.Guid

Int16

System.Int16

DbType.Int16

Int32

System.Int32

DbType.Int32

Int64

System.Int64

DbType.Int64

PersistentEnum

System.Enum

The DbType for the underlying value.

Single

System.Single

DbType.Single

Ticks

System.DateTime

DbType.Int64

TimeSpan

System.TimeSpan

DbType.Int64

NHibernate 경우 선수지식이 필요한 프레임워크입니다이번 세션에서는 기본적으로 알아둬야하는 사항을 몇가지 정리하였습니다작업을 하시다가 필요할때마다 참고하시면 좋을것 같습니다.^^


sqler에 아티클 작성 했던것을 공유합니다.

출처 : http://www.sqler.com/402212


Posted by 사나에