This is the right approach, and anything generic can be <T extends Entity> if you need it.
Using Nashorn is a bad idea for this (crossing language barriers comes with a cost), and it’s also in the process of being dropped from the JDK so your code might not work in future.