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: Problems when working with Attributes
PostPosted: Wed Apr 09, 2008 11:39 pm 
Newbie

Joined: Thu Jul 26, 2007 6:58 am
Posts: 19
Hi,

I am creating a simple app to play with NHibernate 1.2.1GA, the app builds, I can initialise NHibernate but I could see 2 problems so far:

1) The Criteria.List<Class>() returns an empty list;
2) When saving an object I get the following Exception:

Unknown entity class: [NameOfClass]

I am using NHibernate.Mapping.Attributes to map the class and the XML to congif Hibernate is (Embedded Resource):

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>

      <property name="dialect">
        NHibernate.Dialect.MySQLDialect
      </property>
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="connection.driver_class">
        NHibernate.Driver.MySqlDataDriver
      </property>
      <property name="connection.connection_string">
        Server=localhost;Database=tracking;User ID=root;Password=
      </property>
      <property name="hibernate.show_sql">
        true
      </property>
   
    </session-factory>
</hibernate-configuration>


Regards, Jean


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 3:13 pm 
Newbie

Joined: Thu Jul 26, 2007 6:58 am
Posts: 19
Any one? Please.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 2:33 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Post the code when you create the session factory and the classes, it's hard to say anything without more information.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 9:15 am 
Newbie

Joined: Thu Jul 26, 2007 6:58 am
Posts: 19
Ok!

Sorry, I thought that problem/Exception might be a well known one! ;)

The place where I initialise NHibernate:
Code:
        public void InializeNHibernate()
        {
            try
            {
                Configuration objConfiguration = new Configuration();
                objConfiguration.Configure();

                _objFactory = objConfiguration.BuildSessionFactory();

                _bNHibernateInitialized = true;
            }
            catch (Exception ex)
            {
                _objLogger.Warn("Error when initializing NHibernate : " + ex);
            }

        }



2 typical classes:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Mapping.Attributes;

namespace ....Persistence
{
    [Class(Table = "assetgroups")]
    public class AssetGroup
    {
        #region Fields

        private int _iAssetGroupId;
        private Client _objAssetGroupClient;
        private string _strAssetGroupName;
        private List<Asset> _collAssets = new List<Asset>();
        private List<User> _collUsers = new List<User>();

        #endregion

        #region Properties

        [Id(Name = "assetGroupId")]
        [Generator(1, Class = "native")]
        public int AssetGroupId
        {
            get { return _iAssetGroupId; }
            set { _iAssetGroupId = value; }
        }

        [ManyToOne(Column = "assetGroupClientId")]
        public Client AssetGroupClient
        {
            get { return _objAssetGroupClient; }
            set { _objAssetGroupClient = value; }
        }

        [Property(Column = "assetGroupName")]
        public string AssetGroupName
        {
            get { return _strAssetGroupName; }
            set { _strAssetGroupName = value; }
        }

        [Bag(1, Inverse = true, Lazy = true, Table = "assetGroupAssets", Cascade = CascadeStyle.SaveUpdate),
        Key(2, Column = "[assetGroupAssetGroupId]"),
        ManyToMany(3, Column = "[assetGroupAssetAssetId]", ClassType = typeof(Asset))]
        public virtual List<Asset> Assets
        {
            get
            {
                return _collAssets;
            }
            set
            {
                _collAssets = value;
            }
        }

        [Bag(1, Inverse = true, Lazy = true, Table = "assetGroupPerms", Cascade = CascadeStyle.SaveUpdate),
        Key(2, Column = "[assetGroupPermGroupId]"),
        ManyToMany(3, Column = "[assetGroupPermUserId]", ClassType = typeof(User))]
        public virtual List<User> Users
        {
            get
            {
                return _collUsers;
            }
            set
            {
                _collUsers = value;
            }
        }
       
        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Mapping.Attributes;

namespace ....Persistence
{
    [Class(Table = "clients")]
    public class Client
    {
        #region Fields

        private int _iClientId;
        private string _strClientName;
        private string _strClientHouseNameNumber;
        private string _strClientStreetName;
        private string _strClientTown;
        private string _strClientCity;
        private string _strClientCounty;
        private string _strClientPostcode;
        private string _strClientCountry;

        #endregion

        #region Properties

        [Id(Name = "clientId")]
        [Generator(1, Class = "native")]
        public int ClientId
        {
            get { return _iClientId; }
            set { _iClientId = value; }
        }

        [Property(Column = "clientName")]
        public string ClientName
        {
            get { return _strClientName; }
            set { _strClientName = value; }
        }

