-->
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.  [ 7 posts ] 
Author Message
 Post subject: Unable to Retrieve Objects
PostPosted: Fri Mar 30, 2007 4:18 am 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
Greetings:

I'm trying to retrieve the Client object from the database based on the "ClientSSN" field. The customer wants to search by Social, which as a matter of design is not the PK for the Database.

Here's the query I'm using:
Code:
            ICriteria criteria = session.CreateCriteria(typeof(Client));
            criteria.Add(NHibernate.Expression.Expression.Eq("ClientSSN", txtClientSSN.Text));
            IList client = criteria.List();


I get the following Error from the compiler (that admittedly, I don't understand and Google isn't providing much help:
Code:
Error   1   Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments


Finally, if there's a better way to retrieve an object from the DB where you know you will only retrieve one set of records, please let me know. In my searches, I'm not interested in retrieving more than one client at a time, I just want to load the client into the app by inputting their SSN.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 4:27 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Use session.UniqueResult<T> for retrieving a single result. It'll throw if it finds more than one match.
The compiler is complaining because you are retrieving an ArrayList from NHibernate using criteria.List() when what you coded is an generic IList<T>. Use criteria.List<T>() instead.

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 9:12 am 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
Code:
Client client = (Client)session.CreateQuery(
      "select client from Client as client where client.ClientSSN = ?")
      .SetEntity("ClientSSN", txtClientSSN.Text)
      .UniqueResult();


That above is the query. Essentially, I'm trying to grab the object client from the DB (because I'll want to load all properties of that client into textfields and the like), well, it's giving me this exception when I use it at runtime:
Code:
"Not all named parameters have been set: [] [select client from Client as client where client.ClientSSN = ?]"


I'm missing something, aren't I. Errgh. I'm probably just tired, I've been fooling with this most of the night.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 6:24 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
try this in HQL
Code:
Client client = (Client)session.CreateQuery("from Client where ClientSSN = :ssn").SetParameter("ssn",txtClientSSN.Text);

This is using named parameters to pass into the query.
You could use the ICriteria route too:
Code:
ICriteria crit = session.CreateCriteria(typeof(Client)).Add(Expression.Eq("ClientSSN",txtClientSSN.Text));


Hope this helps
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 10:29 am 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
Mike, thanks for your help thus far. I like your solution, I hit a snag with it:
Code:
System.InvalidCastException was unhandled
  Message="Unable to cast object of type 'NHibernate.Impl.QueryImpl' to type 'SocksCDS.Client'."
  Source="Socks CDS"
  StackTrace:
       at SocksCDS.Form2.btnRetrieveClient_Click(Object sender, EventArgs e) in C:\Code Projects\New Socks CDS\Socks CDS\Form2.cs:line 276
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at SocksCDS.Program.Main() in C:\Code Projects\New Socks CDS\Socks CDS\Program.cs:line 27
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Google isn't very helpful with this exception.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 10:55 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Can you post your code that is used ?
It looks like you used the HQL I wrote...try this
Code:
Client client = (Client)session.CreateQuery("from Client where ClientSSN = :ssn").SetParameter("ssn",txtClientSSN.Text).UniqueResult<Client>();


I forgot the UniqueResult<T> bit at the end.
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 11:12 am 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
Thanks M. You helped a bit. :-)


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