Nhibernate 2+oracle 配置

來源:互聯網
上載者:User

第一步:在oracle中建立表CAT

ID varchar2(32)

NAME varchar2(50)

SEX char(1)

WEIGHT float

建立sequence CAT_SEQUENCE

第二步:建立web應用程式QuickStart,

添加相關dll

Iesi.Collections.dll

LinFu.DynamicProxy.dll

log4net.dll

NHibernate.ByteCode.Castle.dll

NHibernate.ByteCode.LinFu.dll

NHibernate.dll

nunit.framework.dll

添加 xsd

nhibernate-configuration.xsd

nhibernate-mapping.xsd

第三步:建立實體類

public class CAT    {        public CAT(){}        public virtual string ID { get; set; }        public virtual string NAME { get; set; }        public virtual char SEX { get; set; }        public virtual float WEIGHT { get; set; }    }

第四步:添加對應檔 Cat.hbm.xml,修改屬性:【產生操作】【內嵌資源】

<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"                   assembly="QuickStart"                   namespace="QuickStart"                   >  <class name="CAT" table="CAT">    <id name="ID" column="ID" type="string">      <!--<generator class="uuid.hex"/>-->      <generator class="sequence">        <param name="sequence">CAT_SEQUENCE</param>      </generator>    </id>    <property name="NAME"/>    <property name="SEX"/>    <property name="WEIGHT"/>  </class></hibernate-mapping>

第五步:添加設定檔 hibernate.cfg.xml 【產生操作】【內嵌資源】【始終複製】

<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">  <session-factory>    <property name="connection.provider">      NHibernate.Connection.DriverConnectionProvider    </property>    <property name="connection.driver_class">      NHibernate.Driver.OracleClientDriver    </property>    <property name="dialect">      NHibernate.Dialect.Oracle10gDialect    </property>    <property name="connection.connection_string">      User ID=EOLANDA;Password=EOLANDA;Data Source=150152    </property>    <property name="proxyfactory.factory_class">      NHibernate.ByteCode.LinFu.ProxyFactoryFactory,      NHibernate.ByteCode.LinFu    </property>    <property name="show_sql">true</property>    <property name="query.substitutions">      true 1, false 0, yes 'Y', no 'N'    </property>    <mapping assembly="QuickStart"/>  </session-factory></hibernate-configuration>

第六步:寫一個類

 public sealed class NHibernateHelper    {        private const string CurrentSessionKey = "nhibernate.current_session";        private static ISessionFactory sessionFactory;        static NHibernateHelper()        {                        sessionFactory = new Configuration().Configure().BuildSessionFactory();        }        public static ISession GetCurrentSession()        {            HttpContext context = HttpContext.Current;            ISession currentSession = context.Items[CurrentSessionKey] as ISession;            if (currentSession == null)            {                currentSession = sessionFactory.OpenSession();                context.Items[CurrentSessionKey] = currentSession;            }            return currentSession;        }        public static void CloseSession()        {            HttpContext context = HttpContext.Current;            ISession currentSession = context.Items[CurrentSessionKey] as ISession;            if (currentSession == null) { return; }            currentSession.Close();            context.Items.Remove(CurrentSessionKey);        }        public static void CloseSessionFactory()        {            if (sessionFactory != null)            {                sessionFactory.Close();            }        }    }

第七步:

 

public sealed class CatBal    {        public static void CreateCat(CAT cat)        {            ISession session = NHibernateHelper.GetCurrentSession();            ITransaction tx = session.BeginTransaction();            CAT princess = new CAT            {                NAME = cat.NAME,                SEX = cat.SEX,                WEIGHT = cat.WEIGHT,                BIRTHDAY = cat.BIRTHDAY            };            session.Save(princess);            tx.Commit();            NHibernateHelper.CloseSession();        }    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.