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. 😐
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
@MappedSuperclassItem, with@Inheritancestrategy set to table-per-class and thenPasta,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 oneItemwith a columntype