I have a simple question regarding Entity declaration in JPA. I have an entity with 2 foreign keys, which are not null and form an uniqueConstraint. First I was thinking about a composite key, composed of the two foreign keys, but I heard that this is a legacy design, and not the recommended way of designing new tables.
So I am interested if Hibernate/JPA can automatically generate id, based on the two foreign keys. Let's say I have the following Entity:
@Entity
public class Foo {
@ManyToOne
private Bar bar;
private int i;
}
(I omitted not null and uniqueConstraint tags to make the code more readable)
I know I can simply add an id field, with GeneratedValue, and let my DB generate the key (in my example MySQL with auto_increment), but this seems inefficient to me as it involves querying the database, and asking it to generate the unique id value.
Is there a way of generating an id, which is not composite (i.e. of type int or long), based on the id of the "Bar" class, and value of the integer "i", since it those two values already form a unique constraint?