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: Getting started - first obstacle...
PostPosted: Tue Nov 11, 2008 1:02 am 
Newbie

Joined: Mon Nov 10, 2008 4:03 pm
Posts: 11
Hope I have this all correct...

2.0.1GA:

mapping file:
<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2">
<class name="MyProject.Contact, MyProject" table="contact" schema="dbo">
<id name="Id" access="property" column="id" type="Int32" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="Datecreated" access="property" type="Timestamp">
<column name="datecreated" not-null="true" />
</property>
<property name="Datemodified" access="property" type="Timestamp">
<column name="datemodified" not-null="true" />
</property>
<property name="IdOrg" access="property" type="Int32">
<column name="id_org" not-null="true" />
</property>
<property name="IdGroup" access="property" type="Int32">
<column name="id_group" />
</property>
<property name="Company" access="property" type="String">
<column name="company" />
</property>
<property name="Keywords" access="property" type="String">
<column name="keywords" />
</property>
<property name="Url" access="property" type="String">
<column name="url" />
</property>
<property name="IdPhoneDefault" access="property" type="Int32">
<column name="id_phone_default" />
</property>
<property name="IdEmailDefault" access="property" type="Int32">
<column name="id_email_default" />
</property>
<property name="IdAddressDefault" access="property" type="Int32">
<column name="id_address_default" />
</property>
<property name="Notes" access="property" type="StringClob">
<column name="notes" />
</property>
<bag name="Persons" access="property" table="person" lazy="false">
<key column="id_contact" />
<one-to-many class="MyProject.Person, MyProject" />
</bag>
</class>
</hibernate-mapping>


NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();

NHibernate.ISessionFactory sessionFactory = cfg.BuildSessionFactory();

NHibernate.ISession session = sessionFactory.OpenSession();

Contact myContact = new Contact();
myContact.Company = "My first test with NHibernate.";
myContact.Notes = "If this works, I will be amazed!";

session.BeginTransaction();
session.Save(myContact); // <----- crashes here!
session.Close();


Stack Trace:


[MappingException: Unknown entity class: MyProject.Contact]
NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type theClass) +176
NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass) +86
NHibernate.Impl.SessionImpl.GetEntityPersister(Object obj) +62
NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) +60
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) +104
NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) +83
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event) +260
NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) +200
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) +398
NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event) +121
NHibernate.Impl.SessionImpl.Save(Object obj) +73
MyProject._Default.Page_Load(Object sender, EventArgs e) in C:\inetpub\ASPNET\MyProject\Default.aspx.cs:24
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627




MS-SQL Express 2005


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 10:41 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post your configuration ? You have to either add the resources (assembly or mapping) in the configuration or in code.

code:
Code:
cfg.AddAssembly(...)
cfg.AddResource(...)
cfg.AddFile(...)


config:
Code:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
...
<!-- mapping files -->
<mapping resource="NHibernate.Auction.Item.hbm.xml" assembly="NHibernate.Auction" />
<mapping resource="NHibernate.Auction.Bid.hbm.xml" assembly="NHibernate.Auction" />
</session-factory>
</hibernate-configuration>


If you have done any of these, check if the build action is "Embedded Resource".

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 4:41 pm 
Newbie

Joined: Mon Nov 10, 2008 4:03 pm
Posts: 11
Buld action is set ok. Do I have to do all three things in code: cfg.AddAssembly()... addresource... and addfile? Or just one of them?

Since I'm using ActiveRecord and ActiveWriter and one or both re-writes files, I guess would rather do in code. My config is as follows per your request. Also, do you know of a good (updated) project example in C# which uses NH (and optimally AR/AW)?




<?xml version="1.0"?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>

</configSections>
<appSettings/>
<nhibernate isWeb="true">
<!--<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/> -->

<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.connection.connection_string" value="Data Source=.\SQLEXPRESS;Initial Catalog=myDb;Integrated Security=True"/>
<add key="hibernate.bytecode.provider" value="null"/>
</nhibernate>
.
.
.


