I keep two identifiers for my entities -
- An internal id which is also the primary key of the corresponding table and the foreign key for referencing tables.
- An external id that is generally provided by clients and is guaranteed to be unique.
@Entity
class Student {
@Id
Integer id; // internal id
String email; // external id
String name;
@ManyToOne
School school // Assume that school also has an internal and external id
// student is the owning side of this relationship,
// In database terms, table student has school_id
}
This seems fine, however, say I get a request from a client to create a new Student
record, this is generally of the form (foo_email, bar_name, school_external_id)
. Even even though the School
entity’s instance is not changing, it will need to be fetched from the db to create its association with the newly created Student
instance.
Student student = new Student(foo_email, bar_name);
School school = // code to fetch from db
student.setSchool(school);
// code to persist student instance
There is no option to create a proxy session.load(School.class, id)
instance because that can only be done with the field marked as @Id
i.e. the internal id.
From a design perspective it’s better to have separate internal and external ids, for example - the external id email
might need to be updated in the future.
From a performance perspective, it’s better to not load a School
instance if it’s not intended to be read from or written to.
Using Hibernate, I feel like I have to give up on either one of them, or maybe I am looking at the problem from the wrong perspective. Is there a better way?