-->
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.  [ 8 posts ] 
Author Message
 Post subject: N00b - nothing returned in session.CreateCriteria
PostPosted: Sun Mar 25, 2007 3:09 pm 
Newbie

Joined: Sun Mar 25, 2007 2:54 pm
Posts: 4
Hibernate version: 1.2

Code between sessionFactory.openSession() and session.close():
Code:
StringBuilder sb = new StringBuilder();

IList ItemList = session.CreateCriteria(typeof(Items)).List();

foreach (Items itm in ItemList)
{
  sb.Append("ItemID: " + itm.Itemid + ", ContentName: " + itm.ContentName + ";");
}

Full stack trace of any exception that occurs:No Error - nothing returned.

Name and version of the database you are using:SQL Server 2005 Express Edition


Ok, I'll explain the problem in English! I'm new to NHibernate, just installed it today and am trying to get it up and running. I used MyGeneration to generate the mapping xml files, running it over SQL Server 2005 Express Edition. They generated ok, and I've included them in my Visual Studio 2005 solution.
I've done everything in the quickstart tutorial, i.e. - created the mapping files over the DB tables, referenced the NHibernate DLL in my project, and then at the top of my class included the using NHibernate and NHibernate.cfg lines.
The code I've listed above runs through ok, but nothing is returned from the CreateCriteria to the IList. I'm at a loss here how to go about figuring out why this is happening - there is no error and there is definitely data in the table.

Sorry if there is something very simple I'm missing here, but as I say I'm a complete n00b to NHibernate!
Thanks
Neal


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 3:47 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Did you check the generated SQL and test it manually with your favorite DB client?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 4:25 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
The class you are passing to CreateCriteria is probably not mapped (i.e. NHibernate doesn't know about it).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 2:16 pm 
Newbie

Joined: Sun Mar 25, 2007 2:54 pm
Posts: 4
sergey wrote:
The class you are passing to CreateCriteria is probably not mapped (i.e. NHibernate doesn't know about it).

Thanks for getting back to me on this sergey

First off, seeing I'm new to NHibernate I'm following the instructions in the quickstart tutorial - http://www.hibernate.org/362.html. In the tutorial it doesn't really go into detail on how NHibernate would know about the classes other than having the .cs and .hbm.xml files, which I have.

My question on your reply - if NHibernate didn't know about the class would an error not be returned rather than an empty collection?

My code again:
Code:
Configuration cfg = new Configuration();
cfg.AddAssembly("TestORMNHibernate");

ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();

StringBuilder sb = new StringBuilder();

IList ItemList = session.CreateCriteria(typeof(Items)).List();

foreach (Items itm in ItemList)
{
     sb.Append("ItemID: " + itm.Itemid + ", ContentName: " + itm.ContentName + ";");
}


In my project I have a folder called NHibernate where I am storing all the .cs and .hbm.xml files. In there I have an Items.cs which begins
Code:
using System;

namespace TestORMNHibernate
{
   /// <summary>
   ///   Generated by MyGeneration using the NHibernate Object Mapping template
   /// </summary>
   [Serializable]
   public sealed class Items


The Items.hbm.xml begins:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="TestORMNHibernate.Items,TestORMNHibernate" table="items">


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 2:48 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Is Item.hbm.xml's build action set to Embedded Resource?
The reason an error is not returned in this case is quite complicated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 6:04 pm 
Newbie

Joined: Sun Mar 25, 2007 2:54 pm
Posts: 4
Thanks again sergey, my bad. I missed this bit, I have now set the Build Action to Embedded Resource for all the .hbm.xml files in the project.

When I run the code again with the following line commented out, again nothing is returned.
Code:
cfg.AddAssembly("TestORMNHibernate");


When I uncomment this line, this returns the following error when I try to run it:
Code:
NHibernate.MappingException: TestORMNHibernate.NHibernate.Usermodules.hbm.xml(2,2): XML validation error: Could not find schema information for the element 'urn:nhibernate-mapping-2.0:hibernate-mapping'. ---> System.Xml.Schema.XmlSchemaException: Could not find schema information for the element 'urn:nhibernate-mapping-2.0:hibernate-mapping'.


In the Comments section of http://www.hibernate.org/362.html, there is a post by beny which states:
Quote:
urn:nhibernate-mapping-2.0 should be urn:nhibernate-mapping-2.2 for 1.2 beta

so I tried this and got the following error:
Code:
NHibernate.MappingException: TestORMNHibernate.NHibernate.Usermodules.hbm.xml(7,4): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content. List of possible elements expected: 'urn:nhibernate-mapping-2.2:meta urn:nhibernate-mapping-2.2:jcs-cache urn:nhibernate-mapping-2.2:cache urn:nhibernate-mapping-2.2:id urn:nhibernate-mapping-2.2:composite-id'. ---> System.Xml.Schema.XmlSchemaValidationException: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content. List of possible elements expected: 'urn:nhibernate-mapping-2.2:meta urn:nhibernate-mapping-2.2:jcs-cache urn:nhibernate-mapping-2.2:cache urn:nhibernate-mapping-2.2:id urn:nhibernate-mapping-2.2:composite-id'.


I then had a look at the .hbm.xml mapping file, it didn't contain any schema, only this message:
Quote:
<!-- composite primary key support is touchy. View the documentation for syntax. -->

So it looks like the MyGeneration template I downloaded (the one by k-dub), is going to need a bit of tweaking ;) - or else I need a new one! Any tips for a good way of creating the .hbm.xml and .cs files?

Ok, so now I've removed all the .hbm.xml and .cs mapping files except 2 of each, 1 for the Items table which I'm trying to retrieve, and another that is linked. When I run it again, I'm getting an error at line
Code:
ISessionFactory factory = cfg.BuildSessionFactory();


The error is:
Code:
NHibernate.InvalidProxyTypeException: The following types may not be used as proxies:
TestORMNHibernate.Users: method get_Userid should be virtual
TestORMNHibernate.Users: method set_Userid should be virtual
TestORMNHibernate.Users: method get_UserType should be virtual
TestORMNHibernate.Users: method set_UserType should be virtual

I think I probably need to go back to the drawing board at this stage and look at either changing the MyGeneration template to generate the .hbm.xml and .cs files, or else find a better way of creating them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 6:25 pm 
Newbie

Joined: Sun Mar 25, 2007 2:54 pm
Posts: 4
Again in the comments section of http://www.hibernate.org/362.html
there is a note by Idiosyncrasy which states
Quote:
(2) In both versions I tried (1.0.2.0 and 1.2.0.Alpha1) I got
an "method get_Id() should be virtual" error. To allow dynamic proxy
generation in your class you must add a virtual modifier to all you
accessors, like this:

public virtual string Id
{
get { return id; }
set { id = value; }
}

Tried this, but now it bombs out on the BuildSessionFactory line, not even giving an error, just HTTP 500 Internal Server Error in the browser


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 1:16 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Since you are using 1.2.0, you need to change the schema namespace in the generated files from <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"> to <... xmlns="urn-nhibernate-mapping-2.2" />.

As for the 500 error, I can't help you with that.


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