i get the exception message "Could not save object" while creating new Order. I am using many-to-one relation in Order.hbm.xml file.
I can create new Customer since there is no relation in that table.
Hibernate version: 1.2.1
Mapping documents:
<!-- Mapping for table Customer -->
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="HibernateDomain1"
assembly="HibernateDomain1"
schema="dbo">
<class
name="Customer"
table="Customer"
lazy="true">
<id
name="Id"
column="CustomerId" >
<generator
class="increment" />
</id>
<property
name="Name"
column="Name"
not-null="true"
/>
<bag
name="Items"
inverse="true"
lazy="true"
order-by="OrderDate"
cascade="all">
<key
column="CustomerId"/>
<one-to-many
class="HibernateDomain1.Order, HibernateDomain1"/>
</bag>
</class>
</hibernate-mapping>
<!-- Mapping for table Order -->
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="HibernateDomain1"
assembly="HibernateDomain1"
schema="dbo">
<class
name="HibernateDomain1.Order, HibernateDomain1"
table="Order"
dynamic-update="true">
<id
name="Id"
column="OrderId"
unsaved-value="0">
<generator
class="increment" />
</id>
<property
name="OrderDate"
column="OrderDate"
not-null="true"/>
<many-to-one
name="Customer"
column="CustomerId"
not-null="true" />
</class>
</hibernate-mapping>
[/b]
//Code for Creating new Order long customerId = 1; _sessions = new Configuration() .AddAssembly("HibernateDomain1") .BuildSessionFactory();
Order order = new Order(); order.OrderDate = DateTime.Now; using (ISession session = _sessions.OpenSession()) using (ITransaction tx = session.BeginTransaction()) { Customer customer = (Customer)session.Load(typeof(Customer), customerId); order.Customer = customer; customer.Items.Add(order); session.Save(order); tx.Commit(); session.Flush(); return true; }
Full stack trace of any exception that occurs:
[SqlException (0x80131904): Incorrect syntax near the keyword 'Order'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +925466
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +800118
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +31
System.Data.SqlClient.SqlDataReader.get_MetaData() +62
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1005
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +122
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +9
NHibernate.Id.IncrementGenerator.getNext(ISessionImplementor session) +460
NHibernate.Id.IncrementGenerator.Generate(ISessionImplementor session, Object obj) +87
NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything) +104
[ADOException: Could not save object]
NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything) +470
NHibernate.Impl.SessionImpl.Save(Object obj) +280
DataAccess.CreateOrder(Customer customer) in f:\DotNet\Lab\HibernateApp2\App_Code\DataAccess.cs:55
_Default.CmdCreateOrder_Click(Object sender, EventArgs e) in f:\DotNet\Lab\HibernateApp2\Default.aspx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
Name and version of the database : MS SqlServer 2005
Regards,
Prajwal Tuladhar
_________________ Intelligence Everywhere!!!
|