I'm just trying to start learning NHibernate, and while testing with an extremely simple POCO I'm already having issues. I'm getting the No Persister exception, here is what my code looks like:
Account
table:
create table Account
(
AccountID int primary key identity(1,1),
AccountName varchar(10),
CreateDate datetime
)
go
Account
class:
public class Account
{
public virtual int AccountID { get; set; }
public virtual string AccountName { get; set; }
public virtual DateTime CreateDate { get; set; }
public Account()
{
AccountID = 0;
AccountName = "";
CreateDate = new DateTime();
}
}
Mapping file, Account.hbm.xml
(yes, it's embedded into assembly):
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernateTesting" assembly="NHibernateTesting">
<class name="Account" table="Account">
<id name="AccountID">
<generator class="native"/>
</id>
<property name="AccountName" />
<property name="CreateDate" />
</class>
</hibernate-mapping>
Config section in config file:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">My connection string</property>
<mapping assembly="NHibernateTesting" />
</session-factory>
</hibernate-configuration>
Finally, the code making the call:
using (var session = NHibernateHelper.GetASession())
{
using (var tran = session.BeginTransaction())
{
var newAccount = new Account();
newAccount.AccountName = "some name";
session.Update(newAccount); // Exception thrown here.
tran.Commit();
}
}
Can anyone see what I'm doing wrong or why I would be getting this exception, I've read on the web the issue is related to mapping, but I just can't see what wrong in this example.
I've reproduced all the mapping and code as you've shown, and it is working. Except:
var newAccount = new Account(); // NEW
...
session.Update(newAccount); // Update throws exception:
NHibernate.StaleStateException: Batch update returned unexpected row count from update; actual row count: 0; expected: 1
New object must be saved via: session.Save(newAccount);
When you say, that the mapping file is marked as embedded resource... it is hard to say what exactly is wrong. Please, try to carefully follow this link (and re-check your project), with a pretty good experience chain about the No persister excepiton:
This error occurs because of invalid mapping configuration. You should check where you set .Mappings for your session factory. Basically search for ".Mappings(" in your project and make sure you specified correct entity class in below line.
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<YourEntityClassName>())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With