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.  [ 12 posts ] 
Author Message
 Post subject: Ahh Going out of my mind... TRYING to make NHibernate work!!
PostPosted: Wed Oct 15, 2008 10:40 am 
Newbie

Joined: Wed Oct 15, 2008 8:46 am
Posts: 5
OK, this is messed up...
All those versions... and all those exceptions... with no proper documentation!!!
Ahhhh!

Here's my project (these are text files):

NHibernate_Test_01
|
|-> App.config
|
|-> User.cs
|
|-> User.hbm.xml
|
|-> program.cs


I'm using Microsoft's Visual Studio 2008 Professional with SP1.

I've used both the "NHibernate-1.2.1.GA-bin" and the "NHibernate-2.0.1.GA-bin" and I get different exceptions for each of them.

About the "Embedded Resource" issue:
User.hbm.xml
property Build Action == "Embedded Resource".

App.config
property Build Action == "Embedded Resource".



I get all kinds of exceptions when trying to make this work....
Changing versions in "App.config" or in "User.hbm.xml"... Trying to use "NHibernate.Dialect.MsSql2005Dialect"/"Dialect.MsSql2005Dialect"... whatever.... all the time I get some exception....

Right now I think the most advanced one is {"Could not compile the mapping document: NHibernate_Test_01.User.hbm.xml"} with the inner exception {"Could not find the dialect in the configuration"}.

So...

Could you please help me figure this one out...
Or send me a sample project of NHibernate that actually works!!!

Desperately waiting for your response!

- Sagi.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 4:06 pm 
Beginner
Beginner

Joined: Sun Oct 22, 2006 12:06 pm
Posts: 39
I feel your pain, You just must be carefull on these configurations.
For instance all hbm.xml files must be compiled as embedded sources. But take a look http://www.castleproject.org/activereco ... index.html

It's a more convient version on dealing with nhibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 2:16 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Please post your <hibernate-configuration> section from app.config.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 2:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Afaik, App.config shouldn't be "Embedded Resource". Set the build action to "None". And remember that there are breaking changes between the configuration of 1.2 and 2.0:

Quote:
# <nhibernate> section is ignored, using <hibernate-configuration> section (note that they have different XML formats)
# Configuration values are no longer prefixed by "hibernate.", if before you would specify "hibernate.dialect", now you specify just "dialect"

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2008 4:30 pm 
Newbie

Joined: Wed Oct 15, 2008 8:46 am
Posts: 5
cremor wrote:
Please post your <hibernate-configuration> section from app.config.


wolli wrote:
Afaik, App.config shouldn't be "Embedded Resource". Set the build action to "None". And remember that there are breaking changes between the configuration of 1.2 and 2.0:

Quote:
# <nhibernate> section is ignored, using <hibernate-configuration> section (note that they have different XML formats)
# Configuration values are no longer prefixed by "hibernate.", if before you would specify "hibernate.dialect", now you specify just "dialect"


O.K., my conclusion from these responses was that there needs to be a "<hibernate-configuration>" in "App.config" (wich I already kinda knew, since I used it in one of my tests... but now it's concrete for me) when using 2.0.1GA.. so here's an example I've found of that... and I'm using it in "App.config" instead of what is shown in the original file from my very first post...

Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>

    <!-- NHibernate Section -->
    <section
        name="hibernate-configuration"
        type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"
        />
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">
        Connection.DriverConnectionProvider
      </property>
      <property name="dialect">
        Dialect.MsSql2005Dialect
      </property>
      <property name="connection.driver_class">
        Driver.SqlClientDriver
      </property>
      <property name="connection.connection_string">
        Data Source=localhost;Initial Catalog=NHibernate_Test_01;Integrated Security=True;Pooling=False
      </property>
    </session-factory>
  </hibernate-configuration>
</configuration>


But then it throws the following exception:
{"Could not compile the mapping document: NHibernate_Test_01.User.hbm.xml"} with the inner exception {"Could not instantiate dialect class Dialect.MsSql2005Dialect"} which itself has an inner exception... {"Could not load type Dialect.MsSql2005Dialect. Possible cause: no assembly name specified.":""}.


And yeah, I know that it is necessary only for the "*.hbm.xml" files to be as "Embedded Resource" but it gave me a design-time error while "App.config" wasn't as "Embedded Resource"...

Oh, wait, actually.. now when I recheck... "App.config" wasn't set as "Embedded Resource" when I ran these tests.... hmmm... It wasn't set as "Embedded Resource" from the begining... but one time I did set it as "Embedded Resource" but now it's not, my mistake.

Anyways, I also changed the "User.hbm.xml" from what it was to this:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernate_Test_01.User, NHibernate_Test_01"
         table="Users">

    <id name="ID" column="LogonId" type="String(20)">
      <generator class="native" />
    </id>

    <property name="UserName" column="Name" type="String(40)"/>
    <property name="Password" type="String(20)"/>
    <property name="EmailAddress" type="String(40)"/>
    <property name="LastLogon" type="DateTime"/>
  </class>
</hibernate-mapping>



Oh, and yeah, all the exceptions come from "Configuration cfg = new Configuration();" which is found at the end of "Program.cs".


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 19, 2008 3:10 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
"NHibernate" was only removed from the property names. The values still have to be fully qualified class names. Try

NHibernate.Connection.DriverConnectionProvider

NHibernate.Driver.SqlClientDriver


http://nhforge.org/doc/nh/en/index.html#configuration-hibernatejdbc

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 19, 2008 5:50 pm 
Newbie

Joined: Wed Oct 15, 2008 8:46 am
Posts: 5
Oh my god, wolli, you are amazing!

That did it!!

I did what you said... then I had to add "virtual" to all my properties, then by the help of this thread I changed my entity named "Id" to "ID" (and that forced me to change it everywhere in the .cs of course.) because I had "ID" in the config file.


Well, that works! Finally!! Thank you!

cremor, thanks as well!!

pyhavaim, at the end I didn't need to use that.

Well,
on to some more... advanced exceptions! :)


