-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problems working with views
PostPosted: Fri Jan 09, 2009 1:46 pm 
Newbie

Joined: Fri Jan 09, 2009 1:23 pm
Posts: 3
I was working on a very simple example to test views on NHibernate. However I couldn't get the thing to work. The code compiles, the program runs, the call to Configure() from the configuration object succedes and I can create instances of the object representing the view.
But when I try to list the content of the view the result is always an empty List. I know the view returns values because I tested it on sql server, I'm not doing any writing, deleting or updating records and the source of data from the view is from a single table.

I can't see what I'm doing wrong, maybe its something stupid but I just can't figure it out. Anyway, this is what I've got:

Database:
Table Customer(id, name)
View CustomerTypes(id, name) -> a simple SELECT * FROM CUSTOMER

The Mappings:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest" assembly="NHibernateTest">
  <class name="Customer" table="Customer">
    <id name="Id" column="Id" type="Int64">
      <generator class="native"/>
    </id>
    <property column="Name" type="String" name="Name" length="50"/>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateTest.CustomerTypes,NHibernateTest" table="CustomerTypes" lazy="true" mutable="false">
    <property type="long" access="field" not-null="true" name="_id" column="[id]" />
    <property type="string" access="field" not-null="true" length="50" name="_name" column="[name]" />
  </class>
</hibernate-mapping>


The Classes:
Code:
public class Customer
{
   public virtual long Id { get; set; }

   public virtual string Name { get; set; }
}

public class CustomerTypes
{
   public virtual long Id { get; set; }

   public virtual string Name { get; set; }
}


The program code:
Code:
Configuration cfg = new Configuration();
cfg.Configure();

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

IList l = new List<object>();

CustomerTypes ct = new CustomerTypes();
ICriteria crit = session.CreateCriteria(typeof(CustomerTypes));
crit.List(l);

transaction.Commit();
session.Close();


The list comes out empty, with 0 elements.

Can anyone see a the problem? The Customer class works fine, I can list its content, but the view doesn't work. :(

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2009 10:13 am 
Newbie

Joined: Fri May 25, 2007 5:06 am
Posts: 11
Do you have "_id" and "_name" in the CustomerTypes class (there is none in your pasted code)?

_________________
Regards,
Thomas

NConstruct - NHibernate code generation tool


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2009 10:52 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You also need a id mapping in CustomerTypes, not just the properties. Or is this left out for readability ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 5:57 am 
Newbie

Joined: Fri Jan 09, 2009 1:23 pm
Posts: 3
Sorry for the late reply, I had an exam yesterday.

Ok, so there were a couple of wrong things with it:

I didn't have _id and _name mapped correctly, I don't know what happened.
I forgot to put the dam mapping as a embeded resource.
I didn't have the <id> mapping

I changed it and it works.

So, thanks for the help.

I knew It was something stupid : (


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