-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Identifier type mismatch
PostPosted: Thu Jul 09, 2009 7:04 am 
Newbie

Joined: Tue Dec 02, 2008 8:32 am
Posts: 2
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.xml
Code:
<?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.cs
For 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.

_________________
To error is only human, but to really screw things up, you'll need a computer.


Top
 Profile  
 
 Post subject: Re: Identifier type mismatch
PostPosted: Sun Jul 12, 2009 7:48 am 
Newbie

Joined: Tue Dec 02, 2008 8:32 am
Posts: 2
Ok, on further inspection. I think i have narrowed down the issue.

When i do
Code:
session.Save(advert);
At this time, the advert's id property is empty, as it is left up to NHibernate to decide which unique id to assign to it.

However, when i do
Code:
object entity = session.Get(typeof(T), reference);
where 'reference' is the advert object i used to save to the db in the first place. This object in code STILL does not have an id property. However, the entity that NH will return from the DB WILL have an id value as it was auto-assigned during the .commit() earlier.

Thus, the id's values of both the c# object and the object returned from the db match in every sense apart from the id values....thus "identifier type mismatch".

At least, that is what i THINK is going on! So the question no of course is, is there any way of injecting the id value that NH will assign when it saves to the db, into the c# object so that when i ask NH to find the db equivalent it knows what id to look for ?

SOMEONE must know SOMETHING about this bug !

_________________
To error is only human, but to really screw things up, you'll need a computer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.