-->
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.  [ 6 posts ] 
Author Message
 Post subject: Nhibernate and system tables
PostPosted: Tue Mar 18, 2008 10:26 am 
Newbie

Joined: Tue Mar 18, 2008 10:13 am
Posts: 3
Hi,

this is my problem. I need to read the INFORMATION_SCHEMA.COLUMNS from the SQL Server 2005. I want to map the outcomes to this class:

Code:
class Outcome
    {
        private string table_name;
        private string column_name;

        public Outcome()
        {
        }

        public string Table_name
        {
            get { return table_name; }
            set { name = value; }
        }
        public string Column_name
        {
            get { return column_name; }
            set { name = value; }
        }

    }


I've used this piece of code to write everything out:
Code:
IList outcomelist= session.CreateSQLQuery("SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS").AddScalar("TABLE_NAME", NHibernateUtil.String).AddScalar("COLUMN_NAME", NHibernateUtil.String).List();
                write out with foreach...


But now my question is: how can i map the outcome of CreateSQLQuery to a list of Outcome-objects? Do i need a mapping-file (.hbm.xml) or not?

It's fairly easy to manipulate tables that are created by yourself then read from system tables/views.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 1:33 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Either. The advantage of using a mapped class is that it appears that you don't have to call AddScalar() for every column; it detects the info from the mapping.

See http://www.hibernate.org/hib_docs/nhibe ... ml#d0e8984 for using mapped entities, and http://www.hibernate.org/hib_docs/nhibe ... ml#d0e9192 for using non-mapped entities (sometimes called DTOs, for Data Transfer Objects).

It's up to you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 5:23 am 
Newbie

Joined: Tue Mar 18, 2008 10:13 am
Posts: 3
Alright, i tried something with the non-mapped entities:

Outcome.cs:
Code:
class Outcome
    {
        private string table_name;

        public Outcome()
        {
        }

        public string Table_name
        {
            get { return table_name; }
            set { table_name = value; }
        }
    }


nhibernate:
Code:
IList tablelist = session.CreateSQLQuery("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'").SetResultTransformer(Transformers.AliasToBean(typeof(databases))).List();


but i get:
Quote:
could not find a setter for property 'TABLE_NAME'


what am i doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 7:17 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
i that since you didn't define a mapping document, nhibernate
searches for properties that have the same name as the columns.

try naming your properties like the columns


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 1:38 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
First, I believe it is case sensitive; second, your classname is "Outcome" but you set your transformer for a "database" class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 20, 2008 5:42 am 
Newbie

Joined: Tue Mar 18, 2008 10:13 am
Posts: 3
marcal wrote:
First, I believe it is case sensitive; second, your classname is "Outcome" but you set your transformer for a "database" class.


Sorry, i forgot to change that :)

@Matrasinator: thank you, that did the trick!


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