Implement subclass in inheritance- ing

Source: Internet
Author: User
What is subclass? For example, users have general users and management users. They are all recorded in the User table and identified by a field as a general user or a management user. Let's continue with this example.

You can use a table named users to save the information of common users and administrators. The field identified is a metadata type named userkind. If the value is user, the field is admin and administrator.

Code

/** // <Summary>
/// User
/// </Summary>
Public class users
{
Public int ID;
Public string name;
/** // <Summary>
/// Constructor
/// </Summary>
Public users ()
{
}
}

/** // <Summary>
/// Average user
/// </Summary>
Public class user: Users
{
Public user ()
{
//
// Todo: add the constructor logic here
//
}
}

/** // <Summary>
/// Administrator.
/// </Summary>
Public class admin: Users
{
Public Admin ()
{
//
// Todo: add the constructor logic here
//
}
}

 Ing FileWriting is actually very simple.
Users. HBM. xml

<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.0">
<Class name = "myentity. Users, myentity" table = "users">
<ID name = "ID" type = "int32">
<Generator class = "native"/>
</ID>
<Discriminator column = "userkind" type = "string"/>
<Property name = "name" type = "string" not-null = "true" length = "50"/>

<Subclass name = "myentity. Admin, myentity" discriminator-value = "user">
</Subclass>
<Subclass name = "myentity. Admin, myentity" discriminator-value = "admin">
</Subclass>
</Class>
</Hibernate-mapping>

 

Note that discriminator and discriminator-value.

Output Data Structure

Configuration CFG = new configuration ();
Cfg. addclass (typeof (users ));
New schemaexport (CFG). Create (True, true );

User and Admin do not need to be added to configuration. Adding a user and Admin will only cause errors.

Persistent Object
Configuration CFG = new configuration ();
Cfg. addclass (typeof (users ));
_ Sessions = cfg. buildsessionfactory ();
User USR = new user ();
USR. Name = "normal user ";
Admin ad = new admin ();
Ad. Name = "Administrator ";
Isession session = This. _ sessions. opensession ();
Session. Save (usr );
Session. Save (AD );

Execution result

Nhibes: insert into users (name, userkind) values (@ P0, 'user'); select scope_identity ()
@ P0 = 'average user'
Nhibes: insert into users (name, userkind) values (@ P0, 'admin'); select scope_identity ()
@ P0 = 'postmaster'

Search

Isession session = This. _ sessions. opensession ();
Int IID = 1;
Int iid2 = 2;
Object OBJ = session. Load (typeof (users), IID );
Object obj2 = session. Load (typeof (users), iid2 );

String str1 = "user ";
String str2 = "user ";
If (obj is admin)
Str1 = "admin ";
If (obj2 is admin)
Str2 = "admin ";
Console. writeline ("obj1 is" + str1 );
Console. writeline ("obj2 is" + str2 );

Result
Obj1 is user
Obj2 is admin.

Directly query general user isession session = This. _ sessions. opensession ();
Int IID = 1;
User USR = (User) Session. Load (typeof (user), IID );
Session. Close ();

Query Process

Nhib.pdf: Select user0 _. ID as id0 _, user0 _. nickname as nickname0 _, user0 _. name as name0 _ from users user0 _ Where user0 _. id = @ P0 and user0 _. userkind = 'user'
@ P0 = '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.