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