-->
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.  [ 4 posts ] 
Author Message
 Post subject: Composite-ID mapping
PostPosted: Mon Apr 16, 2007 3:35 pm 
Newbie

Joined: Mon Apr 16, 2007 3:20 pm
Posts: 7
Hi, New to nhibernate. I've been looking at examples all day and trying to get my composite id correct, but I am really having trouble. I hope you can help. Here is my mapping. The table has 4 fields that make up the composite key, and one additional 5th field. Only 3 of the 4 fields are related to other tables, and "Date_Used" is the one that has no relation. It's just a date.
This is the closest I can get it to what i think is right:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="EmployeeTimeTracker.EmpTime, EmployeeTimeTracker" table="dbo.EMPLOYEE_TIME_USED">
      <composite-id name="EmpTimeKey" class="EmpTimeKey">
      
          <key-many-to-one name="EmpID" class="Employee" column="EMPLOYEEID" insert="false" update="false" />
                   
         <key-many-to-one name="TimeType" class="TimeType" column="TIME_ID" insert="false" update="false" />
                  
         <key-many-to-one name="AttendReason" class="AttendReason" columne="REASON_ID" insert="false" update="false" />
                  
         <key-property name="DateUsed" column="DATE_USED" />    
                            
      </composite-id>
            
      <property name="HoursUsed" column="HOURS_USED" />            
                  
   </class>
</hibernate-mapping>


The error i am getting is as follows, during runtime when NHibernate is reading the mapping files:

[error]
"The element 'urn:nhibernate-mapping-2.2:composite-id' cannot contain text. Expected 'urn:nhibernate-mapping-2.2:key-property urn:nhibernate-mapping-2.2:key-many-to-one'. An error occurred at , (4, 54).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.Schema.XmlSchemaException: The element 'urn:nhibernate-mapping-2.2:composite-id' cannot contain text. Expected 'urn:nhibernate-mapping-2.2:key-property urn:nhibernate-mapping-2.2:key-many-to-one'. An error occurred at , (4, 54)."
[/error]

Is it just my syntax? What does "error occurred at (4, 54)" mean?
Must the mapping files for the classes/components related to these "many-to-one" fields, also have coordinating "one-to-many" tags?

I know this is my problem, bcz i already had everything working, ie, using nh to pull back data. It was when I went to do my update routine, that I realized i needed this composite key, so I could query this table for one, precise record. I want it this way so that I can use HQL to query for the record, update the live object and flush it. Thanks for any help. Although it's a bit agonizing getting started, this is really awesome and powerful technology,and I can't wait to get it fully functional.


Top
 Profile  
 
 Post subject: Here is my class too
PostPosted: Mon Apr 16, 2007 4:37 pm 
Newbie

Joined: Mon Apr 16, 2007 3:20 pm
Posts: 7
Sorry about that, i should've probably included my class file too. I read something the class must be serializable, and i must override the equals() method and the gethashcode() method. I've done that. Not certaint hat the syntax is correct. Thank you very much:
Code:
using System;
using NHibernate;
using NHibernate.Cfg;

namespace EmployeeTimeTracker
{
   [Serializable]
   public class EmpTime
   {
      public override bool Equals(object obj)
      {
         return base.Equals (obj);
      }

      public override int GetHashCode()
      {
         return base.GetHashCode ();
      }

      public EmpTime()
      {
      }   

      // set member variables
      private string m_emp_id = string.Empty;
      private string m_time_id = string.Empty;
      private int m_reason_id;
      private DateTime m_date_used;
      private double m_hours_used;
      
      public virtual string EmpID
      {
         get {return m_emp_id;}
         set {m_emp_id = value;}
      }

      public virtual string TimeType
      {
         get {return m_time_id;}
         set {m_time_id = value;}
      }

      public virtual int AttendReason
      {
         get {return m_reason_id;}
         set {m_reason_id = value;}
      }
      
      public virtual DateTime DateUsed
      {
         get {return m_date_used;}
         set {m_date_used = value;}
      }
      public virtual double HoursUsed
      {
         get {return m_hours_used;}
         set {m_hours_used = value;}
      }   
      
   }
}


Top
 Profile  
 
 Post subject: Solution
PostPosted: Wed Apr 18, 2007 10:35 am 
Newbie

Joined: Mon Apr 16, 2007 3:20 pm
Posts: 7
My solution was to get "objectmapper". It's one of the programs that write the class files and mappings for you. It's included in the Puzzle Framework, another open source project that has a bunch of things available. You can download here:

http://www.objectmapper.com/download/

I found a tutorial for making the class files and xml mappings for the Northwind database here:

http://www.objectmapper.com/Doc/Tutoria ... thwind.htm

I still had some difficulty w/ the way it created some of hte files. The class files that were exported didn't declare any properties as "virtual". I guess there is some setting that may do that for you, but I couldn't find. The tutorial also tells you to use the "Run" button on the tool bar, but for the most part, you have to use the "Commit" button instead, which is directly to the right of the Run button. It's got the little circle arrows, like "refresh". It seems to work though.


Top
 Profile  
 
 Post subject: overloading hashcode
PostPosted: Wed Apr 18, 2007 3:12 pm 
Newbie

Joined: Tue Mar 27, 2007 6:30 am
Posts: 12
I has a similiar problem and found the errorto be my overriding of the equals method.

It should test for equality for the composite key


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