By the way, is there a way to make this work with MS SQL Server 2008?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 9:07 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
By the way, is there a way to make this work with MS SQL Server 2008?


You can use Sql Server 2005 dialect. Works with 2008, too.

Quote:
then I had to add "virtual" to all my properties


Read about lazy-loading in the documentation. You onyl need this if you want to use it. You can turn lazy loading of by adding lazy="false" to the class element in the mapping.

Quote:
I changed my entity named "Id" to "ID"

Why that ? You can name the id property whatever you like as long as you have a matching property. You can also use different access strategies like nosetter.camelcase (which uses the property getter to read and the field behind the property to write) or "field" which directly reads and writes the field.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 11:26 am 
Newbie

Joined: Wed Oct 15, 2008 8:46 am
Posts: 5
Hmm.. Thank you for the info.


Anyways, right from where I took off last time... didn't change anything... and I get an "AssertionFailure Exception" which only says {"null identifier"} at this.session.Save(newUser); which is right above "Configuration cfg = new Configuration();" if you look for it in the .cs file.

Some more info....
At "StackTrace" it says...
Code:
   at NHibernate.Engine.EntityKey..ctor(Object identifier, String rootEntityName, String entityName, IType identifierType, Boolean batchLoadable, ISessionFactoryImplementor factory, EntityMode entityMode)
   at NHibernate.Engine.EntityKey..ctor(Object id, IEntityPersister persister, EntityMode entityMode)
   at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   at NHibernate_Test_01.Program.AddNewUser() in C:\Users\Sagi\Documents\NHibernate_Test_01\NHibernate_Test_01\Program.cs:line 99
   at NHibernate_Test_01.Program.Main(String[] args) in C:\Users\Sagi\Documents\NHibernate_Test_01\NHibernate_Test_01\Program.cs:line 27
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()



And at "TargetSite" it says...
Code:
{Void .ctor(System.Object, System.String, System.String, NHibernate.Type.IType, Boolean, NHibernate.Engine.ISessionFactoryImplementor, NHibernate.EntityMode)}



"Source" is "NHibernate".

"Message" is "null identifier".

And at "Data" it says "{System.Collections.ListDictionaryInternal}".


Any ides for this one?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 11:43 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
<id name="ID" column="LogonId" type="String(20)">
<generator class="native" />
</id>

Does natvie work with a string column ? I'm not sure about this. Can you post the code where you save the user ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 3:30 am 
Newbie

Joined: Wed Oct 15, 2008 8:46 am
Posts: 5
Hey, you were right spot on.

I changed the "native" to "assigned" and it did it...

It was originally at "assigned" but I thought to change it at some point for some reason to make this work....

Well, it's free from errors right now, hope I won't run into any more...

Thank you so much for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2008 10:36 am 
Newbie

Joined: Sun Jan 13, 2008 6:48 am
Posts: 16
Hi HowDH311DoUUseThisNH,

i have experienced for myself the (partially day-long) pain of getting nhibernate to work. Thus, i can supply you with a few links, which will help you to get it up and running.

http://www.beansoftware.com/asp.net-tut ... g4net.aspx
http://www.tobinharris.com/2007/2/3/nhibernate-faq
http://www.tobinharris.com/tags/nhibernate



Hope, that helps...


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