-->
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.  [ 9 posts ] 
Author Message
 Post subject: Reading database value of id in Get, Load, etc.
PostPosted: Wed Jan 17, 2007 7:56 am 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
Question:
I'm pretty new to (N)Hibernate and I'm trying to persist parts of an existing database. The database is in use, and I have no way to change the schema.

My problem is that for several tables I need to read the value of the primary key in the database, and act on it. But when I do a Get, Load, Find, etc. the id is not filled with the value from the database, but rather the UnsavedValue.

So, the question is, how do I get the value of the id in the persisted class?

Following is just one such example, there are many and more complex uses. In the example I only read the healthcare objects, never save or update them.

Hibernate version:
1.2.0.Beta2

Mapping documents:
Code:
    [Serializable]
    [HibernateMapping(DefaultLazy = true)]
    [Class(Table = "Healthcare", NameType = typeof(Healthcare))]
    public class Healthcare
    {
        private int mHealthcareId;
        private string mDescription = null;
        [...]
       
        [Id(1, Column = "HealthcareId", TypeType = typeof(Int32), UnsavedValue = "0")]
        [Generator(2, Class = "assigned")]
        public virtual int HealthcareId
        {
            get { return mHealthcareId; }
            private set { mHealthcareId = value; }
        }
       
        [Property(1, Column = "Description", NotNull = false, Length = 255)]
        [Type(2, NameType = typeof(string))]
        public virtual string Description
        {
            get { return mDescription; }
            set { mDescription = value; }
        }

        [...]
    }


Code between sessionFactory.openSession() and session.close():
Code:
IList<Healthcare> healthcares = session.CreateCriteria(typeof(Healthcare)).List<Healthcare>();
foreach (Healthcare healthcare in healthcares)
{
    ydelseskoder.Add(healthcare.HealthcareId.ToString(), healthcare.Description);
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 6:35 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
What database are you using?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 6:41 am 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
Mostly MS SQL Server 2000, but also MS SQL Server 2005.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 6:50 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
Have you tried changing the Generator value to native eg.

[Generator(2, Class = "native")]

I'm not sure if this is the problem but this is what I used when I mapped an existing SQL Server database and it worked fine.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 7:29 am 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
hmm, no luck. I tried changing the generator to native, but got the same result, the id values aren't read.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 7:44 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
I have never done my mapping using attributes I have always used seperate XML files but I'm am wondering if the problem could be the positioning you have in the attributes. You have the positioning as 1, 2 and I think it should start from 0 e.g.

Code:
        [Id(0, Column = "HealthcareId", TypeType = typeof(Int32), UnsavedValue = "0")]
        [Generator(1, Class = "assigned")]
        public virtual int HealthcareId
        {
            get { return mHealthcareId; }
            private set { mHealthcareId = value; }
        }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 8:40 am 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
Double post...


Last edited by Longbow on Thu Jan 18, 2007 8:42 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 8:41 am 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
Changing the position (1->0, 2->1) doesn't change anything either. Here is the mapping in XML format:

Code:
<?xml version="1.0" encoding="utf-8"?>
<!--Generated by NHibernate.Mapping.Attributes on 2007-01-18 13:34:01Z.-->
<hibernate-mapping namespace="UnifiedBatchFramework.Schema.TMT150" assembly="UnifiedBatchFramework.Schema.TMT150" default-lazy="true" xmlns="urn:nhibernate-mapping-2.2">
  <class name="UnifiedBatchFramework.Schema.TMT150.Healthcare, UnifiedBatchFramework.Schema.TMT150" table="Healthcare">
    <id column="HealthcareId" type="Int32" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Description" column="Description" length="255" not-null="false">
      <type name="String" />
    </property>
    [...]
  </class>
</hibernate-mapping>


If I save objects to the database the data is correct in the table, so it's only the select part that gives problems.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 8:54 am 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
Looking at the XML i suddenly realise that NHibernate.Mapping.Attributes doesn't generate a name attribute on the id. If I change my mapping to

Code:
        [Id(0, Name = "HealthcareId", Column = "HealthcareId", TypeType = typeof(Int32), UnsavedValue = "0")]
        [Generator(1, Class = "native")]
        public virtual int HealthcareId
        {
            get { return mHealthcareId; }
            private set { mHealthcareId = value; }
        }


that is, explicitly add a Name attribute, it works! I get the value of the id in my object.

I would consider this a bug: The Property attribute automatically extracts the name of the property, but id doesn't.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.