-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Persistent object serialization & multi-layered architec
PostPosted: Wed Apr 21, 2004 1:48 pm 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
Hello,

My question is where I can find a specification of how exactly
object networks are serialized (when sent over RMI).

My real problem is that I am planning to use Hibernate in multi-tired architecture:
- Presentation (web)
- Business (EJB)
- Session Facade
- ...
- Hibernate
- Database

The question is how and in what form do I transport objects between presentation and business layers.

There are basicly two options:
1. Create a parallel structure of Data Transfer Objects (DTO) and a set of helpers to convert from Hibernate Domain Model Objects (DMO) to DTO and back.
2. Pass DMO's directly.
The first option is standard (in CMP/JDBC world), but has this annoying feature of having to enter the same information twice (DTO & DMO).

In order to evaluate the second option I need to know how serialization of DMO networks works.

Finally, I have one idea I would like to get comments about:
----------------
The basic difference between DTO & DMO is that DMO has relations, whereas DTO usually keeps just foreign key.

This brought me to idea of avoiding using Hibernate relations and map DTO's theirselves to the tables. In this case I would have to use Hibernate ehanced SQL (but not HQL), because Hibernate cannot use information about relations any more.

What do You think of such solution?
-------------

Finally, a couple of proposals:
- Add serialization section to Hibernate manual
- Write a systematic account of multi-layered architecture which explores these issues.

There are some docs, but I think they are not sufficient:
http://www.hibernate.org/124.html
Most of examples are oriented towards a situation when business layer and presentation layer are in the same JVM.

With best wishes,
Oleg M


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 3:10 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
2. is definetly the thing to do
and not using relation in hibernate sounds really strance, like driving the highway using a 747 (a non-sense).
What is you pb about serialization ? Any specific questions ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 11:27 am 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
Hi!

emmanuel wrote:
2. is definetly the thing to do and not using relation in hibernate sounds really strance, like driving the highway using a 747 (a non-sense). What is you pb about serialization ? Any specific questions ?


Well, first it is not so black-and-white. Making DTO's is not really my idea:
http://www.hibernate.org/124.html
I have seen it quite many times on different pages/postings related to Hibernate.

About serialization:
* Type:
class X {
Long getId();
String getName();
X getParent();
Y getY();
}

* Operation in EJB:
X loadX(id) {
X x = (X)session.get(id, X.class);
x.getY(); // What changes if i remove this line?
x.getY().getName(); // And this?
x.getParent(); // And this?
return x;
}

saveX(X x) {
}

* Operation in web/client:
X x = ejb.loadX(xId); // What object network did i just receive?
x.getY().getId(); // What happens here?
x.getY().getName(); // And here?
x.getParent().getParent().getParent(); // And here?

x.setName("sxx");
Y y = ejb.getY(yId);
x.setY(y); // If I only wanted to change a relation between X & Y without modifying Y, why do I have to load Y?
y.setName("x");
ejb.saveX(x); // What object network did I just send back?

Now regarding 747: basicly, from the viewpoint of EJB interface, what I really need is not an object model, but relational model. I need to pass around records which have foreign keys and stuff. Can You suggest a better tool to do the job?

Yes, I don't use 70% of Hibernate capabilities, but if it does the job without the overhead, who cares? Besides, if situation changes (which I doubt) I can start using objects.

Now, theoretically speaking: what are advantages of passing around real objects instead of (relational)records aka DTO? I understand, that when Your data access layer is in the same web container as the servlet itself - it is convenient to use real objects. But why would I want to do this way when working with EJB over remote inerfaces?

OM


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 11:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you want a record orientated approach, why would you use an ORM and not something more table-orientated like iBatis ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 3:18 am 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
michael wrote:
If you want a record orientated approach, why would you use an ORM and not something more table-orientated like iBatis ?


Well, I still need a concept of mapping Record <-> Table. Otherwise I would have to duplicate infomration in every:
select
update
insert
...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:18 am 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
So can anyone answer my serialization questions?

OM


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I have absolutely no idea what your actual question is. If your question is: "can you serialize graphs of Hibernate objects?" then, as Emmanuel already said, the answer is: "yes".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 4:04 am 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
gavin wrote:
I have absolutely no idea what your actual question is. If your question is: "can you serialize graphs of Hibernate objects?" then, as Emmanuel already said, the answer is: "yes".


How exactly? See my questions in the comments of the source code from my second post:

* Type:
Code:
class X {
  Long getId();
  String getName();
  X getParent();
  Y getY();
  ...
}


* Operation in EJB:
Code:
X loadX(id) {
  X x = (X)session.get(id, X.class);
  x.getY(); // What changes if i remove this line?
  x.getY().getName(); // And this?
  x.getParent(); // And this?
  return x;
}

saveX(X x) {
  ...
}


* NB! Remote calls, Hibernate objects are serialized, no lazy-loading proxies any more!

* Operation in web/client:
Code:
X x = ejb.loadX(xId); // What object network did i just receive?
x.getY().getId(); // What happens here?
x.getY().getName(); // And here?
x.getParent().getParent().getParent(); // And here?

