From 5a3521263be6bc8bce0da2fc14c4793f4ed34397 Mon Sep 17 00:00:00 2001 From: John Walker Date: Fri, 19 Jun 2015 10:15:20 -0700 Subject: [PATCH 1/2] Allow user to disable or override HibernateInterceptor --- framework/src/play/db/jpa/JPAPlugin.java | 32 ++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/framework/src/play/db/jpa/JPAPlugin.java b/framework/src/play/db/jpa/JPAPlugin.java index 88f2dd5af0..ebc89c92ee 100644 --- a/framework/src/play/db/jpa/JPAPlugin.java +++ b/framework/src/play/db/jpa/JPAPlugin.java @@ -2,6 +2,7 @@ import org.apache.commons.beanutils.PropertyUtils; import org.apache.log4j.Level; +import org.hibernate.Interceptor; import org.hibernate.ejb.Ejb3Configuration; import play.Logger; @@ -198,8 +199,35 @@ public void onApplicationStart() { } catch (Exception e) { Logger.error(e, "Error trying to override the hibernate classLoader (new hibernate version ???)"); } - - cfg.setInterceptor(new HibernateInterceptor()); + + //play.db.jpa.HibernateInterceptor is a Hibernate interceptor that modifies Hibernate's behavior to support + //Play's .willBeSaved property on Entities (among other things). + //Here we allow the user to disable this standard hibernate interceptor altogether, or replace it with + //their own interceptor. + if (!dbConfig.getProperty("hibernate.interceptor.disabled", "false").toLowerCase().trim().equals("true")) + { + final String interceptorClassName = dbConfig.getProperty("hibernate.interceptor", play.db.jpa.HibernateInterceptor.class.getCanonicalName()); + Logger.info(String.format("Loading Hibernate interceptor %s for db %s...", interceptorClassName, dbName)); + try + { + final Class interceptorClass = Play.classloader.loadClass(interceptorClassName); + if (!(org.hibernate.Interceptor.class.isAssignableFrom(interceptorClass))) { + Logger.error(String.format("Interceptor %s for db %s is not a sub-class of org.hibernate.Interceptor", interceptorClassName, dbName)); + } else { + final org.hibernate.Interceptor interceptor = (Interceptor)(interceptorClass.newInstance()); + Logger.info(String.format("Interceptor %s successfully loaded for db %s", interceptorClassName, dbName)); + cfg.setInterceptor(interceptor); + } + + } catch (ClassNotFoundException e) { + Logger.error(e, String.format("Unable to load Hibernate Interceptor %s for db %s : ClassNotFound.", interceptorClassName, dbName)); + } catch (Throwable e) { + Logger.error(e, String.format("Error instantiating Hibernate Interceptor %s for db %s", interceptorClassName, dbName)); + } + + } else { + Logger.info(String.format("Hibernate interceptor disabled for db %s...", dbName)); + } if (Logger.isTraceEnabled()) { Logger.trace("Initializing JPA for %s...", dbName); From 4668865d5eddb1a113d3c3535ecded1a30cc7c9b Mon Sep 17 00:00:00 2001 From: John Walker Date: Tue, 23 Jun 2015 15:39:12 -0700 Subject: [PATCH 2/2] Simplification: now only option is whether to disable play's interceptor. (Allowing user to add their own using hibernate.ebj properties if desired.) --- framework/src/play/db/jpa/JPAPlugin.java | 27 +++++------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/framework/src/play/db/jpa/JPAPlugin.java b/framework/src/play/db/jpa/JPAPlugin.java index ebc89c92ee..c67547556b 100644 --- a/framework/src/play/db/jpa/JPAPlugin.java +++ b/framework/src/play/db/jpa/JPAPlugin.java @@ -202,31 +202,14 @@ public void onApplicationStart() { //play.db.jpa.HibernateInterceptor is a Hibernate interceptor that modifies Hibernate's behavior to support //Play's .willBeSaved property on Entities (among other things). - //Here we allow the user to disable this standard hibernate interceptor altogether, or replace it with - //their own interceptor. + //Here we allow the user to disable this interceptor. The user can set up their own interceptor using + //hibernate.ejb.interceptor.session_scoped if desired. if (!dbConfig.getProperty("hibernate.interceptor.disabled", "false").toLowerCase().trim().equals("true")) { - final String interceptorClassName = dbConfig.getProperty("hibernate.interceptor", play.db.jpa.HibernateInterceptor.class.getCanonicalName()); - Logger.info(String.format("Loading Hibernate interceptor %s for db %s...", interceptorClassName, dbName)); - try - { - final Class interceptorClass = Play.classloader.loadClass(interceptorClassName); - if (!(org.hibernate.Interceptor.class.isAssignableFrom(interceptorClass))) { - Logger.error(String.format("Interceptor %s for db %s is not a sub-class of org.hibernate.Interceptor", interceptorClassName, dbName)); - } else { - final org.hibernate.Interceptor interceptor = (Interceptor)(interceptorClass.newInstance()); - Logger.info(String.format("Interceptor %s successfully loaded for db %s", interceptorClassName, dbName)); - cfg.setInterceptor(interceptor); - } - - } catch (ClassNotFoundException e) { - Logger.error(e, String.format("Unable to load Hibernate Interceptor %s for db %s : ClassNotFound.", interceptorClassName, dbName)); - } catch (Throwable e) { - Logger.error(e, String.format("Error instantiating Hibernate Interceptor %s for db %s", interceptorClassName, dbName)); - } - + Logger.info(String.format("Loading Hibernate interceptor %s for db %s...", play.db.jpa.HibernateInterceptor.class.getCanonicalName(), dbName)); + cfg.setInterceptor(new play.db.jpa.HibernateInterceptor()); } else { - Logger.info(String.format("Hibernate interceptor disabled for db %s...", dbName)); + Logger.info(String.format("play.db.jpa.HibernateInterceptor disabled for db %s...", dbName)); } if (Logger.isTraceEnabled()) {