-->
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.  [ 13 posts ] 
Author Message
 Post subject: Constructing a non-persistent object using HQL
PostPosted: Thu May 17, 2007 8:33 am 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I have a transient object I'm using for data binding to my UI LookupDTO and I have a mapped class called Market.

I'm trying the following HQL query to construct a list of LookupDTO objects using the values from the mapped class as follows:

Code:
return session.CreateQuery
   ("SELECT new LookupDTO(Id, Id) FROM Market").List<LookupDTO>();


I'm getting the following error:
Code:
NHibernate.QueryException: class not found: LookupDTO [SELECT new LookupDTO(Id, Id) FROM TransAlta.DealManagement.Domain.Market.Market]



The error indicates that I'm missing a reference somewhere, but I'm not sure which dll. I'm referencing the NHibernate.dll.

Any help would be much appreciated...

Greg

Code Below...

LookupDTO

Code:
[Serializable]
    public class LookupDTO
    {
        private string key;
        private string text;

        public LookupDTO()
        {
        }

        public LookupDTO(string key, string text)
        {
            this.key = key;
            this.text = text;
        }

        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public override string ToString()
        {
            return text;
        }
    }
}


Market
Code:

public class Market : DomainObject<string>
{
}

public class DomainObject<T> : IDomainObject
    {
        private T id;
        protected int version;
        protected DateTime createDate;
        protected DateTime lastModifiedDate;
        protected string createBy;
        protected string lastModifiedBy;


        public virtual void SetId(object o)
        {
            id = (T) o;
        }

        public void MarkModified()
        {
            LastModifiedDate = DateTime.Now;
            LastModifiedBy = Thread.CurrentPrincipal.Identity.Name;
        }

        public void MarkCreated()
        {
            Version = 1;
            CreateDate = DateTime.Now;
            CreateBy = Thread.CurrentPrincipal.Identity.Name;
            MarkModified();
        }

        public virtual T Id
        {
            get { return id; }
            set { SetId(value); }
        }

        public int Version
        {
            get { return version; }
            set { version = value; }
        }

        public DateTime CreateDate
        {
            get { return createDate; }
            set { createDate = value; }
        }

        public DateTime LastModifiedDate
        {
            get { return lastModifiedDate; }
            set { lastModifiedDate = value; }
        }

        public string CreateBy
        {
            get { return createBy; }
            set { createBy = value; }
        }

        public string LastModifiedBy
        {
            get { return lastModifiedBy; }
            set { lastModifiedBy = value; }
        }

        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            DomainObject<T> _domainObject = obj as DomainObject<T>;
            if (_domainObject == null) return false;
            return Equals(id, _domainObject.id);
        }

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

public interface IDomainObject
    {
        void SetId(object o);
        void MarkModified();
        void MarkCreated();

        int Version { get; set; }

        DateTime CreateDate { get; set; }

        DateTime LastModifiedDate { get; set; }

        string CreateBy { get; set; }

        string LastModifiedBy { get; set; }
    }



Mapping for Market

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="TransAlta.DealManagement.Domain" namespace="TransAlta.DealManagement.Domain.Market">
 
  <class name="Market" table="mrktc" lazy="false" mutable="false" where="mrktc_active='1' AND mrktc_futopt IN ('I', 'P')">

    <id name="Id" column="mrktc_mkt" type="string" access="field.camelcase">
      <generator class="native" />
    </id>
 
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Constructing a non-persistent object using HQL
PostPosted: Thu May 17, 2007 12:11 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
gcook1@shaw.ca wrote:
Code:
return session.CreateQuery
   ("SELECT new LookupDTO(Id, Id) FROM Market").List<LookupDTO>();


I'm getting the following error:
Code:
NHibernate.QueryException: class not found: LookupDTO [SELECT new LookupDTO(Id, Id) FROM TransAlta.DealManagement.Domain.Market.Market]



The error indicates that I'm missing a reference somewhere, but I'm not sure which dll. I'm referencing the NHibernate.dll.

Any help would be much appreciated...

Greg


Greg,

Just add an import line to your mapping file. At the top of the mapping file put <import class="LookupDTO" rename="LookupDTO" />.

Now NHibernate will be aware of your class and how to use it. Problem solved.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 2:30 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You can also do the import programmatically at runtime on the configuration object before creating the session factory.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 12:08 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
merge_s.rottem wrote:
You can also do the import programmatically at runtime on the configuration object before creating the session factory.

Cheers,

Symon.


Symon,

Do you have an example of doing this programmatically?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 12:21 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Code:
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();

//This imports the class Fully.Qualified.Class so it can be queried in HQL as "MyClass".
cfg.CreateMappings().AddImport("Fully.Qualified.Class", "MyClass");

SessionFactory factory = cfg.Configure().BuildSessionFactory();


Hope that helps

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 12:53 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
merge_s.rottem wrote:
Code:
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();

