Nhibernate throws error No persister for: - nhibernate

Mapping file:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="PlayHibernate.Employee, PlayHibernate" lazy="false">
<id name="id" access="field">
<generator class="native"/>
</id>
<property name="name" access="field" column="name"/>
<many-to-one access="field" name="manager" column="manager" cascade="all"/>
</class>
</hibernate-mapping>
I tried setting the mapping file as a resource, but that just changes the error message to
"Unhandled Exception: NHibernate.MappingException: PlayHibernate.Employee.hbm.xml
(5,31): XML validation error: The element 'id' in namespace 'urn:nhibernate-mapp
ing-2.2' cannot contain text. ---> System.Xml.Schema.XmlSchemaValidationExceptio
n: The element 'id' in namespace 'urn:nhibernate-mapping-2.2' cannot contain tex
t."

The first error "No persister for..." was a consequence of not setting mapping file as an embedded resource.
The current error has something to do with your id definition. It looks like you have some text within your <id> definition. Maybe some unallowed whitespaces? Try to write it as:
<id name="id" access="field" column="uid" generator="native" />
Also if that doesn't help, try to use some other name than id for a field name. Maybe Id.

In visual studio remember change the property: "Compilation action" of your .hbm.xml file to : "Embedded resource"

Related

NHibernate one-to-one relationship to a mapped view

