-->
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: Session.save is not working Fine
PostPosted: Wed Sep 09, 2009 8:14 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
Hi,

I made a small application in Nhibernate..

my table is:
CREATE TABLE [dbo].[UserInterests](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] ,
[InterestID] [int]
) ON [PRIMARY]
and FYI this is junction table

This is my class


public class UserInterests
{
private int _Id;
private int _userid;
private IList<UserInterests> _UserInterestsList;
private IList<UserInterests> _UserInterestsList2;
private int _interestid;
}


This is my Mapping File

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DTO" namespace="Business.Entities">

<class name="Business.Entities.UserInterests" table="UserInterests" >

<id name="Id" column="Id" type="Int32" unsaved-value="null">
<generator class="identity" />
</id>


<!-- composite primary key support is touchy. View the documentation for syntax. -->

<bag name="UserInterestsList" inverse="true" lazy="true" cascade="all">
<key column="UserId" />
<one-to-many class="UserInterests" />

</bag>

<bag name="UserInterestsList2" inverse="true" lazy="true" cascade="all" >
<key column="InterestID" />
<one-to-many class="UserInterests" />

</bag>

</class>
</hibernate-mapping>





and i am unable to save session with proper values
public void addUserInterest(UserInterests userInterest)
{
try
{
session.Save(userInterest);
session.Flush();
}
catch (Exception ex)
{
throw ex;
}
}



but unfortunately Sesseion.save() didn't save data into the table... even though while debugging UserInterest contains all the data but after execution... i cant find data in the table.
and got
1 Null Null
2 Null Null
3 Null Null
4 Null Null
5 Null Null

just identity is working and rest of records didnt get there .... Dont know why; Can some body ans me?


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Wed Sep 09, 2009 8:26 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You've marked your associations as inverse, but you don't have the according many-to-one association. So your associations have no owner and therefore will not be saved.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:16 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
i did inverse = false but it didnt effect at output :(
what to do ... can you plz help me out ?


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:16 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
Quote:
...


Last edited by asfandtnt on Thu Sep 10, 2009 2:20 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:19 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Are their really objects inside the bags ? Can you post the code where you create the object ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:24 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
This is from where i calling it.... in a code behind file

protected void LinkButtonUserInterests_Click(object sender, EventArgs e)
{


UserInterests UserInter = new UserInterests();
UserInter.UserId = 9;
UserInter.Interestid = 2;

DAL.DAL pub = new DAL.DAL();
pub.addUserInterest(UserInter);


}

This is my model


using System;
using System.Collections.Generic;


namespace Business.Entities
{
[Serializable]
public class UserInterests
{
#region Private Members

private int _Id;
private int _userid;
private IList<UserInterests> _UserInterestsList;
private IList<UserInterests> _UserInterestsList2;
private int _interestid;
#endregion
#region Public Properties

public virtual int Id
{
get
{
return _Id;
}
set
{
_Id = value;
}

}


public virtual int UserId
{
get
{
return _userid;
}
set
{
_userid = value;
}

}

public virtual IList<UserInterests> UserInterestsList
{
get
{
return _UserInterestsList;
}
set
{
_UserInterestsList = value;
}
}
public virtual IList<UserInterests> UserInterestsList2
{
get
{
return _UserInterestsList2;
}
set
{
_UserInterestsList2 = value;
}
}

public virtual int Interestid
{
get
{
return _interestid;
}
set
{
_interestid = value;
}

}



#endregion

#region Public Functions

public virtual void AddUserInterests(UserInterests obj)
{
#region Check if null
if (obj == null)
throw new ArgumentNullException("obj", "El parametro no puede ser nulo");
#endregion

_UserInterestsList.Add(obj);
_UserInterestsList2.Add(obj);
}




#endregion //Public Functions



}
}



This is my XML File

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DTO" namespace="Business.Entities">

<class name="Business.Entities.UserInterests" table="UserInterests" >

<id name="Id" column="Id" type="Int32" unsaved-value="null">
<generator class="identity" />
</id>


<!-- composite primary key support is touchy. View the documentation for syntax. -->

<bag name="UserInterestsList" inverse="false" lazy="true" cascade="all">
<key column="UserId" />
<one-to-many class="UserInterests" />
</bag>
<bag name="UserInterestsList2" inverse="false" lazy="true" cascade="all" >
<key column="InterestID" />
<one-to-many class="UserInterests" />
</bag>

</class>
</hibernate-mapping>



This is DAL inside code

public void addUserInterest(UserInterests userInterest)
{
try
{
session.Save(userInterest);
session.Flush();
}
catch (Exception ex)
{
throw ex;
}
}


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:31 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What do you expect from this code ? In your mapping file, you only have the id and the bags. Since you don't add any objects to the bag and you don't add the object itself to any other UserInterest, hibernate stores exactly what's there: the id and two NULLs

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:46 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
Infect i am very new to Nhibernate....
man Can you pleaseee Fix this code Once...
I am just learning this stuff and book is prety tough....
i beg of plz help me out...


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:48 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
its been a month in Nhibernate and i am no where. :(


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 2:57 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
plz spare some time or minimum kindly send me some example code for this, or a small application made in Nhibernate


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 3:10 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look here:

http://nhforge.org
http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx
http://nhforge.org/doc/nh/en/index.html#example-parentchild

and I suggest a book like "NHibernate in Action" (http://www.manning.com/kuate/)

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Session.save is not working Fine
PostPosted: Thu Sep 10, 2009 3:14 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
Thanks man...
I agree that is the right direction...
perhaps it will take long time but ultimately i will get strong grip on it..
i got that book too..
Thanks for you time and help...
Regards..
Asfand


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.