-->
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.  [ 1 post ] 
Author Message
 Post subject: Constructing and Saving objects. JSON Style App
PostPosted: Thu May 14, 2009 12:56 am 
Newbie

Joined: Thu May 14, 2009 12:21 am
Posts: 1
This is my first post and I don't have much experience in ORM/nHibernate.

I am comfortable with setting up and Configuring nHibernate now. But I have a Question - How is the best way to create a new Object that has Child Objects?

I have included a simplified example of what I'm trying to do below.

Objects
UsersDetails
Guid UsersDetailsId
Users Users (Relationship with users on UsersId)
String UsersDetailsString

Users
Guid UsersId
string UserName

Here is how I am currently trying to Add my new UsersDetails object to the DB.

Users ThisUser = MyUsersDal.Getusers(TheUsersID)

UsersDetails ThisUsersDetails = new UsersDetails(ThisNewGuid, ThisUser, ThisString);

Guid ThisUsersDetailsId = MyUsersDetailsDal.AddUsersDetails(ThisUsersDetails);

public int AddUsersDetails(UsersDetails usersdetails, bool NonExtension)
{
using (ISession session = GetSession())
{
int NewID = (int)session.Save(usersdetails);
session.Flush();
return NewID;
}
}


This was the way I assumed I would do it as all I have is the UsersId (which is passed from the frontend)!?!

because I'm Using a JSON type front end application the other option would be to construct the Object in JSON on the Client Side and then pass back a JSON string in the queryString. I'd then deserialize it on the server and pass this Object to my Dal to save. But this seems to have many pit falls to me as the object will need to be cobbled together by client side code??

The other way that I was considering was to have two Objects - One usersDetails with Child Objects.... and then another "Non Mapped" version with just the Id's (Example below).

Objects
UsersDetails
Guid UsersDetailsId
Guid UsersId (Relationship with users on UsersId)
String UsersDetailsString

Users
Guid UsersId
string UserName

Much easier to add my new UsersDetails object to the DB.

UsersDetails ThisUsersDetails = new UsersDetails(ThisNewGuid, MyUsersID, ThisString);

Guid ThisUsersDetailsId = MyUsersDetailsDal.AddUsersDetails(ThisUsersDetails);

public int AddUsersDetails(UsersDetails usersdetails, bool NonExtension)
{
using (ISession session = GetSession())
{
int NewID = (int)session.Save(usersdetails);
session.Flush();
return NewID;
}
}

I am trying to get my head around how to construct my new object and it would be much easier without the Child Objects mapped as I can just pass in the Related child ID's into the constructor..... But I lose out then when I need to display as I'd have to get the User and the UsersDetails objects separately.

Any help/advice will be much appreciated.
Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.