I'm working with plain 'ol J2SE and using Hibernate via JPA2, but at least there I've found that passing a custom properties map to the persistence unit when I init it seems to be the easiest way to go.
I use the classloader to find my default properties map for the connection, containing all the usual hibernate props. I slurp that into Properties.load(InputStream) to create the default properties map I'll be using. The user is prompted for authentication by the app before initing the PU, and a new properties map is created with the old one as its base and the username and password attributes inserted into it. Oversimplified, something like:
Code:
AuthDialog someDialog = new AuthDialog();
someDialog.setVisible(true);
if (!someDialog.isCancelled()) {
Properties connProps = new Properties( dflConnProps );
connProps.setProperty("hibernate.connection.username", someDialog.getSuppliedUsername());
connProps.setProperty("hibernate.connection.password", someDialog.getSuppliedPassword());
EntityManagerFactory f = new Persistence.createEntityManagerFactory("PUname", connProps);
}
... though of course in the real app I don't jumble DB and gui code together like this, it's all cleanly separated and the DB work runs on a background thread.
This way, I don't have to mess about with custom connection providers. I don't know how well this applies to an appserver managed environment, though...