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.  [ 3 posts ] 
Author Message
 Post subject: Problem trying to set association type by reflection
PostPosted: Tue Dec 26, 2006 5:36 pm 
Newbie

Joined: Mon Dec 25, 2006 1:22 pm
Posts: 2
Hi,

I am new to the Nhibernate.

I have two tables, created with the constraints.

User
Id (Primary Key)
Name

and

Video
VideoId
UserId (FK_User_Id)
Description

and the following is the mapping file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="business.User, business" table="User" >
<id name="Id" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<set name="Video" inverse="true" lazy="true">
<key column="UserId"/>
<one-to-many class="business.Video, business"/>
</set>
<property name="Name" column="Name" type="String" length="50" />
</class>
<class name="business.Video, business" table="Video" >
<id name="VideoId" column="VideoId" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="UserId" column="UserId" type="Int32" length="4" />
<many-to-one name="User" column="Id" not-null="true"/>
<property name="Description" column="Description" type="String" />
</class>
</hibernate-mapping>


My class files contain the code

using System;
namespace business
{
public class User
{
private int _Id;
private string _Name;

public int Id
{
get { return _Id; }
set { _Id = value; }
}
public int Name
{
get { return _Name; }
set { _Name = value; }
}
}
}


using System;
namespace com.zvdeo.business
{
public class Video
{
private int _VideoId;
private int _UserId;
private string _Description;

public int VideoId
{
get { return _VideoId; }
set { _VideoId = value; }
}
public int UserId
{
get { return _UserId; }
set { _UserId = value; }
}
public string Description
{
get { return _Description; }
set { _Description = value; }
}
}
}


I just wanted to retrieve the records of the table Video by executing..

session.CreateQuery("select V.VideoId, V.UserId, V.Description from video V");

and i am getting the Nhibernate.MappingException as
{"Problem trying to set association type by reflection"}

and InnerException as

{"Could not find a getter for property User in class business.Video"}


Is the mapping file which I created wrong?

What should I do to get the correct results?

Thanks in advance,
Ali


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 1:10 am 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
The error message you got gives you the hint what to do. In your mapping files you have defined a bidirectional relation between User and Video. But in your class definition this relation is missing. In your User class you have to define a Video property and in your video class you have to define a User collection.

Code:
public class User
{
   private int _Id;
   private string _Name;
   private Video _Video
   ...
   public Video Video
   {
      get { return _Video; }
      set { _Video = value; }
   }
}

public class Video
{
   private int _VideoId;
   //private int _UserId; Replaced by _Users!!!
   private IList _Users
   private string _Description;

   ...
   
   public IList Users
   {
      get { return _Users; }
      set { _Users = value; }
   }
}


Your Query should look llike

Code:
session.CreateQuery("select V.VideoId, V.Users, V.Description from video V");


I have found a nice article about object relational mapping sometimes ago. Here is the link:

http://www.agiledata.org/essays/mappingObjects.html

Regards
Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 12:51 pm 
Newbie

Joined: Mon Dec 25, 2006 1:22 pm
Posts: 2
Hi Klaus,

Thanks so much for the solution..


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