-->
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.  [ 6 posts ] 
Author Message
 Post subject: Pls help me this problem, it's urgent.
PostPosted: Sun May 13, 2007 11:30 pm 
Newbie

Joined: Tue Mar 13, 2007 1:01 am
Posts: 14
Hi everybody,

I have 2 tables : Departments and Staffs
Departments : DepartmentID, Name
Staffs : StaffID, Name, DepartmentID

When I use some templates to create my mapping and persistent files.
I receive the following mapping file :

<property name="StaffID" column="StaffID" type="int" not-null="true" />
<property name="StaffName" column="StaffName" type="string" not-null="true" />

<many-to-one name="Department" column="DepartmentID " />

And here is persistent class file :

protected int _staffID;
protected string_name;
protected Department _department;

protected int StaffID
{
get {return this._staffID;}
set {this._staffID=value;}
}

protected int Name
{
get {return this._name;}
set {this._name=value;}
}

public virtual Departments Department
{
get { return _department; }
set {_department = value; }
}

I want to update or insert a new staff :

Staffs st=new Staffs();

st.StaffID = 1;
st.StaffName = "Test";

How can I update DepartmentID for this staff. Because in persistent class does not have DepartmentID property. It just has Department - I think this is the Departments class.

Pls help me do this.

Thanks so much.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 2:29 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Um...Update the 'ID' property in the Department class.
'DepartmentID' is not a property in an object but rather a column in your DB table. This tells NHibernate which Department values to hydrate an Department for your Staff class.

NHibernate is creating an association to an object called Department.

BTW...unless you are constrained to do so, I'd avoid using the assignment strategy for ID fields and instead let the db handled the constraints/associations with, say, autoincrementing fields.

But maybe you have some constraint not allowing this.

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 2:35 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
Why don't you use a regular key for mapping? It could look like the following:

Code:
<id name="StaffId" type="Int32">
   <generator class="assigned" />
</id>
<property name="StaffName" column="StaffName" type="string" not-null="true" />
<many-to-one name="Department" column="DepartmentID" />


Also you don't need operate the dept id, do you? Let the NH do it for you. Is there any reason for doin it your way?

BTW - there is a space character in your mapping for the dept id column...

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 5:09 am 
Newbie

Joined: Tue Mar 13, 2007 1:01 am
Posts: 14
Thanks for your response!

I am newbie in Nhibernate so I do not understand the data is inserted or updated.

For example :

I'd like to insert a new staff I'll do that :

Staffs st=new Staffs();

I can set :

st.StaffID = 1;
st.StaffName = "Test";

How about field DepartmentID for this staff. How can I set value for it?
Because I can not set value for it normally :

st.DepartmentID = dropdownlist1.SelectedValue; // It does not have DepartmentID property.

Could you give me the explanation for this.

Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 7:54 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
pcphuc wrote:
How about field DepartmentID for this staff. How can I set value for it?
Because I can not set value for it normally


You don't need the ID. Assuming you have a persisted instance of Department, you simply assign it like the following:

Code:
Department dept = new Department();
sess.Save(dept);

st.Department = dept;
sess.Save(st);


Quote:
st.DepartmentID = dropdownlist1.SelectedValue; // It does not have DepartmentID property


You will do it this way:
Code:
st.Department = dropdownlist1.SelectedValue as Department;


Hope this helps

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject: I think I got your question.
PostPosted: Wed May 16, 2007 9:54 am 
Newbie

Joined: Wed May 16, 2007 7:39 am
Posts: 5
The way I would think of solving this problem is to have one other property called DepartmentId that comes handy to me to just set the DepartmentId (just the value and not the whole entity itself - saves me from loading the Department if I dont have one in the cache). And mark the Department property with update="false" insert="false".

<property name="StaffID" column="StaffID" type="int" not-null="true" />
<property name="StaffName" column="StaffName" type="string" not-null="true" />
<property name="DepartmentID" column="DepartmentID" type="string" not-null="true" />

<many-to-one name="Department" column="DepartmentID" class="Depart, ..." update="false" insert="false" />

By this you achieve twin benefits:
1. No need to have whole Department Entity to update/save.
2. You can get the whole Department while reading the Staff.

Hope this helps!


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