如何写hibernate配置文件

一、hibernate.cfg.xml文件:
     <!--该文件的开头信息,对Hibernate而言,该类文件基本都这么开头:)-->

     <?xml version='1.0' encoding='UTF-8'?>
     <!DOCTYPE hibernate-configuration PUBLIC 
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
        "
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     <!--
正文开始 -->
     <hibernate-configuration>
     <!--
下面是数据库的基本连接信息,对一个应用来说,设置一个session-factory节点就够了,除非我们中间使用了多个数据库-->
     <session-factory>
     <!--
用户名 -->
     <property name="connection.username">root</property>
     <!--url
信息 --> 
     <property name="connection.url">jdbc:mysql://localhost:3306/webases</property>
     <!--
数据库方言信息--> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     <!--
密码 --> 
     <property name="connection.password">274507</property>
     <!--
数据库驱动信息 --> 

     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
     <!--
指定Hibernate映射文件路径 -->
     <mapping resource="com/Hibernate/test_products.hbm.xml" />
     </session-factory>
     </hibernate-configuration>
二、.hbm.xml文件:

1.实体

        public virtual string Code { get; set; }
        public virtual string VoucherDate { get; set; }
        public virtual Guid? PersonID { get; set; }
        public virtual Person Person { get; set; }
        public virtual Guid? ProjectID { get; set; }
        public virtual Project Project { get; set; }
        public virtual Guid? UpdaterID { get; set; }
        public virtual User Updater { get; set; }
        public virtual string UpdateTime { get; set; }
        public virtual Guid? CreaterID { get; set; }
        public virtual User Creater { get; set; }
        public override string CreateTime { get; set; }
        public virtual Guid? AuditerID { get; set; }
        public virtual User Auditer { get; set; }
        public virtual string AuditeTime { get; set; }
        public virtual string Memo { get; set; }
        public override Guid ID { get; set; }
        public virtual Guid? WarehouseID { get; set; }
        public virtual Warehouse Warehouse { get; set; }
        //public override string CreateTime { get; set; }


        public virtual IList<StockInDetail> Details { get; set; }

        public virtual IList<StockInVerificationDetail> VerificationDetail { get; set; }

2.配置文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Haso.Swms.Entities" namespace="Haso.Swms.Entities.Stock">
  <class name ="StockIn" table="StockIn">
    <id name="ID"></id>
    <property name ="Code"/>
    <property name ="VoucherDate"/>
    <property name ="PersonID" not-null="false"/>
    <many-to-one column="PersonID" name="Person" insert="false" update="false" not-null="false" class="Haso.Swms.Entities.System.Person,Haso.Swms.Entities" foreign-key="FK_StockIn_Person" unique="true" lazy="false"/>

    <property name ="ProjectID" not-null="false"/>
    <many-to-one column="ProjectID" name="Project" insert="false" update="false" not-null="false" class="Haso.Swms.Entities.System.Project,Haso.Swms.Entities" foreign-key="FK_StockIn_Project" unique="true" lazy="false"/>

    <property name ="UpdaterID" not-null="false"/>
    <many-to-one column="UpdaterID" name="Updater" insert="false" update="false" not-null="false" class="Haso.Swms.Entities.System.User,Haso.Swms.Entities" foreign-key="FK_StockIn_Updater" unique="true" lazy="false"/>
    <property name ="UpdateTime"/>

    <property name ="CreaterID" not-null="false"/>
    <many-to-one column="CreaterID" name="Creater" insert="false" update="false" not-null="false" class="Haso.Swms.Entities.System.User,Haso.Swms.Entities" foreign-key="FK_StockIn_Creater" unique="true" lazy="false"/>
    <property name ="CreateTime"/>

    <property name ="AuditerID" not-null="false"/>
    <many-to-one column="AuditerID" name="Auditer" insert="false" update="false" not-null="false" class="Haso.Swms.Entities.System.User,Haso.Swms.Entities" foreign-key="FK_StockIn_Auditer" unique="true" lazy="false"/>
    <property name ="AuditeTime"/>
    <property name ="Memo"/>

    <property name ="WarehouseID" />
    <many-to-one column="WarehouseID" name="Warehouse" insert="false" update="false" not-null="false" class="Haso.Swms.Entities.System.Warehouse,Haso.Swms.Entities" foreign-key="FK_StockIn_Warehouse" unique="true" lazy="false"/>
   
    <bag name="Details" table="StockInDetail" generic="true" inverse="true" cascade="all">
      <key column="StockInID" foreign-key="FK_StockInDetail.StockIn"/>
      <one-to-many class="Haso.Swms.Entities.Stock.StockInDetail,Haso.Swms.Entities"/>
    </bag>

    <bag name="VerificationDetail" table="StockInVerificationDetail" generic="true" inverse="true" cascade="all">
      <key column="StockInID" foreign-key="FK_StockInVerificationDetail_StockIn"/>
      <one-to-many class="Haso.Swms.Entities.Stock.StockInVerificationDetail,Haso.Swms.Entities"/>
    </bag>

  </class>
</hibernate-mapping>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值