Simple ICriteria Query returning empty list

282 views
Skip to first unread message

maumad

unread,
Jan 31, 2011, 10:27:40 PM1/31/11
to Castle Project Users
I am trying to get very simple query working. It does not throw but
returns empty list. Here is the query:

using (var session =
ActiveRecordContext.Instance.SessionFactory.OpenSession())
{
var query1 = session.CreateCriteria<ModuleData>();
query1.SetProjection(Projections.Property("Id"));
query1.Add(Expression.Ge("Id", 0));
var list = query1.List<ModuleData>();
}

If I write HqlBasedQuery it works fine:

var query = new HqlBasedQuery(typeof(ModuleData),
QueryLanguage.Sql,
@"SELECT Id
FROM `aviprod`.`ModuleData`
WHERE Id > '0'");

Any pointers?

Tomek Pluskiewicz

unread,
Feb 1, 2011, 3:56:07 PM2/1/11
to Castle Project Users
Try peeking at the generated query using NHibernate Profiler.

maumad

unread,
Feb 1, 2011, 7:28:42 PM2/1/11
to castle-pro...@googlegroups.com
Tomek,

Thanks for your suggestion. I am really n00b in using ActiveRecord so I apologize for asking these questions.

I installed NHibernateProfiler and saw that it is generating no query. 

I am not expecting spoon feeding but what could be going wrong in this simple query? How can I make sure Session object is correct.

FYI: I am not using xml files for mappings. I have attributed all my properties. Is that the problem?

Troublesome statement again:

ActiveRecordContext.Instance.SessionFactory.OpenSession())
{
    var query1 = session.CreateCriteria<ModuleData>();
    // query1.SetProjection(Projections.Property("Id"));
    // query1.Add(Expression.Ge("Id", 0));

    var list = query1.List<ModuleData>();
}

Thanks!
--Am


2011/2/1 Tomek Pluskiewicz <ploo...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


José F. Romaniello

unread,
Feb 1, 2011, 7:57:07 PM2/1/11
to castle-pro...@googlegroups.com
I think you dont have to set the projection on the id.


2011/2/1, maumad <mau...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To post to this group, send email to castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to
> castle-project-u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/castle-project-users?hl=en.
>
>

--
Enviado desde mi dispositivo móvil

maumad

unread,
Feb 1, 2011, 8:13:53 PM2/1/11
to castle-pro...@googlegroups.com
Commenting that out does not help either. Code is reduced just to 2 lines now:

ActiveRecordContext.Instance.SessionFactory.OpenSession())
{
    var list = session.CreateCriteria<ModuleData>().List<ModuleData>();
}

Now my suspicion is on session object. How can I make sure it is correct?

To verify, I tried: ?session.Get<CellId>(01010101)

I get MappingException Unhandled. {"No persister for: Avi.Engine.CellId"}.

But when I try to get the value by calling following code: I do get the CellId object back.

CellId.FindOne(Expression.Eq("Id", 01010101));

I do not have any xml mappings files. Is it necessary for session object to be created?

Thanks for reading through all this. Your help is appreciated.

--Am


2011/2/1 José F. Romaniello <jfroma...@gmail.com>

José F. Romaniello

unread,
Feb 3, 2011, 9:01:04 AM2/3/11
to castle-pro...@googlegroups.com
please, show your classes. and the query your want to execute. I don't understand your problem.

2011/2/1 maumad <mau...@gmail.com>

maumad

unread,
Feb 5, 2011, 11:07:00 PM2/5/11
to castle-pro...@googlegroups.com
Sorry that I was ambiguous. Here is code in details:

I have attached the Module data class file. And this is the query I want to execute:

ActiveRecordContext.Instance.SessionFactory.OpenSession())
{
    var list = session.CreateCriteria<ModuleData>().List<ModuleData>();
}

I have also attached the ActiveRecordContext file where I initialize the classes.

Let me know if I am doing anything wrong. Basically, the query is not generated when watched under NHibernate Profiler.

Thanks!
--Am


2011/2/3 José F. Romaniello <jfroma...@gmail.com>
ModuleData.cs
ActiveRecordContext.cs

maumad

unread,
Feb 5, 2011, 11:10:59 PM2/5/11
to castle-pro...@googlegroups.com
Pasting classes here since attachments are rejected:

/* Moduledata.cs classes */
using Castle.ActiveRecord;
using System;

namespace Avi.Engine
{
    [ActiveRecord(Table = "ModuleData", Lazy = false)]
    public class ModuleData : ActiveRecordBase<ModuleData>
    {
        [PrimaryKey(Generator = PrimaryKeyType.Native)]
        public int Id { get; set; }
        [Property]
        public string MobileNumber { get; set; }
        [Property]
        public DateTime EnteredDate { get; set; }
        [Property]
        public string ModuleId { get; set; }
        [Property]
        public string PhcSubcenter { get; set; }
        [Property]
        public int Value { get; set; }
    }
}

/* This is what is called when I initialize */
public void ActiveRecord_Init()
        {
            if (!ActiveRecordStarter.IsInitialized)
            {
                // Get environment and connection string from web.config
                string environment = ConfigurationManager.AppSettings["Environment"];
                string connectionString = string.Empty;
                switch (environment.ToLower())
                {
                    case "prod":
                        connectionString = ConfigurationManager.ConnectionStrings["avi-prod"].ConnectionString;
                        break;

                    default:
                    case "dev":
                    case "ppe":
                        connectionString = ConfigurationManager.ConnectionStrings["avi-ppe"].ConnectionString;
                        break;
                }

                var properties = new Dictionary<string, string>();
                properties.Add("connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
                properties.Add("dialect", "NHibernate.Dialect.MySQLDialect");
                properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
                properties.Add("connection.connection_string", connectionString);
                properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

                InPlaceConfigurationSource source = new InPlaceConfigurationSource();
                source.Add(typeof(ActiveRecordBase), properties);

                if (!ActiveRecordStarter.IsInitialized)
                {
                    ActiveRecordStarter.Initialize(source,
                        typeof(Avi.Engine.CellId),
                        /* typeof(Avi.Engine.AppError), */
                        typeof(Avi.Engine.FieldUser),
                        typeof(Avi.Engine.ModuleData),
                        typeof(Avi.Engine.RawIncomingSms));
                }

                try
                {
                    if (SessionFactory == null)
                    {
                        var config = new NHibernate.Cfg.Configuration();
                        config.Configure();
                        config.AddAssembly("Avi.Engine");
                        /*config.AddClass(typeof(Avi.Engine.ModuleData));
                        config.AddClass(typeof(Avi.Engine.CellId));
                        config.AddClass(typeof(Avi.Engine.FieldUser));
                        config.AddClass(typeof(Avi.Engine.RawIncomingSms));*/
                        SessionFactory = config.BuildSessionFactory();
                    }
                }
                catch (System.Exception ex)
                {

                }
            }
        }

--Am


2011/2/5 maumad <mau...@gmail.com>

Richard Reinicke

unread,
Nov 9, 2016, 8:42:23 AM11/9/16
to Castle Project Users, mau...@gmail.com
This is a really old thread but for anybody who`s facing such a behaviour, please make sure you declared all your mappings property build process to "embedded resource".
Reply all
Reply to author
Forward
0 new messages