• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Organization

 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking...

Lets say I have an object for Person, Address, Company and Hours (start and end times for a particular day).

Now I want to show a Person's personal info, company info, addresses for person and company and the hours worked for a particular week on a screen or web page.

I'm wondering how to best organize the objects for delivery (and general use for that matter)...

What brought me to this "question" is when I pull data from a database, it's not uncommon to have some sort of table join. This act of joining crosses object "borders".

I should note that I try and mimic my objects to the tables in the database (is this bad?)

How do I handle the mixing of data?

Do I create properties in the Person object for each of the other objects?
i.e.
<CODE>
public class Person {
Address personAddress = new Address();,
Company personCompany = new Company ();,
//etc.
}
</CODE>

but that wouldn't allow me to store multiple addresses for a particular Person, so would I declare a collection say,

<CODE>
public class Person {
ArrayList personAddresses = new ArrayList();
ArrayList personCompanies = new ArrayList();
//etc.
}
</CODE>

Or Do I simply keep them separate object and load my session up accordingly (Collection for the multiple items or simply the object)

In terms of performance, I'd rather do one query that grabs a persons "person", address and company all at the same time rather than a separate call to conform with my object organization...

Just don't know the "preferred" approach.

Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you investigate Object Relational Mapping (ORM). We have an entire forum to discuss this topic.

Systems such as EJB, Hibernate, iBatis, JDO, etc. all are based on just such a concept as you're discussing, but the persistency and (usually) the ability to generate your object classes from the database scheme - or vice-versa - are already provided and debugged.

In fact, one of my biggest stumbling blocks with JDO is that I keep thinking in SQL terms when I want to work with parent-child relationship when in fact all I have to to is retrieve the children as a java Collection from the parent object.
[ September 23, 2005: Message edited by: Tim Holloway ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you don't use a ORM framework, the question is still about ORM, so I'm moving to the forum with the same name...
 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting - I'm just learning hibernate now.

So what you are saying is create my People, Address, Company, as beans that use hibernate to access the data.

I'm still old school and for some reason like to work harder rather than smarter...

I'm on like day two of hibernate and am using this struts example as my foundation:
Struts With Hibernate

this sample albeit basic is exactly what I need to get my feet wet.
If you have a moment, take a look and let me know if this approach contains "bad habits".

Basic questions:
Does each type of lookup require its own bean?
The hibernate-mapping appears to be table specific does it apply to join queries too?

Feel free to direct me to pre-existing documentation or forums on this.

Thanks,

Chris
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in jdo i would do something like this.

then I would edit the metadata file to tell it if I want to use a foreign key in the personAddresses table, or if it should use a join table. Also I tell it if the relationship is bidirectional or unidirecitonal. bidirectional would mean personAddresses class also gets a reference to Person like so


If I use List instead of set then the foreign or join table would also get an index since a List is ordered but a set is not.

but this is just for putting the data in. yes you can get it out this way too, but its less usefull. You can write a query in JDOQL or SQL which is more useful to me.

so you create the classes depending on how you want to use them. but you can tweak the way the tables are created using the metadata. Of course its easy once you know how...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic