[Stripes-users] Best way to handle hierarchy of objects?

2010-10-07 Thread Aurangzeb Agha
I have a tiered set of model objects:

Restaurants have one ore more Menus that have one or more Items.

I've expressed the Restaurant to Menu relationship as follows:

Entity
public class Restaurant extends ModelBase {
OneToMany(mappedBy="restaurant")
 private List menus;
...
}

Entity
Table (
name="MENU",
 uniqueConstraints=UniqueConstraint(columnNames={"NAME",
"RESTAURANT_ID"})
)
public class Menu extends ModelBase {
 ManyToOne(optional=false)
JoinColumn(name="RESTAURANT_ID")
 private Restaurant restaurant;
...
private String name;
...
}

I'm curious about a couple of issues:

1.  I want to make sure that when a menu is created *for a restaurant*, not
more than one menu *with the same name *can be created for that restaurant.
 Is controlling this with the UniqueContraint the right way to go?

2.  Do I need to create a Restaurant object *in* the Menu?  What I want to
ensure is that when a menu is created for a restaurant, I have a way of
knowing which menu was created for which restaurant.  Should I just have an
"int restaurantId" parameter in place of the Restaurant object?  If so, how
do I ensure the constraint in point #1 holds true?
--
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb___
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users


Re: [Stripes-users] Best way to handle hierarchy of objects?

2010-10-07 Thread Oscar Westra van Holthe - Kind
On 07-10-2010 at 01:45, Aurangzeb Agha wrote:
> I have a tiered set of model objects:
> 
> Restaurants have one ore more Menus that have one or more Items.
> 
> I've expressed the Restaurant to Menu relationship as follows:
[snip: bidirectional 1-n relationship]

> I'm curious about a couple of issues:
> 
> 1.  I want to make sure that when a menu is created *for a restaurant*, not
> more than one menu *with the same name *can be created for that restaurant.
>  Is controlling this with the UniqueContraint the right way to go?

Yes, especially at the start. Having the database restrict your data to your
own constraints helps you to keep your code correct: if it isn't the database
will return an error.

The second step (sadly often done as step one) is to make your code behave as
you want it. Wedged between the database constraints and your unit tests,
your code can only be good.

 
> 2.  Do I need to create a Restaurant object *in* the Menu?  What I want to
> ensure is that when a menu is created for a restaurant, I have a way of
> knowing which menu was created for which restaurant.  Should I just have an
> "int restaurantId" parameter in place of the Restaurant object?  If so, how
> do I ensure the constraint in point #1 holds true?

Use a Restaurant object instead of the id (you can always ask the object for
its id if you need to).

As for the first part, that is a matter of preference. Keeping it simple, you
have two options:
1. Menu menu = new Menu(restaurant, "name");
2. Menu menu = restaurant.newMenu("name");

Assuming that a Menu cannot exist on its own (it must be created for a
Restaurant), I'd choose option 2. As a bonus, it's easier to verify the name
is unique within the restaurant.

On the other hand, if a Menu _can_ exist without a Restaurant (not the case
as you marked the restaurant field non-optional), option 1 is better.


Oscar

-- 
   ,-_
  /() ) Oscar Westra van Holthe - Kind  http://www.xs4all.nl/~kindop/
 (__ (
=/  ()  QED - Quite Easily Done


signature.asc
Description: Digital signature
--
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb___
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users