After using C++ I got used to the concept of Identifier which can be used with a class for the type, provides type safety and has no runtime overhead (the actual size is the size of the primitive). I want to do something like that, so I will not make mistakes like:
personDao.find(book.getId());//I want compilation to fail
personDao.find(book.getOwnerId());//I want compilation to succeed
Possible solutuions that I don't like:
- For every entity have an entity id class wrapping the id primitive. I don't like the code bloat.
Create a generic Identifier class. Code like this will not compile:
void foo(Identifier<Book> book);
void foo(Identifier<Person> person);
Does anyone know of a better way?
Is there a library with a utility such as this?
Is implementing this an overkill?
And the best of all, can this be done in Java without the object overhead like in C++?