-->
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: How to list objects by Example?
PostPosted: Tue Jan 24, 2006 4:03 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi!

I'm trying to list objects by example using this code:

Code:
public static IList ListByExample(object example)
{
    ICriteria criteria = NHibernateHelper.GetSession().CreateCriteria(example.GetType());
    Example sample = Example.Create(example);
    sample.ExcludeZeroes();
    sample.ExcludeNulls();
    criteria.Add(sample);

    return criteria.List();       
}


I'm getting all records as the result! Anybody know what is wrong?

Tks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 2:56 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hey guys, nobody knows? :(

I do need this feature...

Thanks and sorry!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 8:23 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
post some mappings and source code...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 8:41 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
dado2003 wrote:
post some mappings and source code...


Hi dado2003!

The code:

Code:
public static IList ListByExample(object example)
{
    ICriteria criteria = NHibernateHelper.GetSession().CreateCriteria(example.GetType());
    Example sample = Example.Create(example);
    sample.ExcludeZeroes();
    sample.ExcludeNulls();
    criteria.Add(sample);

    return criteria.List();       
}


The mapping:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MapOR.Contato,MapOR" table="contato">

      <composite-id access="field">
          <key-property name="EmpId" column="EMP_ID" type="Int32" />
          <key-property name="PesId" column="PES_ID" type="String"/>
          <key-property name="ConId" column="CON_ID" type="Int32" />         
      </composite-id>

      <property column="CON_NOME" type="String" name="ConNome" not-null="true" length="50" />
      <property column="CON_FONE1" type="String" name="ConFone1" length="12" />
      <property column="CON_FONE2" type="String" name="ConFone2" length="12" />
      <property column="CON_FAX" type="String" name="ConFax" length="12" />
      <property column="CON_RAMAL" type="String" name="ConRamal" length="5" />
      <property column="CON_DEPARTAMENTO" type="String" name="ConDepartamento" length="50" />
      <property column="CON_HOMEPAGE" type="String" name="ConHomepage" length="50" />
      <property column="CON_EMAIL" type="String" name="ConEmail" length="50" />
      <property column="CON_DTANIVERSARIO" type="DateTime" name="ConDtaniversario" />
      
   </class>
</hibernate-mapping>


When I try to do this:

Code:
Contato c = new Contato();
c.EmpId = 1;
c.PesId = 1;

IList lista = ListByExample(c);


The variable "lista" will receive all records from the table "contato". The filter EmpId and PesId isn't being used.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 10:33 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
I have used QBE but I have not used composite-id.

Does it work for other situations where you don't have a composite-id ? Have you looked at the generated sql? I'll try to reproduce this and, if possible, get back with more info.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 10:37 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
dado2003 wrote:
I have used QBE but I have not used composite-id.

Does it work for other situations where you don't have a composite-id ? Have you looked at the generated sql? I'll try to reproduce this and, if possible, get back with more info.


I don't know if it works when I don't use composite-id. All of my tables have composite-id :(

I have not looked the generated sql... could you tell me how do you do it? Using log4net? Nhibernate query analyzer?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 11:59 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
Sure,

- Using log4net for debugging NHibernate. See http://wiki.nhibernate.org/display/NH/Configuring+log4net+Logging for how to do this.

- Using ShowSql configuration (this will log to the console for console applications)

- Using a specific database profiler or network monitor (for SQL Server use SQL Profiler, for Oracle use Packet Sniffer or something similar).

Surelly the first approach is the better...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:31 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
dado2003 wrote:
Sure,

- Using log4net for debugging NHibernate. See http://wiki.nhibernate.org/display/NH/Configuring+log4net+Logging for how to do this.

- Using ShowSql configuration (this will log to the console for console applications)

- Using a specific database profiler or network monitor (for SQL Server use SQL Profiler, for Oracle use Packet Sniffer or something similar).

Surelly the first approach is the better...


Ok... but about my problem... this feature (QBE) already is disponible in NHibernate?

As you can see, I think that my code is right.

Do you agree?

Thanks guy!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 11:01 am 
Newbie

Joined: Tue Oct 24, 2006 9:10 am
Posts: 17
I think that the QBE is not to be used with ID's. So it won´t work.


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.