r2591 - in trunk: topia-persistence/src/main/java/org/nuiton/topia/framework topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence topia-persistence-tck/src/test/java/org/nuiton/topia/tck/legacy/framework
Author: athimel Date: 2012-06-29 18:12:11 +0200 (Fri, 29 Jun 2012) New Revision: 2591 Url: http://nuiton.org/repositories/revision/topia/2591 Log: Fix isTableExists implementation in TopiaHibernateSpecificUtil Modified: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernatePersistenceProvider.java trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-06-15 16:28:53 UTC (rev 2590) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-06-29 16:12:11 UTC (rev 2591) @@ -42,7 +42,6 @@ import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaContextFactory; import org.nuiton.topia.TopiaContextIllegalStateException; -import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaImportExportException; import org.nuiton.topia.TopiaMisconfigurationException; import org.nuiton.topia.TopiaNotFoundException; @@ -339,6 +338,21 @@ result = getParentContext().getSpecificUtil(); } if (result == null) { + try { + // FIXME AThimel 29/06/2012 This should not be hard-coded + Class<?> specificUtilClass = Class.forName( + "org.nuiton.topia.persistence.TopiaHibernateSpecificUtil"); + + result = (TopiaSpecificUtil)specificUtilClass.newInstance(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + if (result == null) { throw new TopiaMisconfigurationException("TopiaSpecificUtil not " + "found. Please check that you're using a " + "topia-persistence-xxx implementation"); @@ -1012,7 +1026,7 @@ public boolean isTableExists(Class<?> clazz) { checkClosed("isTableExists"); - boolean result = getSpecificUtil().isTableExists(clazz); + boolean result = getSpecificUtil().isTableExists(this, clazz); return result; } @@ -1027,7 +1041,7 @@ try { boolean showSchema = log.isDebugEnabled(); getFiresSupport().firePreCreateSchema(this); - getSpecificUtil().createSchema(showSchema); + getSpecificUtil().createSchema(this, showSchema); getFiresSupport().firePostCreateSchema(this); } catch (PersistenceException pe) { throw new TopiaPersistenceException( @@ -1039,7 +1053,7 @@ @Override public void showCreateSchema() { try { - getSpecificUtil().showCreateSchema(); + getSpecificUtil().showCreateSchema(this); } catch (PersistenceException pe) { throw new TopiaPersistenceException( "Schema creation could not be displayed for the following reason : " @@ -1052,7 +1066,7 @@ try { boolean showSchema = log.isDebugEnabled(); getFiresSupport().firePreUpdateSchema(this); - getSpecificUtil().updateSchema(showSchema); + getSpecificUtil().updateSchema(this, showSchema); getFiresSupport().firePostUpdateSchema(this); } catch (PersistenceException pe) { throw new TopiaPersistenceException( Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java 2012-06-15 16:28:53 UTC (rev 2590) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java 2012-06-29 16:12:11 UTC (rev 2591) @@ -44,37 +44,41 @@ * storage for a given persistent class. This is used within the * TopiaContext * - * @param clazz the researched class + * @param context the context on which table existence is checked + * @param clazz the researched class * @return true if the table exists on storage * @see TopiaContext#isTableExists(Class) */ - boolean isTableExists(Class<?> clazz); + boolean isTableExists(TopiaContextImplementor context, Class<?> clazz); /** * Implementation-dependent method to create the schema on storage. This is * used within the TopiaContext * - * @param showSchema indicates if schema must be displayed + * @param context the context on which schema creation must append + * @param showSchema indicates if schema must be displayed * @see TopiaContext#createSchema() */ - void createSchema(boolean showSchema); + void createSchema(TopiaContextImplementor context, boolean showSchema); /** * Implementation-dependent method to display the SQL queries for the * schema creation on storage. This is used within the TopiaContext * + * @param context the context on which schema creation must be shown * @see TopiaContext#showCreateSchema() */ - void showCreateSchema(); + void showCreateSchema(TopiaContextImplementor context); /** * Implementation-dependent method to update the schema on storage. This is * used within the TopiaContext * - * @param showSchema indicates if schema must be displayed + * @param context the context on which schema update must append + * @param showSchema indicates if schema must be displayed * @see TopiaContext#updateSchema() */ - void updateSchema(boolean showSchema); + void updateSchema(TopiaContextImplementor context, boolean showSchema); /** * Method used by the given TopiaContext when it receives a replicated Modified: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernatePersistenceProvider.java =================================================================== --- trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernatePersistenceProvider.java 2012-06-15 16:28:53 UTC (rev 2590) +++ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernatePersistenceProvider.java 2012-06-29 16:12:11 UTC (rev 2591) @@ -107,6 +107,27 @@ Map<String, Object> properties, Set<Class<?>> entities) { + Ejb3Configuration configured = loadConfiguration( + entityManagerName, properties, entities); + + EntityManagerFactory entityManagerFactory = + configured.buildEntityManagerFactory(); + + // And build the hibernate's specific util + Configuration hibernateConfiguration = + configured.getHibernateConfiguration(); + TopiaSpecificUtil specificUtil = + new TopiaHibernateSpecificUtil(hibernateConfiguration); + + Pair<EntityManagerFactory, TopiaSpecificUtil> result = + Pair.of(entityManagerFactory, specificUtil); + return result; + } + + public static Ejb3Configuration loadConfiguration( + String entityManagerName, Map<String, Object> properties, + Set<Class<?>> entities) { + Map<String, Object> hibernateProperties = Maps.newHashMap(properties); boolean useHibernateMapping = false; @@ -150,19 +171,9 @@ // Start configuration and build EntityManagerFactory Ejb3Configuration configured = cfg.configure(metadata, - hibernateProperties); - EntityManagerFactory entityManagerFactory = - configured.buildEntityManagerFactory(); + hibernateProperties); - // And build the hibernate's specific util - Configuration hibernateConfiguration = - configured.getHibernateConfiguration(); - TopiaSpecificUtil specificUtil = - new TopiaHibernateSpecificUtil(hibernateConfiguration); - - Pair<EntityManagerFactory, TopiaSpecificUtil> result = - Pair.of(entityManagerFactory, specificUtil); - return result; + return configured; } private static final ArrayList<EventType<? extends Serializable>> EVENT_TYPES = Lists.newArrayList( @@ -172,7 +183,7 @@ EventType.PRE_DELETE, EventType.POST_DELETE ); - private void registerListener(Map<String, Object> hibernateProperties) { + private static void registerListener(Map<String, Object> hibernateProperties) { String entityListenerFQN = TopiaHibernateEntityListener.class.getName(); @@ -185,7 +196,7 @@ } - protected void loadHibernateMappingFiles(List<String> entitiesNames, + protected static void loadHibernateMappingFiles(List<String> entitiesNames, PersistenceMetadata metadata) { // For each entity, get the HBM InputStream @@ -196,7 +207,7 @@ metadata.setHbmfiles(entitiesHbmFiles); } - protected void loadJPAMappingFiles(List<String> entitiesNames, + protected static void loadJPAMappingFiles(List<String> entitiesNames, PersistenceMetadata metadata) { // For each entity, get the ORM mapping path Modified: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java =================================================================== --- trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java 2012-06-15 16:28:53 UTC (rev 2590) +++ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java 2012-06-29 16:12:11 UTC (rev 2591) @@ -31,6 +31,7 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; import org.hibernate.dialect.Dialect; +import org.hibernate.ejb.Ejb3Configuration; import org.hibernate.ejb.EntityManagerImpl; import org.hibernate.jdbc.Work; import org.hibernate.mapping.PersistentClass; @@ -40,6 +41,7 @@ import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.hibernate.tool.hbm2ddl.TableMetadata; import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaPersistenceProvider; import org.nuiton.topia.framework.TopiaSQLWork; import org.nuiton.topia.framework.TopiaSpecificUtil; @@ -49,6 +51,8 @@ import java.sql.SQLException; import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Set; /** * Hibernate implementation of the {@link TopiaSpecificUtil}. @@ -65,32 +69,57 @@ protected Configuration configuration; + public TopiaHibernateSpecificUtil() { + // No need to set the 'configuration' + } + public TopiaHibernateSpecificUtil(Configuration configuration) { this.configuration = configuration; } + protected Configuration getConfiguration(TopiaContextImplementor context) { + Configuration result = configuration; + if (result == null) { + String entityManagerName = "whatever"; + Map<String, Object> properties = context.getJPAConfiguration(); + Set<Class<?>> entities = (Set<Class<?>>) properties.get(TopiaPersistenceProvider.TOPIA_ENTITIES); + + Ejb3Configuration configured = TopiaHibernatePersistenceProvider.loadConfiguration(entityManagerName, properties, entities); + + // WARNING AThimel 29/06/2012 : Do not set 'configuration' because it might be used in a non-initialized context + result = configured.getHibernateConfiguration(); + } + return result; + } + @Override - public void createSchema(boolean showSchema) { + public void createSchema(TopiaContextImplementor context, boolean showSchema) { + Configuration configuration = getConfiguration(context); SchemaExport schemaExport = new SchemaExport(configuration); schemaExport.create(showSchema, true); } @Override - public void showCreateSchema() { + public void showCreateSchema(TopiaContextImplementor context) { + Configuration configuration = getConfiguration(context); new SchemaExport(configuration).execute(true, false, false, true); } @Override - public void updateSchema(boolean showSchema) { + public void updateSchema(TopiaContextImplementor context, boolean showSchema) { + Configuration configuration = getConfiguration(context); new SchemaUpdate(configuration).execute(showSchema, true); } @Override - public boolean isTableExists(Class<?> clazz) { + public boolean isTableExists(TopiaContextImplementor context, Class<?> clazz) { String entityName = clazz.getName(); + Configuration configuration = getConfiguration(context); boolean result = false; try { + // Force the mapping loading (should not create the tables) + configuration.buildMappings(); PersistentClass classMapping = configuration.getClassMapping(entityName); if (classMapping == null) { Modified: trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java =================================================================== --- trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java 2012-06-15 16:28:53 UTC (rev 2590) +++ trunk/topia-persistence-tck/src/test/java/org/nuiton/topia/tck/legacy/framework/TopiaUtilTest.java 2012-06-29 16:12:11 UTC (rev 2591) @@ -103,18 +103,13 @@ public void testIsSchemaExist() throws Exception { TopiaContext rootContext = db.getRootCtxt(); - // XXX AThimel 10/05/2012 This cannot be done with ToPIA 3 because of - // XXX independence regarding to the JPA implem, which changes the way - // XXX EntityManager is created -// boolean actual = rootContext.isTableExists(PersonImpl.class); - boolean actual = false; + boolean actual = rootContext.isTableExists(PersonImpl.class); assertFalse(actual); TopiaContext tx = rootContext.beginTransaction(); - tx.createSchema(); - actual = rootContext.isTableExists(PersonImpl.class); + actual = rootContext.isTableExists(PersonImpl.class); assertTrue(actual); }
participants (1)
-
athimelï¼ users.nuiton.org