-->
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: session.save(obj) -- entity not found Error
PostPosted: Fri Jan 25, 2008 8:55 am 
Newbie

Joined: Fri Jan 25, 2008 6:18 am
Posts: 10
Location: Bangalore, India
Hi,

I am new to NHibernate, and trying to implement it in my project. While I am trying to create a small application from quickstart. I am getting an error at session.Save(obj). I am doing it in VS 2005.

I changed the myobj.nbt.xml file properties to "Embedded Resource", even though the same error is comming.


please help me, this will make to learn and use new technology in my project.

Here is my Code.

//Consol application class file

using System;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace HibernateTest
{
public class Program
{
static void Main(string[] args)
{
Configuration cfg = new Configuration();
cfg.AddAssembly("HibernateTest");

ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();

HibernateTest.User newUser = new HibernateTest.User();
newUser.Id = "joe_cool";
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "joe@cool.com";
newUser.LastLogon = DateTime.Now;

// Tell NHibernate that this object should be saved
session.SaveOrUpdate(newUser);

// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();
System.Diagnostics.Debug.WriteLine("User Added");
}
}
}

//Entity Class File

using System;
using System.Collections.Generic;
using System.Text;

namespace HibernateTest
{

public class User
{
private string id;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;


public User()
{
}

public virtual string 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; }
}

public string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}

public DateTime LastLogon
{
get { return lastLogon; }
set { lastLogon = value; }
}

}

}


//MyObj.nbt.xml File

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="User" table="users">
<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>

//app.config.file

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

<nhibernate>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Data Source=B4B-2F-258-8GVX\SQLEXPRESS;Initial Catalog=NHibernate;Integrated Security=True;Pooling=False"
/>
</nhibernate>
</configuration>

_________________
Thanks & Regards
Babar Shaik.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 9:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
For entities that have "assigned" ID generator, SaveOrUpdate() will always do an Update(). Use Save() instead.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 1:02 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
you can also use the version element in your mapping file to add support for automated saving/updating in instances where your id is assigned.

Change your mapping file to:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="User" table="users">
<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>

<version name="Version" access="nosetter.camelcase" unsaved-value="0" />

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

Add an int column to your table called version

and add an int field to your class:

public class User
{
private string id;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;

private int version;

public int Version
{
get {return version; }
}
.....
}

Then you can still just call SaveOrUpdate. NHibernate will rely on the value of version to determine if it should issue a Save or Update.

Granted, this does start to blur the lines between your data model and your domain model, but I personally don't mind the intrusion and find that it helps in many ways with optimistic locking and persisting objects with assigned keys.


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.