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.  [ 3 posts ] 
Author Message
 Post subject: NHibernate and postgresql mapping
PostPosted: Thu Apr 12, 2007 6:16 pm 
Newbie

Joined: Thu Apr 12, 2007 5:52 pm
Posts: 2
I downloaded NHibernate and think it is very promising. I'm a experienced user of PostgresSQL and so i want to use NHibernate with PostgreSQL.

Connecting works fine butting mapping failes, first the error then my code:

(I'm using a dutch version of Sharp Develop so some messages contain Dutch expressions).

Uitzondering NHibernate.MappingException werd in de debugger verworpen:
NHibernate.Users.hbm.xml(6,12): XML validation error: The 'table' attribute is not declared.堀ಸ


As you can see below, there is a table attribute in my "param" tag at line 6 just as the documentation tells.

---------------------------------------------------------------------------------

The code in my main class:
Code:
Configuration cfg = new Configuration();
cfg.Properties.Add("hibernate.connection.driver_class","NHibernate.Driver.NpgsqlDriver");
cfg.Properties.Add("hibernate.dialect","NHibernate.Dialect.PostgreSQLDialect");
cfg.Properties.Add("hibernate.connection.provider","NHibernate.Connection.DriverConnectionProvider");
cfg.Properties.Add("hibernate.connection.connection_string","Server=localhost;Database=xxxx;User ID=xxxx;Password=xxxx;Encoding=UNICODE");
cfg.AddAssembly("NHibernate");
cfg.AddClass(typeof(Users));
         
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
ISession currentSession = sessionFactory.OpenSession();
currentSession.Close();


Code in my User object:
Code:
namespace NHibernate
{
   public class Users
   {
      private int       id;
      private string    username;
      private string    password;

      public Users()
      {
      }
      
      public int Id
      {
         get { return id;}
         set { id = value; }
      }
      
      public string Username
      {
         get { return username;}
         set { username = value; }         
      }
      
      public string Password
      {
         get { return password; }
         set { password = value; }
      }
   }
}


My mapping file (referenced as embedded resource):
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="NHibernate.Users" table="users">
      <id name="Id" column="user_id">
         <generator class="sequence">
            <param table="sequence">users_user_id_seq</param>
         </generator>
      </id>
      <property name="Username" column="username">
      </property>
      <property name="Password" column="password">
      </property>
   </class>
</hibernate-mapping>


My table:
Code:
Column   |   Type                  |   Modifiers
---------------------------------------------------------------------------------------------------
user_id  | integer                 | not null default nextval('users_user_id_seq'::regclass);
username | character varying(25)   | not null
password | character varying(32)   | not null


----------------------------------------------------------------------------------

I hope to get to some solution soon so i can experiment any futher with NHibernate.

Thanks in advanced!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 3:45 am 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
You'll need 'name' instead of 'table' as the attribute for param. This is how I do it (also works for MySQL and SQL Server this way).

Code:
<id name="Id" column="siteid" type="Int32" unsaved-value="-1">
    <generator class="native">
        <param name="sequence">site_siteid_seq</param>
    </generator>
</id>


Setting the generator class to 'native' automatically uses sequences on PostgreSQL.

_________________
Cuyahoga


Top
 Profile  
 
 Post subject: Does't work either
PostPosted: Fri Apr 13, 2007 7:01 am 
Newbie

Joined: Thu Apr 12, 2007 5:52 pm
Posts: 2
Tried you're suggestion, where i hope i could eventually use the native generator because of it's portability. The following error occurred with the following code:

Uitzondering NHibernate.MappingException werd in de debugger verworpen:
Could not compile the mapping document: NHibernate.Users.hbm.xml


Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="NHibernate.Users" table="users">
      <id name="Id" column="user_id" type="Int32" unsaved-value="-1">
         <generator class="native">
            <param name="sequence">users_user_id_seq</param>
         </generator>
      </id>
      <property name="Username" column="username">
      </property>
      <property name="Password" column="password">
      </property>
   </class>
</hibernate-mapping>


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