-->
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: ICriteria.List<>() with added Example always returns n
PostPosted: Tue Feb 19, 2008 5:49 pm 
Newbie

Joined: Tue Feb 19, 2008 11:26 am
Posts: 3
Hello, Im currently doing my bachelor project for a company here in Norway with 3 other students.
We decided to use nHibernate, and have run into some trouble.
We are all new to nHibernate, and I have been given the main task of working with nHibernate.

The problem seems to happen when we are trying to get a generic IList from an ICriteria using List<>().
We are adding an Example with the desirable fields to the ICriteria,
but when the table has a many-to-many relation, it always returns an empty list.

Because it was a many-to-many relation we added a generic ISet in the code.
Without the ISet in the class List<>() returns correct, but with ISet it always returns nothing.

This is how we are doing it:
Lets say we have two classes/tables Employee and Position.
An Employee can have one or many different positions,
and one Position can be taken by one or many employees, which gives us a many-to-many relation.

Code:

Code:
public class DTOEmployee
{
    public virtual Int32 ID { get; set; }
    public virtual ISet<DTOEmployeePostion> Positions { get; set; }
}

public class DTOPosition
{
    public virtual Int32 ID { get; set; }
    public virtual ISet<DTOEmployeePosition> Employees { get; set; }
}

public class DTOEmployeePosition
{
    public virtual Int32 ID { get; set; }
    public virtual DTOEmployee Employee { get; set; }
    public virtual DTOPosition Position { get; set; }
    public virtual Nullable<DateTime> Date { get; set; }
}


Mapping documents:
Code:
  <class name="DTOEmployee" table="Employee">
    <id name="ID" type="Int32">
      <generator class="identity"/>
    </id>
    <set name="Positions">
      <key column="ID"/>
      <one-to-many class="DTOEmployeesPosition"/>
    </set>
  </class>

  <class name="DTOPosition" table="Position">
    <id name="ID" type="Int32">>
      <generator class="identity"/>
    </id>
    <set name="Employees">
      <key column="ID"/>
      <one-to-many class="DTOEmployeesPosition"/>
    </set>
  </class>

  <class name="DTOEmployeesPosition" table="EmployeesPosition">
    <id name="ID" type="Int32">>
      <generator class="identity"/>
    </id>
    <property name="Date" type="DateTime"/>
  </class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 5:37 am 
Newbie

Joined: Tue Feb 19, 2008 11:26 am
Posts: 3
oh, i forgot some of the code...

Code:
        public static IList<DTOEmployee> Find(DTOEmployee ex)
        {
            Example example = Example.Create(ex)
                .IgnoreCase()
                .ExcludeZeroes()
                .ExcludeNulls()
                .EnableLike();
            ICriteria crit = DBHandler.GetInstance().Session.CreateCriteria(typeof(DTOEmployee));
            crit.Add(example);
            return crit.List<DTOEmployee>();
        }


I have even tried with
Code:
example.ExcludeProperty("Positions");

which doesnt help
Does anyone know what we are doing wrong, or any better way to do this?


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.