• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design Question

 
Varun Chopra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following tables in database (simplified version):

Units - [id, description]
Property - [id, name, unitid]

From objects point of view, if I have a property object, need would be to call getUnits method on it and return value should be unit description.

class Property
{
int id;
String name;
private id unitid;

public String getUnits()
{
//get unitDescrition from unitid
return unitDescription;
}

}

question is, how will getUnits method get unit description using unitid? Should it call DAO method here to get description from Units table? Is it ok to call DAO method (or even a service method) in a domain object?

How should I design this?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little lost.

You have a property, the property has units and the units have a description? Or is the description the property of the unit?

The pattern of a Thing object and a ThingDetail object is very common. A Thing has a property of type detail. Sometimes two objects map to one table, sometimes these two Java objects map to two tables.

I describe how to do this through a one-to-one mapping in the following tutorial on my site (you might want to flip the mappings though):

Mapping One To One Associations - Thing and a ThingDetail

If you're doing it with two classes mapping to one table, you can do it by making the ThingDetail an embeddable object. Here's that tutorial:

Mapping two classes to one table in Hibernate with JPA Annotations: A Tutorial

I'm not sure if this is where you're going. Post back with more detail and we'll try our best to help you out!

-Cameron McKenzie
 
Varun Chopra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply McKenzie. Let me try to explain it more.

I have 2 entities - Property and Unit
Units can exist in isolation as well, through the Admin interface I can add more units. Later on I can link one or more Properties to their respective unit (only one unit per property). So, to Admin module Unit is directly accessible, hence I cannot always embed it in property.

On the other hand, for user module, unit exists only with Property object and they don't know unitid, they should directly get unit-description, I won't like to code it like below:

propObject.getUnit().getDescription();

Instead I would like to code like this:

propObject.getUnit();//which should return me unit description

I understand that with hibernate or other persistence framework, I can define relation between property and unit and load units along with property. But my question is if I am using plain JDBC, where should I write the code for loading unit description from database? I am trying to understand it in old DAO world, not with hibernate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic