zoukankan      html  css  js  c++  java
  • Many to many example 1 of 4

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TstDBConnection.Entities" assembly="TstDBConnection">
      <class name="Product" table="Product">
        <id name="Id" column="Id">
          <generator class="guid.comb"/>
        </id>
        <property name="Name" column="Name"></property>
        <bag name="Orders" generic="true" table="ProductOrder" cascade="save-update">
          <key column="ProductId" />
          <many-to-many column="OrderId" class ="Order" />
        </bag>
      </class>
    </hibernate-mapping>
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TstDBConnection.Entities" assembly="TstDBConnection">
      <class name="Order" table="[Order]">
        <id name="Id" column="Id">
          <generator class="guid.comb"/>
        </id>
        <property name="Name" column="Name"></property>
        <bag name="Products" generic="true" table="ProductOrder" cascade="save-update">
          <key column="OrderId" />
          <many-to-many column="ProductId" class ="Product"/>
        </bag>
      </class>
    </hibernate-mapping>
    namespace TstDBConnection.Entities
    {
        public class Order
        {
            public virtual Guid Id { get; set; }
            public virtual string Name { get; set; }
            public virtual IList<Product> Products { get; set; }
        }
    }
    namespace TstDBConnection.Entities
    {
        public class Product
        {
            public virtual Guid Id { get; set; }
            public virtual string Name { get; set; }
            public virtual IList<Order> Orders { get; set; }
        }
    }
            public static void TestMany2Many(ISessionFactory sessionFactory)
            {
                using (ISession session = sessionFactory.OpenSession())
                {
                    Product product = new Product() { Name = "A product" };
                    Order order = new Order()
                    {
                        Products = new List<Product>() { product },
                        Name = "Coffee CupBox"
                    };
                    var id = session.Save(order);
                    session.Flush();
                }
            }
            static void Main(string[] args)
            {
                log4net.Config.XmlConfigurator.Configure(); // log4net
                Configuration configuration = new Configuration();
                configuration.Configure();
                ISessionFactory sessionFactory = configuration.BuildSessionFactory();
    
                TestMany2Many(sessionFactory);
    
                sessionFactory.Close();
            }

  • 相关阅读:
    list for循环中删除元素
    XMLFeedSpider例子
    myeclipse一直卡在loading workbench解决方法
    代码
    在Github上面搭建Hexo博客(一):部署到Github
    RegEX正则表达式截取字符串
    将后台值传单前台js接收
    C# List<T>泛型用法
    基于jQuery——TreeGrid
    在线编程学习网站
  • 原文地址:https://www.cnblogs.com/webglcn/p/2673392.html
Copyright © 2011-2022 走看看