I have a mapped view, which I would like to reference in another mapping.
Mapped View:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.TagBE, Model" table="vw_CurrentTag" lazy="true" schema="dbo">
<id name="Id" column="TAG_HISTORY_ID" type="Guid"
unsaved-value="00000000-0000-0000-0000-000000000000">
</id>
<property name="Tag" column="TAG"/>
<property name="TagStatus" column="TAG_STATUS"/>
<property name="Created" column="CREATE_DATE"/>
<many-to-one name="Defect" class="Model.DefectBE, Model" fetch="join"
not-found="ignore" cascade="none" column="DEFECT_ID" />
<many-to-one name="CreatedBy" class="Model.UserBE, Model" lazy="false"
fetch="join" cascade="none" column="CREATE_USER_ID" />
</class>
</hibernate-mapping>
I would like to use it in this mapping, to get a single TagBE:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DefectBE, Model" lazy="true" table="DEFECT" schema="dbo">
<id name="Id" column="DEFECT_ID" type="Guid">
<generator class="assigned" />
</id>
<property name="Sn" column="SERIAL_NUMBER" />
<property name="Description" column="PART_DESCRIPTION" />
<many-to-one name="System" class="Model.SystemBE, Model" fetch="join"
cascade="none" column="SYSTEM_ID" not-found="ignore"/
</class>
</hibernate-mapping>
The above mappings have been trimmed down for the sake of an example (removed a bunch of properties and and man-to-one relationships not related to the problem at hand.
The many-to-one shown with the name "System" works as expected, and it is just another mapped table, not a view. My problem is that when I try to map the view "vw_CurrentTag", I can only access the data when I specifically use ISession and ICriteria to get specificaly something of the TagBE class.
When I try to use the following in the DefectBE mapping, it just gives gives null for the TagBE:
<many-to-one name="Tag" class="Model.TagBE, Model" fetch="join" cascade="none"
column="DEFECT_ID" not-found="ignore"/>
I have also tried using one-to-one, but i always get null when the TagBE object part of a DefectBE object.
If it's not possible I can always use a Bag with only 1 entry.
The view is just a query on a single table to return unique Tag column based on other columns.
the many-to-one "Tag" uses the column DEFECT_ID on the table DEFECT to match the primary key of the view TAG_HISTORY_ID which is never equal.
What you probably want is a one-to-one mapping on the Defect class which points to the DEFECT_ID on the view.
<one-to-one name="Tag" class="Model.TagBE, Model" fetch="join" property-ref="Defect" foreign-key="none"/>

NHibernate Mapping Exception: invalid child element

I'm having a problem with NHibernate and the mapping file when I try to save my drink object in my MVC application. My mapping file is an embedded resource and my hibernate.cfg.xml is copy always.
Here are my class.cs:
namespace FrancosPoS.DBMapping {
public class drink {
public drink() { }
public virtual int id { get; set; }
public virtual string type { get; set; }
public virtual string price { get; set; }
}
}
My XML mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS.DBMapping" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="drink" table="drink" lazy="true" >
<id name="id">
<generator class="identity" />
<column name="id" sql-type="int(11)" not-null="true" />
</id>
<property name="type">
<column name="type" sql-type="varchar(25)" not-null="true" />
</property>
<property name="price">
<column name="price" sql-type="varchar(8)" not-null="true" />
</property>
</class>
</hibernate-mapping>
By the way, the connection opens and close fine if I don't try to save it on the database.
Here is my Solution Explorer:
Solution Explorer Image
And here is the error that is driving me nuts:
"Error: NHibernate.MappingException: FrancosPoS.DBMapping.drink.hbm.xml(6,8): XML validation error: The element 'id' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'column' in namespace 'urn:nhibernate-mapping-2.2'. ---> System.Xml.Schema.XmlSchemaValidationException: The element 'id' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'column' in namespace 'urn:nhibernate-mapping-2.2'.\r\n --- End of inner exception stack trace ---
the problem is in node id, it does not have a child, remove that node and it will simply be like this:
<id name="id">
<generator class="identity" />
</id>
the column's name is specified in id node, and you dont have to tell column has "not-null" constraint since id columns are always required
Also, in this line of your xml mapping:
<hibernate-mapping assembly="FrancosPoS.DBMapping" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
"assembly" attribute seems to be wrong, it should be the name of your assembly (which i'm almost sure is "FrancosPos").

Hibernate collection loaded by custom SQL

I have some problems trying to get a bag collection of a class to be loaded through custom sql.
Here's the xml mappings I have for my class
<hibernate-mapping>
<class name="alekso.npe.model.Utente" table="VNPEZZ_UTE_MAT" lazy="false">
<id name="matricola" column="C_UTE_MAT">
</id>
<property name="nome" column="T_NOM" />
<property name="cognome" column="T_COG" />
<property name="email" column="T_EML" />
<bag name="ruoli" table="VNPEZX_UTE_APP_TRN"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key column="C_UTE_MAT" />
<one-to-many class="alekso.npe.model.Ruolo" />
<loader query-ref="rolesQuery"/>
</bag>
</class>
<class name="alekso.npe.model.Ruolo" table="VNPEZH_TIP_USR"
lazy="false" where="C_APP = 'NPE'">
<id name="codice" column="C_TIP_USR">
</id>
<property name="nome" column="T_DES_TIP_USR"/>
</class>
<sql-query name="rolesQuery">
<return alias="role" class="alekso.npe.model.Ruolo"></return>
<load-collection alias="ruoli" role="alekso.npe.model.Ruolo" ></load-collection>
<![CDATA[select {ruolo.*}
from NPEA.vnpezx_ute_app_trn permesso join NPEA.vnpezh_tip_usr ruolo
on ruolo.c_tip_usr = permesso.c_tip_usr
where permesso.c_app = 'NPE'
and permesso.c_ute_mat = :matricola ]]>
</sql-query>
</hibernate-mapping>
But when I run the application I get an error:
org.hibernate.HibernateException: Errors in named queries: rolesQuery in the buildSessionFactory phase.
Can you tell me what's wrong with this mapping?
I tried both with and without the <return> tag inside the <sql-query> but it still don't work
I found out the error... the line <load-collection alias="ruoli" role="alekso.npe.model.Ruolo" ></load-collection> inside the custom query definition gives the error.
Without that line, everything works fine (almost... the collection of roles is lazy loaded even if I set lazy="false" everywhere).
But that line I removed is on the documentation, so I'd like to know why it's wrong.
I'll leave the question open for other to give some hints on this.

NHibernate One-To-One Relationships not saving correctly

I am having an issue with saving entities with one-to-one relationships. I just want to save the parent entity and have the child be save aswell but I am having to save both separately.
This is an example of what I am having to do, otherwise the child is not saved.
var session = SessionProvider.OpenSession.Session;
using (var tx = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(parent);
if (parent.Child.IsPersisted)
{
session.Update(parent.Child);
}
else
{
session.Save(parent.Child);
}
}
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" assembly="xxx">
<class name="Parent" polymorphism="explicit" table="Parent">
<id name="Id" column="JointID" type="int">
<generator class="native" />
</id>
<one-to-one name="Child" class="Child" />
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"
assembly="xxx">
<class name="Child" xmlns="urn:nhibernate-mapping-2.2" polymorphism="explicit" table="Child">
<id name="Id" column="JointID" type="int" unsaved-value="0">
<generator class="native" />
</id>
<many-to-one name="Parent" column="JointID" insert="false" update="false" />
</class>
</hibernate-mapping>
Any ideas on how I can just make it save without having to do two save calls?
When I set the relationship to cascade as suggested below I get foreign key constraint errors. If I analyse the queries with NHProf, its trying to use the temporary id (-1) as the JointId in the insert statement rather than the newly created parent id. The JointId in the Parent table is an Identity key, perhaps that is an issue?
You'll need to enable cascading on your <one-to-one> mapping to have this work properly.
Something like:
<one-to-one name="Child" class="Child" cascade="save-update" />
You can read up on the various cascade settings here.

NHibernate, one-to-one mapping, cascade insert

I have a one-to-one relationship between a Company class and a CompanySettings class. When I create a new Company object, (a CompanySettings object is created in Company's constructor for its Settings property), and then
SaveOrUpdate(session, companyObject)
I expect the INSERT to cascade from the Company to the CompanySettings. However, this does not happen unless I explicitly call SaveOrUpdate on the CompanySettings object as well.
Mapping files shown below:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"`>
<class name="AST.Domain.Company, AST.Domain" table="Companies">
<id name="EntityID" column="CompanyId">
<generator class="guid.comb" />
</id>
<property name="CompanyName" />
. . .
<one-to-one name="Settings" class="AST.Domain.CompanySettings, AST.Domain"
constrained="true" lazy="false" />
</class>
</hibernate-mapping>
My mapping file for the Company Settings class:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="AST.Domain.CompanySettings, AST.Domain" table="CompanySettings">
<id name="EntityID" column="CompanyId">
<generator class="foreign">
<param name="property">Company</param>
</generator>
</id>
<property name="MaxUsers" />
<one-to-one name="Company" class="AST.Domain.Company, AST.Domain" />
</class>
</hibernate-mapping>
Have you tried specifying cascade="all" on your one-to-one mapping?
Set the cascade attribute of the one-to-one in the Company mapping.
But, on another note:
Have you thought of mapping the CompanySettings as a 'component' of Company instead of a separate entity ?
Isn't it so that 'CompanySettings' is a 'value object', and should be mapped better as a component ?
By doing this, you can put the CompanySettings values in the same table as 'Company', but it will be treated as a separate class.
Since this is a one-to-one mapping, I think it is a better option for your data model as well.
Then, your mapping would look something like this:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"`>
<class name="AST.Domain.Company, AST.Domain" table="Companies">
<id name="EntityID" column="CompanyId">
<generator class="guid.comb" />
</id>
<property name="CompanyName" />
. . .
<component name="Settings" class="AST.Domain.CompanySettings, AST.Domain">
<property name="MaxUsers" />
</component>
</class>
</hibernate-mapping>
You will have indeed 2 separate objects (Company & CompanySettings, and Company will have a 'Companysettings' object, but the settings will be saved in the Company table).
Or, is there any special reason on why you've put the CompanySettings in a separate table ? I mean, it is a one-to-one relation so this is completely not necessary (and imho even a bad practice :) ).