Using NHibernate as an ORM easy to encounter problem

Source: Internet
Author: User
Tags generator insert

1, could not initialize proxy-no session.

Reason: The default option for Hibernate3 many-to-one is lazy = "Proxy"

> Solution:<many-to-one> & <set> Set lazy= "false"

In fact, only in <many-to-one> set lazy= "false" on it, the set also sets the words will be more than one query.

>2, table with no primary key how to write. Hbm.xml configuration file

> has. hbm.xml configuration requires a

<id name= "id" >
     <generator class= "GUID" ></generator>
</id>

No <id> configuration section will complain, but some tables do not have a primary key.

For example, some from a table, such as a list of orders, in most cases, do not require a primary key.

In fact, no <id> can also, but need to have <composite-id>, that is, the joint primary key. A federated primary key is a few attributes uniquely identifying the current record.

In another way, add all the attributes together as a joint primary key.

<composite-id>
      <key-property name= "Amount" ></key-property>
      <key-property name= " Createtime "></key-property>
      <key-property name=" Modifytime "/>
    </composite-id>

3, after the joint primary key need to pay attention to place

The corresponding model needs to implement the Equals and GetHashCode method after using the Union primary key, otherwise it will be an error.

public override bool Equals (object obj)
     {
         var item = obj as Crmtypecustomer;
         return this. Crmtypeid = = Item. Crmtypeid
             && this. CustomerID = = Item. CustomerID;
     }
     public override int GetHashCode ()
     {return
         base. GetHashCode ();
     }

4, index 4 of this sqlserverparametercollection's count=4 is invalid

The reason is:

The problem when the primary table is associated from a table.

<many-to-one name= "dictate" column= "Dictateid" class= "Model.jkptoaxtdictate,model"/>

Modified to:

<many-to-one name= "dictate" column= "Dictateid" class= "Model.jkptoaxtdictate,model" insert= "false" update= "false" />

That is, add a property

Insert= "false" update= "false"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.