//This imports the class Fully.Qualified.Class so it can be queried in HQL as "MyClass".
cfg.CreateMappings().AddImport("Fully.Qualified.Class", "MyClass");

SessionFactory factory = cfg.Configure().BuildSessionFactory();


Hope that helps

Symon.


Symon,

I'm still getting the same "class not found" error...
Here is my code for creating my SessionFactory:

Code:

Configuration cfg = new Configuration();
cfg.Properties = DMSProperties;

DMS.DealManagement _dealManagement =
         new DMS.DealManagement();

cfg.AddAssembly(_dealManagement.GetType().Assembly);

cfg.CreateMappings().AddImport(
               "DealManagement.DTO.LookupDTO", "LookupDTO");

dmsSessionFactory = cfg.BuildSessionFactory();
[/b]


Here is my call to ISession again...
Code:
return Session.CreateQuery("SELECT new LookupDTO(u.ZainetUserId,                     u.ProfileName) FROM UserProfile u").List<LookupDTO>();


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 1:11 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Is ZainetUserId a string?

Since your LookupDTO constructor is expecting two params and both are strings I think the values passed need to match the constructor signature. I'm suspicious that this might not be the case from your query statement.

If that's not it then I'm out of ideas.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 1:29 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
merge_s.rottem wrote:
Is ZainetUserId a string?

Since your LookupDTO constructor is expecting two params and both are strings I think the values passed need to match the constructor signature. I'm suspicious that this might not be the case from your query statement.


It's clear that this is the issue. If you notice he returns u.ZainetUserId and u. Obviously u isn't a string if it has a ZainetUserId parameter on it. Correct the constructor parameters and it should work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 1:44 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
The following is the mapping document for the UserProfile object. Both of the fields used here are string. the u. is an alias to UserProfile as per the hql query. It may not be needed. the intent of the SELECT is to get the ProfileName and ZainetUserId elements from within the UserProfile object as constructor arguments for the DTO transient object..

I thought that since the mapping document identifies these fields as string so I thought I would be good...

BTW: Thanks very much for the help...

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="xxx.DealManagement.Domain" namespace="xxx.DealManagement.Domain">
 
   <class name="UserProfile" table="User_Profile" lazy="false">     
      <id name="Id"  column="User_Profile_Id" access="field.camelcase" unsaved-value="0" type="Int32">
         <generator class="native"/>
      </id>
      <property name="ProfileName" column="Profile_Name" type="string" access="field.camelcase" not-null="true"/>
      <property name="ZainetUserId" column="Zainet_User_Id" type="string" not-null="false" access="field.camelcase"/>
      <property name="Version" column="Version" not-null="true" access="field.camelcase"/>
      <property name="CreateDate" column="Create_Date" not-null="true" access="field.camelcase"/>
      <property name="LastModifiedDate" column="Last_Modified_Date" not-null="true" access="field.camelcase"/>
      <property name="CreateBy" column="Create_By" not-null="true" access="field.camelcase"/>
      <property name="LastModifiedBy" column="Last_Modified_By" not-null="true" access="field.camelcase"/>

   </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 1:51 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
gcook1@shaw.ca wrote:
The following is the mapping document for the UserProfile object. Both of the fields used here are string. the u. is an alias to UserProfile as per the hql query. It may not be needed. the intent of the SELECT is to get the ProfileName and ZainetUserId elements from within the UserProfile object as constructor arguments for the DTO transient object..


Right, but as you are using it now your constructor must be take string, UserProfile. If you change your query to return u.ZainetUserId, u.ProfileName, then your code will work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 2:02 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
jchapman wrote:
gcook1@shaw.ca wrote:
The following is the mapping document for the UserProfile object. Both of the fields used here are string. the u. is an alias to UserProfile as per the hql query. It may not be needed. the intent of the SELECT is to get the ProfileName and ZainetUserId elements from within the UserProfile object as constructor arguments for the DTO transient object..


Right, but as you are using it now your constructor must be take string, UserProfile. If you change your query to return u.ZainetUserId, u.ProfileName, then your code will work.


Sorry. My HQL example was mangled a bit. It is actually using both the u.ZainetUserId, u.ProfileName as you suggest. Still get the error.

Code:
return Session.CreateQuery("SELECT new LookupDTO(u.ZainetUserId, u.ProfileName) FROM UserProfile u").List<LookupDTO>();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 3:39 am 
Newbie

Joined: Fri Jun 08, 2007 3:27 am
Posts: 1
I have the same error and the development of my application is blocked.
And i don't want choose an alternative solution...

Since may 22, have you found and resolve the error ?
If yes, it is possible to have some samples codes so i can continue the development.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 7:46 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Scorpi666_be wrote:
I have the same error and the development of my application is blocked.
And i don't want choose an alternative solution...

Since may 22, have you found and resolve the error ?
If yes, it is possible to have some samples codes so i can continue the development.


Scorpi, read the post. Just add the import line to your mapping file. Everything should work after that. I believe the original poster ran into an issue when he tried to programmatically import the class.


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