Top
 Profile  
 
 Post subject: Getting started - first obstacle...
PostPosted: Wed Nov 12, 2008 4:58 pm 
Newbie

Joined: Mon Nov 10, 2008 4:03 pm
Posts: 11
I'm a bit further along as I've been workiing trying to get things going until I get responses from here.

My current status:

I've modified the C# code as follows:

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();

cfg.Configure();

// Add class mappings to configuration object
Assembly thisAssembly = typeof(Salutation).Assembly;
cfg.AddAssembly(thisAssembly);

// Create session factory from configuration object
//m_SessionFactory = cfg.BuildSessionFactory();

NHibernate.ISessionFactory sessionFactory = cfg.BuildSessionFactory();
NHibernate.ISession session = sessionFactory.OpenSession();

Salutation mySalutation = new Salutation();
mySalutation.Description = "Mr.";
session.BeginTransaction();
session.SaveOrUpdate(mySalutation); // <---- crashes here (see error below)
session.Transaction.Commit();



Current error message...

GenericADOException was unhandled by user code

could not insert: could not insert: [MyProject.Salutation][SQL: INSERT INTO dbo.salutation (description) VALUES (?); select SCOPE_IDENTITY()]


I've checked the database and table. It's a simple table with 2 fields:

1. id (identity, integer, non-null)
2. description (nvarchar(50), nullable)

I get the same error when trying more complex tables.

Stack trace:

[SqlException (0x80131904): Invalid object name 'dbo.salutation'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1364526
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +57
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +201
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, TdsParserStateObject stateObj) +2503
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +76
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +288
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +949
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +240
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +20
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +145
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +12
NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd) +75
NHibernate.Id.InsertSelectDelegate.ExecuteAndExtract(IDbCommand insert, ISessionImplementor session) +84
NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder) +117

[GenericADOException: could not insert: [WebCCMS.Salutation][SQL: INSERT INTO dbo.salutation (description) VALUES (?); select SCOPE_IDENTITY()]]
NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder) +298
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session) +358
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session) +404
NHibernate.Action.EntityIdentityInsertAction.Execute() +178
NHibernate.Engine.ActionQueue.Execute(IExecutable executable) +242
NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) +930
NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) +671
NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) +339
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) +103
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event) +259
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) +217
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) +398
NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) +116
NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) +68
WebCCMS._Default.Page_Load(Object sender, EventArgs e) in C:\inetpub\ASPNET\WebCCMS\Default.aspx.cs:37
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


Top
 Profile  
 
 Post subject: figured it out!
PostPosted: Thu Nov 13, 2008 1:54 am 
Newbie

Joined: Mon Nov 10, 2008 4:03 pm
Posts: 11
Thanks for the help posted and to those of you who might be pondering this, but haven't yet responded.

I have figured out the problem and have gotten things to work. One issue was that I was using NH config settings in both the web.config and in hibernate.cfg.xml file. It appears the hibernate.cfg.xml takes precedence and in it was the problem -- part of the connection string (the database) was incorrect as I had copied from another project and failed to update.

I'm still not clear on everything such as the use of "cfg.AddAssembly()... addresource... and addfile" - when and what of those to use. I found a working ASP.NET example online (http://www.codeproject.com/KB/dotnet/my ... rnate.aspx) and it doesn't have any of those; I don't know why. I my project I had to use just the "cfg.AddAssembly", else it wouldn't run; it didn't know about the module.

If anyone can shed more light on some of this, please fire away.

thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 4:21 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
cfg.AddAssembly(...): all hbm.xml that are embedded resources in the assembly are added to the configuration
cfg.AddFile(...): the hbm.xml file is added to the configuration
cfg.AddResource(...): not sure about this one

You can use any combination of these methods, e.g. add all mappings from an assembly plus some explicit mapping files from a directory (e.g. customer specific mappings).

You can also specify the mappings in the configuration as mentioned in my previous post. And again you can mix it with the calls above.

I don't think that mixing these possibilities too much makes a lot of sense. You have to decide whats the best for your setup.

_________________
--Wolfgang


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.