Hey guys, a quick question.
I am new to NHibernate and have just got my first application functioning correctly. Well, correctly apart from this little issue. Before explaining the issue, ill dump the code in here so that we are on the same page when i start asking questions.
So;
Mapping file - Advertisement.hbm.xmlCode:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="HiFi.Model.DAO"
assembly="HiFi.Model">
<!-- Mappings for class 'Advertisement' -->
<class name="Advertisement" table="Advertisement" lazy="false">
<!-- Identity mapping -->
<id name="ID">
<column name="advert_id" />
<generator class="native" />
</id>
<!-- Simple mappings -->
<property name="UserID" not-null="true" />
<property name="CatagoryID" not-null="true" />
<property name="Title" not-null="true" />
<property name="Body" not-null="true"/>
<property name="Image" />
<property name="Type" not-null="true" />
<property name="VendorType" not-null="true" />
<property name="Address" not-null="true" />
<property name="ContactNumber" not-null="true" />
<property name="PostedOn" not-null="true" />
<property name="ExpiresOn" not-null="true" />
<property name="Price" not-null="true" />
<property name="Status" not-null="true" />
</class>
</hibernate-mapping>
Advertisement.csFor the sake of you not having to trawl through a tonne of pointless code, i have removed the other properties from this class. I can't see how other properties would cause my issue but if there is a possibility, ill just re-post the entire class.
Code:
public class Advertisement
{
/* PROPERTIES */
int id;
public virtual int ID
{
get { return id; }
set { id = value; }
}
/* CONSTRUCTORS */
public Advertisement()
{
}
Now as i am new to the whole NHibernate scene, i don't know exactly how much of my code you need to fully understand what's going on.
In my sql table 'Advertisement', the id column is stored as sqltype = int.So i go on my merry way, writing tests to make sure it all works properly and all tests that involve creating an Advertisement class and persisting it in the db work fine. The test methods can even retrieve items back from the db for comparison with no errors.
The problems began when i wrote a test to get an Advert from the db not by id, but by just by passing in an Advertisement type to my get method and asking NH to retrieve the data in the db that exactly matches this object.
My AdvertisementRepository class inherits from a BaseRepository class and it is here where the .Get<T>(object reference) method is. I will post it below.
So you type;
Code:
AdvertisementRepository myRepository = new AdvertisementRepository();
then create an advertisement object;
Code:
Advertisement advert = new Advertisement();
advert.UserID = 32;
advert.CatagoryID = 115;
advert.Type = AdvertisementType.Wanted;
advert.VendorType = VendorType.Private;
advert.Title = "NHibernate Title";
advert.Body = "How long is it 'gonna take to get this working?";
advert.Price = 1500.00f;
advert.Address = "415a Testingyou Road";
advert.ContactNumber = "774";
advert.PostedOn = DateTime.Now;
advert.ExpiresOn = DateTime.Now.Add(TimeSpan.FromDays(7.00));
advert.Status = false;
and save it to the db;
Code:
myRepository.Add(advert);
which calls -->
public void Add(Advertisement advert)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(advert);
transaction.Commit();
}
}
All this works fine. But then, when it comes to getting that same advertisement back out of the db using;
Code:
myRepository.Get(advert);
which calls
Code:
public Advertisement Get(Advertisement advert)
{
return (Advertisement)Get<Advertisement>(advert);
}
protected override object Get<T>(T reference) <-- Inherited from its BaseRepository parent
{
return base.Get<T>(reference);
}
Which calls
Code:
protected virtual object Get<T>(T reference)
{
using (ISession session = NHibernateHelper.OpenSession())
{
object entity = session.Get(typeof(T), reference); <--- THE COMPILER STOPS ON THIS LINE !!!
return entity;
}
}
It all breaks and the compiler tells me "identifier type mismatch. Parameter name: id".
Now i did some searching and found
https://forum.hibernate.org/viewtopic.php?f=25&t=952356&view=previous which is a guy that seems to have a similar problem. Yes, it's in the Java section but still.
So i tried modifying my mapping document to;
Code:
<!-- Identity mapping -->
<id name="ID" type="Integer">
<column name="advert_id" />
<generator class="native" />
</id>
This only served to give me a "Mapping file could not be compiled" error or just the same identity type mismatch error. I also tried defining the sqltype like so;
Code:
<!-- Identity mapping -->
<id name="ID">
<column name="advert_id" sqltype="int" />
<generator class="native" />
</id>
Which didnt work. Yes, i did try both sqltype="int" AND sqltype="Integer" i just didnt post both versions up. So now im a bit stuck as to what to try next.
The only other thing that i can think of is that when NH pulls the data from the db, is trying to persist it into an object and not an Advertisement (which it knows it is looking for). By default objects dont have an "id" property and so NH is complaining that it cant find a place to put the "id" data. If this is the case, id still really appreciate the help as i do not know how to get round it and im getting to the point where i swear NH has a personal vendetta against me!. Im casting the object back up to an Advertisement before its reaching the test method but that doesn't seem to work.
Thank you for your time, any help greatly appreciated.