In order to get this to work, you have to bootstrap Hibernate. To do this:
In your main class, find your EntityManagerFactory instance. It probably looks something like this:
public static final EntityManagerFactory EMPFAC = Persistence.createEntityManagerFactory("jpa")
Leave that line for now. Above it, add the following:
private static StandardServiceRegistryBuilder servReg = new StandardServiceRegistryBuilder();
static {
//Do this for every property in persistence.xml
servReg.applySetting("hibernate.connection.url", "jdbc://myurl://www.example.org");
}
Add a servReg.applySetting()
for every property in persistence.xml
. Then, add this below it:
private static MetadataSources sources = new MetadataSources(servReg.build());
static {
// Do this for every pojo with JPA annotations
sources.addAnnotatedClass(Pojo.class);
}
Add a sources.addAnnotatedClass()
for every JPA-annotated pojo in your application. Almost done here. Now create a class that implements StatementInspector
like so:
public class Inspector implements StatementInspector {
private static final long serialVersionUID = 5545844969759630544L;
@Override
public String inspect(String select) {
// modify string here
return select;
}
}
Finally, go back to your main application class, and, underneath the static block where you registered your pojos, add the following:
public static final SessionFactory EMPFAC = sources.buildMetadata().getSessionFactoryBuilder().applyStatementInspector(new Inspector()).build();
Now you can go back to persistence.xml
and delete the property declarations there. If you haven't already, also delete your public static final EntityManager
declaration (you've replaced it with the SessionFactory).
EDIT: You can actually delete persistence.xml
and your application should still work fine.
All set!