[Nhib.pdf] using an entity class to correspond to multiple database tables (2)

Source: Internet
Author: User

The last time we saw how to enable Nhibernate to implement dynamic ing by writing multiple ing "semi-automated.

This time, we will look at how to implement multi-to-one relationships and how to access data.

1 ing when two XXX tables have many-to-one relationships

Since only the class is specified, nhib.pdf still does not know which table the class should be mapped to. Therefore, the same entity name must be specified.

Code <Class name = "childhistoryentity" entity-name = "mfschildhistoryentity" table = "tblmfschildhistories">
<ID name = "ID" type = "guid" column = "uidchildhistorykey">
<Generator class = "guid. Comb"/>
</ID>
...
<Your-to-one name = "child" class = "childentity" entity-name = "mfschildentity" column = "uidchildkey" not-null = "true"/>
...
</Class>

 

2. How to save or update an object

The isession interface of Nhibernate provides the SAVE (update and other methods are the same) overload method, you can specify the entity name

However, since the getentityname method has been intercepted in interceptor, we can call isession. Save (Object entity) as before)

3. How to retrieve objects

Since nhib.pdf cannot directly push the table name by class name during retrieval, We must manually specify the entity name.

When using isession. load, we need to use the isession. Load (string entityname, Object ID) method.

When using criteria, we need to input entityname during createcriteria.

When using hql, we also need to change from the original from classname to from entityname

 

This is probably the case.

The next research direction is to automatically generate all possible XXX mapping through a set of Template mapping. The general idea is to deserialize the XML and then clone several copies, replace some items before serialization. Prepare the source code of nhib.pdf and check whether there are any ready-to-use items in hbm2ddl ~~

 

Update: hql does not seem to work, at least I have not found a solution. The details are as follows:

Suddenly found a problem: the following ut code suddenly found to be unable to run

Code [Test]
Public void savechild ()
{
VaR child = new childentity
{
IDNO = "s1234567d ",
Idtype = childidtype. nric,
...
};
Childdao. Save (child );

Assert. arenotequal (guid. Empty, child. ID );
Assert. isnotnull (loadbyidtypeandno (child. centre. Bu, child. idtype, child. IDNO ));
}

Public childentity loadbyidtypeandno (string Bu, childidtype idtype, string IDNO)
{
Return childdao. findbyidtypeandno (Bu, idtype, IDNO );
}

 

I looked at the SQL statement generated by NH and was shocked.

Code Nhib.pdf: insert into tblmfschil.pdf...
Nhibernate: Select... from tblmfschildren mfschilden0 _ Where mfschilden0 _. descridtype = @ P0 and mfschilden0 _. stridno = @ P1; @ p0 = 'n', @ p1 = 's1234567d'
Nhibent: Select... from tblccchildren ccchildent0 _ Where ccchildent0 _. mongodtype = @ P0 and ccchildent0 _. stridno = @ P1; @ p0 = 'n', @ p1 = 's1234567d'
Nhibchildren: Select... from tbllshchildren lshchilden0 _ Where lshchilden0 _. mongodtype = @ P0 and lshchilden0 _. stridno = @ P1; @ p0 = 'n', @ p1 = 's1234567d'

 

Among them, MFs, CC, and lsh are three possible values of my XXX.

Looking back at the DaO statement, there are no three select tables.

Code Public childentity findbyidtypeandno (string Bu, childidtype idtype, string IDNO)
{
VaR query = string. Format ("select C from {0} childentity as c Where C. mongodtype =: idtype and C. IDNO =: IDNO", bu );
Return session. createquery (query)
//. Setenum ("idtype", idtype)
. Setcharacter ("idtype", (char) idtype)
. Setansistring ("IDNO", IDNO)
. Setmaxresults (1)
. Uniqueresult <childentity> ();

}

 

Recall that from the last ut run to the current ut failure, the job done is to extract mapping from (mFs) added the three parts above (please look forward to the following sections on how to do this ).

So the problem is probably that when NH translates hql, the part of from entityname is first translated into classname, and three entity corresponds to the same classname, so three SQL statements are generated. However, if you directly specify it as from classname, an error is returned indicating that there is no mapping corresponding to classname.

After searching for the IQUERY Method for half a day, there is nothing to manually specify the entityname, and you have to give up hql... Which of the following methods can be used?

However, we have long been confused about hql ~~ It cannot obtain the compiler check, and is easy to write errors (such as writing less space ). But I still don't use the criteria API (it's too difficult to read ...) It doesn't matter. I want to try linqtonhibernate for a long time ~~

Code Public childentity findbyidtypeandno (string Bu, childidtype idtype, string IDNO)
{
Return session. LINQ <childentity> (Bu + childentity. staticentityclassname)
. Where (child => child. mongodtype = (char) idtype
& Child. IDNO. Equals (IDNO, stringcomparison. ordinalignorecase ))
. Firstordefault ();
}

 

~~ Simple and pleasing to the eye ~~ The ut results are correct, that is, I do not know how the performance is ~~~~ Let's talk about it later ~ Keke ~~

P.s. 1 mongodtype is originally protect virtual in my childentity, because I do not want external users to directly operate on this char field, but want to access it through an enumeration, but now I have to make it public. Solution? One is to consider. in the net world, the impression provides a friendsassembly mechanism that allows my Dao's assembly to access the internal field of the class in modelassembly, this solution can be used (I want to talk about myself, and all of the above are considered under a normal, well-layered architecture, at the customer's requirement, Dao (including interface and impl) and model are defined in the same Assembly called dataaccess. only protect internal is required, NH )... Many customers require this kind of benefits, orz ). Another better direction is to directly use the enumstringtype provided by NH. As we can see today, we haven't tried it yet.

P.s. 2 when I first used linqtonhibernate, I found that nhib.pdf. LINQ. dll was not named as a strong name.

Google, find the signer, and create a new Named DLL.

 

Finally, I spoke about nhib.pdf. The instruction document on the official website is too old! Why not mention the features of NH 2.1 ~~~

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.