2

I am working on a restaurant app using Hibernate. I have several entities like Pizza, Beverage, Pasta etc.

I want these to be persisted to different tables but have primary keys that are unique among all. i.e if I say itemId 4 it should be sufficient to identify any of the food items available.

Any idea how to do it guys?

I would prefer a solution using Hibernate annotations. :-|

Monika Michael
  • 1,274
  • 5
  • 13
  • 24
  • Why don't you store all items in one table, with an additional field that indicates the identity / item group? – Arjan Dec 24 '11 at 09:42
  • 1
    @Arjan that is a good solution from a relational point of view but it messes with my OO models :-| Secondly, it's not proper normalization. A pizza is likely to have different attributes than a beer. :-) – Monika Michael Dec 24 '11 at 09:46
  • True, I don't know what attributes you are storing for each item :) – Arjan Dec 24 '11 at 09:54
  • Pizza - crust, extraCheese, olives, mushroom ... etc etc. Beer - boolean isChilled :-) – Monika Michael Dec 24 '11 at 09:59

4 Answers4

3

You can use a db sequence and then any of your entities can use this sequence to generate their Id.

Scorpion
  • 3,938
  • 24
  • 37
2

An UUID would be unique for all tables, but you won't be able determine which the table is based on the id only.

To be honest, this is a bit strange requirement. But if you really insist on it, you can simply prefix the table/entity name to the primary key. For that you'd need a custom generator. See this question.

You can use inheritance. Have a base entity @MappedSuperclass Item, with @Inheritance strategy set to table-per-class and then Pasta, Pizza. If the fields don't differ much you use even single-table strategy with a discriminator column. And if your entity types are likely to grow, you can simply have one Item with a column type

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Using a UUID would make itemIds look really ugly to print on bills and stuff. Could I not just somehow get integer ids that are unique? – Monika Michael Dec 24 '11 at 09:40
  • Strange requirement? :-) I could've created a single Item class to represent all the food items. But I want to keep my object model clean by proper categorization. – Monika Michael Dec 24 '11 at 09:41
  • Wow that sounds like a great idea. Thanks. Although I'll wait for a while before selecting an answer to see if I get any alternate solutions. :-) – Monika Michael Dec 24 '11 at 09:52
2

I think you have not got the analysis quite correct. If you look how you will use thee entities you mention I think there will also be an entity like order that will contain several of these, also all these entities do have some similarity they all can be considered a subclass of 'menu item' as they all have some common attributes e.g. price and can be used in the same way e.g. included in an order.

You can look at section 5.1.6 in Hibernate core documentation as to how to implement various strategies of mapping the OO modle to a table or several tables. Note that Table per class strategy and 10.1.5. Table per concrete class would give you your current table setup but you need to be able to generate the key which you sat MySQL cannot do easily. However I think you will find that there are common properties that should be on the base class and so another form of the mapping would be needed and you end up with a menu item table and that can easily have a generated id.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
0

I agree with Mark, it seems something like a table per subclass would fit your design and needs to have an id across multiple items. Also note that having your items in multiple table without a common parent table could lead to some trouble when you are in the need of a FK for these items.

Andrea Vacondio
  • 888
  • 9
  • 19