Solution to Nhibernate unknown entity class

Source: Internet
Author: User

Overview:

When session. Save (TNP); is run, this "unknown entity class: testcleansnow. testnhibernateperson" exception occurs.

The testing file testnhibernateperson. HBM. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Hibernate-Mapping Xmlns = "Urn: nhibernate-mapping-2.2" Assembly = "Testcleansnow" Namespace = "Testcleansnow" >
< Class Name = "Testcleansnow. testnhibernateperson, testcleansnow" Table = "Test_nhibernate_person" Lazy = "False" >
< ID Name = "Usertestid" Column = "Usertestid" Type = "Decimal" >
< Generator Class = "Sequence" >
< Param Name = "Sequence" > Emp_sequence </ Param >
</ Generator >
</ ID >
< Property Type = "String" Not-Null = "True" Length = "6" Name = "Usertestname" Column = "Usertestname"   />
</ Class >
</ Hibernate-Mapping >

 

OperationCodeAs follows:

Configuration config =   New Configuration ();
Isessionfactory Factory = Config. buildsessionfactory ();
Isession session = Factory. opensession ();

Testnhibernateperson TNP =   New Testnhibernateperson ();
TNP. usertestname =   " Test4 " ;
Itransaction Trans = Session. begintransaction ();
Try
{

// Save record
Session. Save (TNP );
Trans. Commit ();
Console. writeline ( " Insert success! " );


}
Catch(Exception ex)
{
Trans. rollback ();
Console. writeline (ex. Message );
}

Finally
{
Session. Close ();
}

The configuration code app. config is as follows:

 

< Configuration >
<! -- Add this element -->
< Configsections >
< Section Name = "Hibernate-configuration" Type = "Nhibernate. cfg. configurationsectionhandler, nhibler"   />
<! -- <Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/> -->
</ Configsections >
<! -- Add this element -->
< Hibernate-Configuration Xmlns = "Urn: nhibernate-configuration-2.2" >

< Session-factory >
< Property Name = "Dialect" > Nhib.pdf. dialect. oracledialect </ Property >
< Property Name = "Connection. provider" > Nhib.pdf. Connection. driverconnectionprovider </ Property >
< Property Name = "Connection. connection_string" > User ID = jkpt; Data Source = jkorasvr; Password = designer; </ Property >
< Property Name = "Connection. Isolation"   > Readcommitted </ Property >
< Property Name = "Show_ SQL" > True </ Property >
<! -- Mapping Files -->
< Mapping Assembly = "Testcleansnow"   />
</ Session-factory >
</ Hibernate-Configuration >

 

Note: After loading the referenced config. addassembly ("testcleansnow") in the operation code, no error will occur.

If a reference is loaded in the form of a configuration file, the following error occurs.

 

Cause analysis:

Question 1:

It may be that the corresponding ing file is not set to "embedded resource". Here, the corresponding ing file is testnhibernateperson. HBM. xml.

Find testnhibernateperson. HBM. XML in Solution Explorer.

Right-click Properties-set "generate operation" to "embedded resource ".

 

Question 2:

I changed configuration Config = new configuration (); To configuration Config = new configuration (). Configure ();

LetProgramGo to hibernate. cfg. xml. The configuration file is the same as in APP. config.

 

Question 3:
Check whether the configuration of the testing file testnhibernateperson. HBM. XML is correct.

< Hibernate-Mapping Xmlns = "Urn: nhibernate-mapping-2.2" Assembly = "Module name" Namespace = "Namespace" >
< Class Name = "Class name" Table = "Database table name" Lazy = "False" >
< ID Name = "Usertestid" Column = "Usertestid" Type = "Decimal" > // Primary key

 <GeneratorClass= "Sequence"> // Primary Key Generation Method

 

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.