-->
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: Could not save object for a many-to-one relation
PostPosted: Thu Jan 24, 2008 4:22 am 
Newbie

Joined: Mon Jan 14, 2008 9:58 am
Posts: 2
Location: Kathmandu, Nepal
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!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 12:45 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
If I had to guess without seeing the SQL, I'd say it's because order is a reserved word in SQL.

Change your mapping to:

<class
name="HibernateDomain1.Order, HibernateDomain1"
table="[Order]"
dynamic-update="true">

Also, make sure show_sql is set to try in your configuration file and then attempt to manually run the generated SQL in SQL Server Magement Studio (or your favorite query tool).

I have a similar problem with a table called User (in hindsight, I should have called it Users). User is a reserved word for sql server and so I need to map it as table=[User]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 4:03 am 
Newbie

Joined: Mon Jan 14, 2008 9:58 am
Posts: 2
Location: Kathmandu, Nepal
benhyrman wrote:
If I had to guess without seeing the SQL, I'd say it's because order is a reserved word in SQL.

Change your mapping to:

<class
name="HibernateDomain1.Order, HibernateDomain1"
table="[Order]"
dynamic-update="true">

Also, make sure show_sql is set to try in your configuration file and then attempt to manually run the generated SQL in SQL Server Magement Studio (or your favorite query tool).

I have a similar problem with a table called User (in hindsight, I should have called it Users). User is a reserved word for sql server and so I need to map it as table=[User]

***************************************
I think u r right. Thanks for ur reply.

_________________
Intelligence Everywhere!!!


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.