I’m facing a apparently very strange problem (I must be doing something wrong, just can’t find my errors!). When certain POCOs are saved into the database, nothing happens. When the same POCOs have some property changed, I get a InvalidCastException during session flush, and the rows are never updated. Here’s the details:
I have the following class declared:
namespace Data
{
public class Picture
{
public virtual int picid { get; set; }
public virtual int width { get; set; }
public virtual int height { get; set; }
public virtual string path { get; set; }
public virtual string thumbnail { get; set; }
public virtual int userid { get; set; }
public virtual int? placeid { get; set; }
public virtual int? eventid { get; set; }
public virtual DateTime? approved { get; set; }
public virtual DateTime date { get; set; }
public virtual bool finished { get; set; }
public virtual User User { get; set; }
public virtual Place Place { get; set; }
public virtual Event Event { get; set; }
public virtual ISet<PictureVote> Votes { get; set; }
}
}
and the following mapping for it:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Data" namespace="Data">
<class name="Picture" table="pictures">
<id name="picid">
<generator class="sequence">
<param name="sequence">pictures_picid_seq</param>
</generator>
</id>
<property name="path" />
<property name="width" />
<property name="height" />
<property name="thumbnail" />
<property name="userid" />
<property name="placeid" not-null="false" />
<property name="eventid" not-null="false" />
<property name="approved" />
<property name="date" />
<property name="finished" />
<many-to-one name="User" column="userid" class="Data.User,Data" insert="false" />
<many-to-one name="Place" column="placeid" class="Data.Place,Data" insert="false" />
<many-to-one name="Event" column="eventid" class="Data.Event,Data" insert="false" />
<set name="Votes">
<key column="picid" />
<one-to-many class="PictureVote" />
</set>
</class>
</hibernate-mapping>
I checked the table definition and all the types seem to be according to the defined class (postgresql):
Table "public.pictures"
Column | Type | Modifiers
-----------+-----------------------------+----------------------------------------------------------
picid | integer | not null default nextval('pictures_picid_seq'::regclass)
path | character varying(250) | not null
thumbnail | character varying(250) | not null
userid | integer | not null
placeid | integer |
date | timestamp without time zone | not null
finished | boolean | not null default false
width | integer | not null
height | integer | not null
eventid | integer |
approved | timestamp without time zone |
When inside the code, the following works just fine and inserts a row in the pictures table:
…
…
var plpic = new Picture
{
date = DateTime.Now,
width = img.Width,
height = img.Height,
path = pic_server_path,
thumbnail = thumb_server_path,
userid = CustomUserManagement.User.userid,
finished = false,
approved = null,
placeid = placeid
};
session.Save(plpic);
session.Flush ();
…
…
which works ok every time (and yes, I’m going to wrap it in a transaction soon enough).
However, later on, the following NEVER works (this is the code inside a MVC action):
…
…
Picture pic = session.Get<Picture>(picid);
// While debugging, I verified that the "pic" object above is retrieved just fine.
if (pic.userid != CustomUserManagement.User.userid || ModelState.IsValid == false)
return Json (new { status = "error" });
using (ITransaction tx = session.BeginTransaction()) {
try
{
pic.finished = true;
tx.Commit();
}
catch (Exception e) {
tx.Rollback();
NHibernateHelper.DestroySession();
return Json (new { status = "error" });
}
}
…
…
But this always throws a System.InvalidCastException: Cannot cast from source type to destination type at NpgsqlTypes.NpgsqlTypesHelper+c_Iterator11.<>m_C (System.Object timestamp) [0x00000] in /Users/fxjr/Desenvolvimento/ProjetosOpenSource/Npgsql/NpgsqlSourceRelease/Npgsql2/src/NpgsqlTypes/NpgsqlTypesHelper.cs:608
What am I doing wrong? I’m using .NET 4 in Mono 2.10.5, even though the same happens on Windows, NHibernate 3.2 and Npgsql 2.0.11.91. I’m also using postgresql 9.1.1, but I have set ansi_conforming_strings to OFF to make sure my Nhibernate dialect still works. For more information, the same thing happens when updating other types of objects.
I have already posted this in the nhusers list and received a very good suggestion on trying a different db to check if it its Npgsql’s fault. However, I don’t have the time for that right now and since I’m beginning to think it is my fault and not someone else’s, I thought I’d post this here before recreating my db schema in another database and trying this code in it. I’m getting a little desperate as time is kind of running out on me.. can anyone save me on this one?
Thanks in advance.
After a long time, I thought I should come back here to answer my own question.
This is not necessarily a bug in either NHibernate or Npgsql.
I was making a huge mistake in the mapping files, by mapping the same column twice. What this means is that having a
int? eventidand aEvent Eventin my mapped class both map to same column will create you problems, since NHibernate can’t deal with this, i.e. you have to pick one of them. What happens is that NHibernate will generate queries with more parameters than the actual number of columns in your table. In MS SQL Server, it is easier to understand the thrown exception:Another alternative is mapping both but setting insert=”false” and update=”false” in one of them, although this is hardly useful.
I think a lot of people like me (coming from LINQ-TO-SQL) will try the same feat and have a lot of trouble later on. Sadly, I haven’t found anything that emphasizes this in NHibernate’s documentation.
Hope it helps someone.