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.  [ 4 posts ] 
Author Message
 Post subject: Hi,Nhibernate
PostPosted: Mon Jan 28, 2008 7:09 am 
Newbie

Joined: Sun Jan 27, 2008 4:26 pm
Posts: 18
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hi, When i try to get data from table i am getting all the columns and data except foreign key column and data.I am not getting error but i couldnot get foreign key column.And when i try to inser data data to the table i am getting error like cannot insert data int to DVBStatus.I know i can get error like this.but how can insert data into this.Could you please help.If you tell me the solution it would be great.I have been working on this since 3 days.

Hibernate version:
1.2.1GA
My DVBChannel Class
namespace FreeView
{
public class DVBChannel
{
private int dVBChannelIDField;
private int dVBChannelNoField;
private string dVBChannelNameField;
private string dVBChannelURLField;
private string ePGDataFileField;
private string descriptionField;
private FreeView.DVBStatus DVBStatus1 = null;

public DVBChannel()
{

}
public virtual int DVBChannelID
{
get
{
return this.dVBChannelIDField;
}
set
{
this.dVBChannelIDField = value;
}
}
public virtual int DVBChannelNo
{
get
{
return this.dVBChannelNoField;
}
set
{
this.dVBChannelNoField = value;
}
}
public virtual string DVBChannelName
{
get
{
return this.dVBChannelNameField;
}
set
{
this.dVBChannelNameField = value;
}
}
public virtual string DVBChannelURL
{
get
{
return this.dVBChannelURLField;
}
set
{
this.dVBChannelURLField = value;
}
}

private int statusid;

//[ManyToOne(Column = "DVBStatusId", NotNull = true)]
public virtual FreeView.DVBStatus DVBStatusID
{
get
{
return DVBStatus1;

}
set
{
DVBStatus1 = value;


}
}

public virtual string EPGDataFile
{
get
{
return this.ePGDataFileField;
}
set
{
this.ePGDataFileField = value;
}
}
public virtual string Description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}
}
}

DVBStatus Class
namespace FreeView
{
public class DVBStatus
{
private int DVBStatusIDField;
private string DescriptionField;
private ISet DVBChannellist = new HashedSet();
public DVBStatus()
{

}

public virtual int DVBStatusID
{
get
{
return this.DVBStatusIDField;
}
set
{
this.DVBStatusIDField = value;
}
}
public virtual string Description
{
get
{
return this.DescriptionField;
}
set
{
this.DescriptionField = value;
}
}
public virtual ISet DVBChannel
{
get { return this.DVBChannellist; }
set
{
this.DVBChannellist = value;
}
}
}
}

Mapping documents:
DVBChannel Mapping
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="FreeView" assembly="FreeView" default-access="property">
<class name="DVBChannel" lazy="false" table="DVBChannel" >
<id name="DVBChannelID" column="DVBChannelID" type="int">
<generator class="increment"></generator>
</id>
<property name="DVBChannelNo" column="DVBChannelNo" type="int"/>
<property name="DVBChannelName" column="DVBChannelName" type="string"/>
<property name="DVBChannelURL" column="DVBChannelURL" type="string"/>
<many-to-one name="DVBStatusID" class="DVBStatus" column="DVBStatusID" cascade="all" not-null="true" insert="false" update="false" />
<property name="EPGDataFile" column="EPGDataFile" type="string"/>
<property name="Description" column="Description" type="string"/>
</class>

</hibernate-mapping>
DVBStatus Mapping

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="FreeView" assembly="FreeView" default-access="property">
<class name="DVBStatus" table="DVBStatus" lazy="false" >
<id name="DVBStatusID" column="DVBStatusID" type="int">
<generator class="increment"></generator>
</id>
<property name="Description" column="Description" type="string" />
<set name="DVBChannel" cascade="all" lazy="true" inverse="true" >
<key column="DVBStatusID"/>
<one-to-many class="DVBChannel"/>
</set>
</class>
</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():
This is getting data from table

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
cfg.AddAssembly("FreeView");
NHibernate.ISessionFactory factory = cfg.BuildSessionFactory();
NHibernate.ISession session = factory.OpenSession();
NHibernate.ITransaction trns = session.BeginTransaction();
IList user1 = session.CreateCriteria(typeof(DVBChannel)).List();
GridView1.DataSource = user1;
GridView1.DataBind();
trns.Commit();
session.Close();

This is inserting data from the table
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
cfg.AddAssembly("FreeView");
NHibernate.ISessionFactory factory1=cfg.BuildSessionFactory();
NHibernate.ISession session1 = factory1.OpenSession();
NHibernate.ITransaction trns1 = session1.BeginTransaction();

DVBChannel channel = new DVBChannel();
DVBStatus status = new DVBStatus();
session1.Lock(status, NHibernate.LockMode.None);

channel.DVBChannelNo = 20;
channel.DVBChannelName = "Mana Telugu";
channel.DVBChannelURL = "http://ManaTelugutv.com/";
status.AddChannel(channel);
channel.DVBStatusID = 1;
channel.EPGDataFile = "ManaTelugu.aspx";
channel.Description = "This is Mana Telugu Channel";
session1.SaveOrUpdate(channel);
trns1.Commit();
session1.Close();
Response.Write("The Values Saved Succesfully");

Full stack trace of any exception that occurs:

Name and version of the database you are using:
sqlserver 2005

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 5:35 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
When you use a many-to-one, you are mapping the reference as the ID, but you should reference the actual object in your class. This seems to be confused in your code. The type on the property is the DVBStatus, but you named it DVBStatusID, and in your insert code you have
Code:
channel.DVBStatusID = 1;

which doesn't make any sense--you should be setting the property to a DVBStatus instance.


Top
 Profile  
 
 Post subject: Hi
PostPosted: Tue Jan 29, 2008 7:00 am 
Newbie

Joined: Sun Jan 27, 2008 4:26 pm
Posts: 18
marcal wrote:
When you use a many-to-one, you are mapping the reference as the ID, but you should reference the actual object in your class. This seems to be confused in your code. The type on the property is the DVBStatus, but you named it DVBStatusID, and in your insert code you have
Code:
channel.DVBStatusID = 1;

which doesn't make any sense--you should be setting the property to a DVBStatus instance.



Thanks for ur help.
And i solved that problem.Now i Could insert the datat into table.But I could not get foreignkey column(DVBStatusID) in DVBChannel table when i try to get table data from database which i am bounding to gridview..


Top
 Profile  
 
 Post subject: Re: Hi
PostPosted: Thu Jan 31, 2008 7:43 am 
Newbie

Joined: Sun Jan 27, 2008 4:26 pm
Posts: 18
sandy.tineti wrote:
marcal wrote:
When you use a many-to-one, you are mapping the reference as the ID, but you should reference the actual object in your class. This seems to be confused in your code. The type on the property is the DVBStatus, but you named it DVBStatusID, and in your insert code you have
Code:
channel.DVBStatusID = 1;

which doesn't make any sense--you should be setting the property to a DVBStatus instance.



Thanks for ur help.
And i solved that problem.Now i Could insert the datat into table.But I could not get foreignkey column(DVBStatusID) in DVBChannel table when i try to get table data from database which i am bounding to gridview..


Hi any one can u please help me regarding my problem which i posted.I could not get foreignkey column and that data when i try to get table data from database and i am bounding that data to gridview.I am new to nhibernate.i could not find much information about this and anywhere in googgle.If you can help it would be grateful for me.All my code is above there which i posted on 3 days back.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.