x.setName("sxx");
Y y = ejb.getY(yId);
x.setY(y); // If I only wanted to change a relation between X & Y without modifying Y, why do I have to load Y?
y.setName("x");
ejb.saveX(x); // What object network did I just send back?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 4:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I'm sorry, you'll have to ask an actual question, written in English, not post some bizarre psuedo-code that not even a compiler could understand, let alone a mere human.

Or how about you just try Hibernate out, experiment with different things, and learn for yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 10:19 am 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
gavin wrote:
I'm sorry, you'll have to ask an actual question, written in English, not post some bizarre psuedo-code that not even a compiler could understand, let alone a mere human.


* Let us have an object:
Code:
class X {
  Long getId();
  String getName();
  X getParent();
  Y getY();
  ...
}


* If I load an object "x"
Code:
  X x = (X)session.get(X.class, xId);

and then access a related object of type Y with getter
Code:
  x.getY();

and after that send this object to the web tier (which implies serialization), what network of serialized objects will I receive?
Code:
  x.getY().getId() = ?
  x.getY().getName() = ?
  x.getY().getZ() = ?
  x.getParent().getParent() = ?

Note that last two objects were not lazy-loaded.
* So in short when I load object "x", access a part of its lazy-loaded object network, and then serialize/deserialize it, which part of object network is passed?
* If I have object "x" in web tier and I want to change its relation to another object "y", should I do it like the following code or is there any way without loading "y"?
Code:
Y y = ejb.getY(yId);
x.setY(y);
ejb.saveX(x);


Quote:
Or how about you just try Hibernate out, experiment with different things, and learn for yourself.

Sure, that's an option that I will definitely use. But I would expect to get such information from Hibernate reference.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 11:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
what network of serialized objects will I receive?


Whatever network of objects you had on the server side .... I mean, this is a reasonably intuitive expectation .... and one you could easily have discovered by experimentation.

Serializing the graph does not cause more objects to be fetched, nor does it trim off any objects that are fetched. I'm not sure why you would expect this explicit statement in the manual.


Quote:
should I do it like the following code or is there any way without loading "y"?


I would advise that code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 12:09 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Hmmmm.... I think oleg_myrk's question is this: If we return a proxy from an EJB call, will it load all of it's lazy properties before being serialized and returned to the EJB client.

oleg_myrk, is that a good interpretation of your question?

If so, methinks the answer is "NO" :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 12:31 pm 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
greg_barton wrote:
Hmmmm.... I think oleg_myrk's question is this: If we return a proxy from an EJB call, will it load all of it's lazy properties before being serialized and returned to the EJB client.

oleg_myrk, is that a good interpretation of your question?

If so, methinks the answer is "NO" :)


Yes, partially this was my question.

See my next post


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 12:41 pm 
Newbie

Joined: Wed Apr 21, 2004 1:15 pm
Posts: 11
First of all thanks for the answer :)

Quote:
Whatever network of objects you had on the server side ....

What happens, if I loaded only X and then ask in web tier x.getY().getId()?
I know that in EJB "x.getY().getId()" would not actually result in loading the object.

Quote:
I mean, this is a reasonably intuitive expectation ....

Speak for Yourself :)

Quote:
and one you could easily have discovered by experimentation.

I would still suggest adding information to the reference. Hey, its *reference* :)

One more question: how would You organize a way to let a user edit a record like that:
Code:
class Z {
  Long getObject1();
  Long getOjbect2();
  Long getClassifier1();
  Long getClassifier2();
  ...
}


Would I have to:
Code:
  // Load Z
  Z z = ejb.loadZ(z);
  Long O1Id = z.getObject1().getId();
  Long O2Id = z.getObject2().getId();
  Long C1Id = z.getClassifier1().getId();
  Long C2Id = z.getClassifier2().getId();
 
  // Edit "z", possibly changing O1Id, O2Id, C1Id, C2Id
  (z, O1Id, O2Id, C1Id, C2Id) <-- edit();
 
  // Save Z
  Object1 o1 = ejb.loadObject1(O1Id);
  Object2 o2 = ejb.loadObject1(O2Id);
  Classifier1 c1 =  ejb.loadClassifier1(C1Id);
  Classifier2 c2 =  ejb.loadClassifier2(C2Id);
  z.setObject1(o1);
  z.setObject2(o2);
  z.setClassifier1(c1);
  z.setClassifier1(c2);
  ejb.saveZ(z);


This seems to be very inefficient:
* When loading Z, I have to load 4 extra objects
* When saving Z, I have to load 4 extra objects again (+ 4 round-trips)!

All this stuff with Hibernate works perfectly well when Hibernate is in web container. But in EJB it smells like I still need Data Transfer Objects with foreign keys. Am I wrong?

Hope I am not getting too annoying ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 12:57 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
M'kay, funny thing is I saw no hibernate code in your last example. How does any of that relate to hibernate?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.