        [Property(Column = "clientHouseNameNumber")]
        public string ClientHouseNameNumber
        {
            get { return _strClientHouseNameNumber; }
            set { _strClientHouseNameNumber = value; }
        }

        [Property(Column = "clientStreetName")]
        public string ClientStreetName
        {
            get { return _strClientStreetName; }
            set { _strClientStreetName = value; }
        }

        [Property(Column = "clientTown")]
        public string ClientTown
        {
            get { return _strClientTown; }
            set { _strClientTown = value; }
        }

        [Property(Column = "clientCity")]
        public string ClientCity
        {
            get { return _strClientCity; }
            set { _strClientCity = value; }
        }

        [Property(Column = "clientCounty")]
        public string ClientCounty
        {
            get { return _strClientCounty; }
            set { _strClientCounty = value; }
        }

        [Property(Column = "clientPostcode")]
        public string ClientPostcode
        {
            get { return _strClientPostcode; }
            set { _strClientPostcode = value; }
        }

        [Property(Column = "clientCountry")]
        public string ClientCountry
        {
            get { return _strClientCountry; }
            set { _strClientCountry = value; }
        }

        #endregion

    }
}



If you need anything else let me know.

Regards, Jean


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 10:50 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You have to add the classes or the assembly which contains the classes to the configuration. I don't know, if there es a special way for doing this when using attributes, but normally you do something like this:

Code:
cfg.AddAssembly("myassembly");
cfg.AddClass(typeof(myclass));


in the doc for NHibernate.Attributes there is:

Code:
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
NHibernate.Mapping.Attributes.HbmSerializer.Default.Validate = true; // Enable validation (optional)
// Here, we serialize all decorated classes (but you can also do it class by class)
cfg.AddInputStream( NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
System.Reflection.Assembly.GetExecutingAssembly() ); );
// Now you can use this configuration to build your SessionFactory..

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 2:55 pm 
Newbie

Joined: Thu Jul 26, 2007 6:58 am
Posts: 19
Thanks, that worked, but the next exception I am getting is:

Could not find a getter for property 'clientId' in class '....NHibernate.Client'

The Client Class (Which is the only one I have in this project at the moment) is:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Mapping.Attributes;

namespace .....NHibernate
{
    [Class(Table = "clients")]
    public class Client
    {
        #region Fields

        private int _iClientId;
        private string _strClientName;
        private string _strClientHouseNameNumber;
        private string _strClientStreetName;
        private string _strClientTown;
        private string _strClientCity;
        private string _strClientCounty;
        private string _strClientPostcode;
        private string _strClientCountry;

        #endregion

        #region Properties

        [Id(Name = "clientId")]
        [Generator(1, Class = "native")]
        public int ClientId
        {
            get { return _iClientId; }
            set { _iClientId = value; }
        }

        [Property(Column = "clientName")]
        public string ClientName
        {
            get { return _strClientName; }
            set { _strClientName = value; }
        }

        [Property(Column = "clientHouseNameNumber")]
        public string ClientHouseNameNumber
        {
            get { return _strClientHouseNameNumber; }
            set { _strClientHouseNameNumber = value; }
        }

        [Property(Column = "clientStreetName")]
        public string ClientStreetName
        {
            get { return _strClientStreetName; }
            set { _strClientStreetName = value; }
        }

        [Property(Column = "clientTown")]
        public string ClientTown
        {
            get { return _strClientTown; }
            set { _strClientTown = value; }
        }

        [Property(Column = "clientCity")]
        public string ClientCity
        {
            get { return _strClientCity; }
            set { _strClientCity = value; }
        }

        [Property(Column = "clientCounty")]
        public string ClientCounty
        {
            get { return _strClientCounty; }
            set { _strClientCounty = value; }
        }

        [Property(Column = "clientPostcode")]
        public string ClientPostcode
        {
            get { return _strClientPostcode; }
            set { _strClientPostcode = value; }
        }

        [Property(Column = "clientCountry")]
        public string ClientCountry
        {
            get { return _strClientCountry; }
            set { _strClientCountry = value; }
        }

        #endregion

    }
}


The names seem to be fine... also the columns' names in the DB looks alright. What am I missing?

Regards, Jean


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 3:22 pm 
Newbie

Joined: Thu Jul 26, 2007 6:58 am
Posts: 19
Hum..

I have set the column ame to Client... instead of client (as the C# Property and not like the DB).

[Property(Column = "clientName")] -> [Property(Column = "ClientName")]

It seems to be working now...!

Is there a way where I could have the column name in proper case (as in the DB)?

Cheers


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.