r2444 - in trunk: . topia-persistence topia-persistence/src/main/java/org/nuiton/topia/framework topia-persistence/src/main/java/org/nuiton/topia/generator topia-persistence/src/main/java/org/nuiton/topia/persistence topia-persistence/src/main/java/org/nuiton/topia/persistence/util
Author: athimel Date: 2012-05-07 17:52:54 +0200 (Mon, 07 May 2012) New Revision: 2444 Url: http://nuiton.org/repositories/revision/topia/2444 Log: Remove explicit Hibernate dependency Removed: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java Modified: trunk/pom.xml trunk/topia-persistence/pom.xml trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityHibernateMappingGenerator.java trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaTagValues.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/DBMapping.java Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/pom.xml 2012-05-07 15:52:54 UTC (rev 2444) @@ -71,8 +71,8 @@ <processorPluginVersion>1.2.1</processorPluginVersion> <nuitonI18nVersion>2.4.1</nuitonI18nVersion> <xmlrpcVersion>3.1.2</xmlrpcVersion> - <hibernateVersion>3.6.10.Final</hibernateVersion> - <!--<hibernateVersion>4.0.0.CR1</hibernateVersion>--> + <!--<hibernateVersion>3.6.10.Final</hibernateVersion>--> + <hibernateVersion>4.1.3.Final</hibernateVersion> <sl4jVersion>1.6.4</sl4jVersion> <h2Version>1.3.165</h2Version> @@ -105,7 +105,7 @@ <scope>compile</scope> </dependency> - <dependency> + <!--dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernateVersion}</version> @@ -134,6 +134,13 @@ </exclusion> </exclusions> <scope>runtime</scope> + </dependency--> + + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jpa_2.0_spec</artifactId> + <version>1.1</version> + <scope>compile</scope> </dependency> <dependency> Modified: trunk/topia-persistence/pom.xml =================================================================== --- trunk/topia-persistence/pom.xml 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/pom.xml 2012-05-07 15:52:54 UTC (rev 2444) @@ -82,7 +82,7 @@ <artifactId>dom4j</artifactId> </dependency> - <dependency> + <!--dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> @@ -94,8 +94,15 @@ <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> + </dependency--> + + + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jpa_2.0_spec</artifactId> </dependency> + <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaConnectionProvider.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -26,13 +26,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.HibernateException; -import org.hibernate.cfg.Environment; -import org.hibernate.connection.ConnectionProvider; -import org.hibernate.connection.ConnectionProviderFactory; -import org.hibernate.util.PropertiesHelper; -import org.hibernate.util.ReflectHelper; +//import org.hibernate.cfg.Environment; +//import org.hibernate.connection.ConnectionProvider; +//import org.hibernate.connection.ConnectionProviderFactory; +//import org.hibernate.util.PropertiesHelper; +//import org.hibernate.util.ReflectHelper; +import javax.persistence.PersistenceException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -112,7 +112,7 @@ } @Override - public void configure(Properties props) throws HibernateException { + public void configure(Properties props) throws PersistenceException { String driverClass = props.getProperty(Environment.DRIVER); poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20 @@ -148,7 +148,7 @@ } catch (ClassNotFoundException e) { String msg = "JDBC Driver class not found: " + driverClass; log.error(msg, e); - throw new HibernateException(msg, e); + throw new PersistenceException(msg, e); } } } @@ -160,7 +160,7 @@ if (log.isErrorEnabled()) { log.error(msg); } - throw new HibernateException(msg); + throw new PersistenceException(msg); } connectionProps = 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-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -35,27 +35,20 @@ import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; -import org.hibernate.EntityMode; -import org.hibernate.FlushMode; -import org.hibernate.HibernateException; -import org.hibernate.Query; -import org.hibernate.ReplicationMode; -import org.hibernate.SQLQuery; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; -import org.hibernate.event.PostDeleteEventListener; -import org.hibernate.event.PostInsertEventListener; -import org.hibernate.event.PostLoadEventListener; -import org.hibernate.event.PostUpdateEventListener; -import org.hibernate.event.PreDeleteEventListener; -import org.hibernate.event.PreInsertEventListener; -import org.hibernate.event.PreLoadEventListener; -import org.hibernate.event.PreUpdateEventListener; -import org.hibernate.jdbc.Work; -import org.hibernate.tool.hbm2ddl.SchemaExport; -import org.hibernate.tool.hbm2ddl.SchemaUpdate; +//import org.hibernate.EntityMode; +//import org.hibernate.FlushMode; +//import org.hibernate.ReplicationMode; +//import org.hibernate.cfg.Configuration; +//import org.hibernate.event.PostDeleteEventListener; +//import org.hibernate.event.PostInsertEventListener; +//import org.hibernate.event.PostLoadEventListener; +//import org.hibernate.event.PostUpdateEventListener; +//import org.hibernate.event.PreDeleteEventListener; +//import org.hibernate.event.PreInsertEventListener; +//import org.hibernate.event.PreLoadEventListener; +//import org.hibernate.event.PreUpdateEventListener; +//import org.hibernate.tool.hbm2ddl.SchemaExport; +//import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaContextFactory; import org.nuiton.topia.TopiaException; @@ -72,6 +65,13 @@ import org.nuiton.topia.persistence.TopiaId; import org.nuiton.util.ArrayUtil; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.FlushModeType; +import javax.persistence.PersistenceException; +import javax.persistence.Query; +import javax.persistence.metamodel.EntityType; import java.beans.PropertyChangeListener; import java.io.BufferedInputStream; import java.io.File; @@ -81,9 +81,6 @@ import java.io.Reader; import java.io.Writer; import java.lang.reflect.Field; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -145,10 +142,10 @@ * la factory permettant de recuperer la session hibernate. Seul les * TopiaContextImpl initiaux contiennent un hibernateFactory */ - protected SessionFactory hibernateFactory; + protected EntityManagerFactory hibernateFactory; /** La session utilisé par le TopiaContextImpl */ - protected Session hibernate; + protected EntityManager hibernate; /** Indique si le contexte a ete ferme */ protected boolean closed; @@ -159,7 +156,7 @@ * The normal usage is to says yes (that's why the default value is * {@code true}), in that case whebn doing queries (says in method * {@link #find(String, Object...)} or {@link #find(String, int, int, Object...)}) - * it will use the flush mode {@link FlushMode#AUTO}). + * it will use the flush mode {@link javax.persistence.FlushModeType#AUTO}). * * But sometimes, when doing a lot of queries (for some imports for example), * we do NOT want the session to be flushed each time we do a find, then you @@ -451,7 +448,7 @@ new SchemaExport(getHibernateConfiguration()).create(showSchema, true); getFiresSupport().firePostCreateSchema(this); - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException( _("topia.persistence.error.create.schema", eee.getMessage()), eee); @@ -463,7 +460,7 @@ try { new SchemaExport(getHibernateConfiguration()). execute(true, false, false, true); - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException( _("topia.persistence.error.create.schema", eee.getMessage()), eee); @@ -481,7 +478,7 @@ new SchemaUpdate(getHibernateConfiguration()).execute(showSchema, true); getFiresSupport().firePostUpdateSchema(this); - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException( _("topia.persistence.error.update.schema", eee.getMessage()), eee); @@ -489,7 +486,7 @@ } @Override - public Session getHibernate() throws TopiaException { + public EntityManager getHibernate() throws TopiaException { if (hibernate == null) { throw new TopiaException( _("topia.persistence.error.no.hibernate.session")); @@ -498,7 +495,7 @@ } @Override - public SessionFactory getHibernateFactory() throws TopiaNotFoundException { + public EntityManagerFactory getHibernateFactory() throws TopiaNotFoundException { if (hibernateFactory == null) { if (getParentContext() != null) { hibernateFactory = getParentContext().getHibernateFactory(); @@ -626,7 +623,7 @@ if (log.isDebugEnabled()) { log.debug("Load persistent class : " + classname); } - + // XXX echatellier 20111007 ce cqui est dommage ici, c'est // la definition de cette classe ne sert a rien (apart security) // car pour hibernate hibernateConfiguration.addClass(persistanceClass) @@ -702,14 +699,15 @@ throw new TopiaException( _("topia.persistence.error.rootContext.access")); } - if (getHibernateFactory().getClassMetadata(entityClass) == null && - getHibernateFactory().getClassMetadata( - entityClass.getName() + "Impl") == null && - getHibernateFactory().getClassMetadata( - entityClass.getName() + "Abstract") == null) { + if (getHibernateFactory().getMetamodel().entity(entityClass) == null +// && getHibernateFactory().getClassMetadata( +// entityClass.getName() + "Impl") == null +// && getHibernateFactory().getClassMetadata( +// entityClass.getName() + "Abstract") == null + ) { log.info(_("topia.persistence.supported.classes.for.context", - getHibernateFactory().getAllClassMetadata().keySet())); + getHibernateFactory().getMetamodel().getEntities())); throw new TopiaException( _("topia.persistence.error.unsupported.class", entityClass.getName())); @@ -751,13 +749,13 @@ checkClosed(_("topia.persistence.error.context.is.closed")); TopiaContextImpl result = new TopiaContextImpl(this); - SessionFactory factory = getHibernateFactory(); - result.hibernate = factory.openSession(); + EntityManagerFactory factory = getHibernateFactory(); + result.hibernate = factory.createEntityManager(); // TODO AThimel 07/05/2012 Maybe provide a Map ? cf Configuration // new TopiaInterceptor(result)); // on ne synchronise jamais les données avec la base tant que // l'utilisateur n'a pas fait de commit du context - result.hibernate.setFlushMode(FlushMode.MANUAL); + result.hibernate.setFlushMode(FlushModeType.COMMIT); // TODO AThimel 07/05/2012 Was MANUAL // tchemit 2010-12-06 propagates the value of the flag result.useFlushMode = useFlushMode; @@ -768,14 +766,15 @@ // transaction par defaut est utilisé mais elle n'est jamais vraiment // fermé ce qui pose des problemes de lock sur les tables. try { - result.hibernate.beginTransaction(); +// result.hibernate.beginTransaction(); + result.hibernate.getTransaction().begin(); } catch (Exception eee) { // on a pas pu ouvrir la transaction, on faut donc tout fermer // et declancher une exception try { result.hibernate.close(); - } catch (HibernateException e1) { + } catch (PersistenceException e1) { if (log.isErrorEnabled()) { log.error("Could not close hibernate session", e1); } @@ -809,8 +808,7 @@ // // TODO-fdesbois-20100507 : need to be removed for 2.5 version // dao.commitTransaction(); // } - Transaction tx = hibernate.getTransaction(); - // Transaction tx = hibernate.beginTransaction(); + EntityTransaction tx = hibernate.getTransaction(); hibernate.flush(); tx.commit(); @@ -820,7 +818,8 @@ parent.getFiresSupport().fireOnPostCommit(this); } - hibernate.beginTransaction(); +// hibernate.beginTransaction(); + hibernate.getTransaction().begin(); // it's seem necessary to change session after commit // NON, NON, NON, il ne faut surtout pas le faire, ca pose plein de @@ -846,17 +845,17 @@ // // TODO-fdesbois-20100507 : need to be removed for 2.5 version // dao.rollbackTransaction(); // } - Transaction tx = hibernate.getTransaction(); - // Transaction tx = hibernate.beginTransaction(); + EntityTransaction tx = hibernate.getTransaction(); hibernate.clear(); tx.rollback(); hibernate.close(); // it's very important to change the session after rollback // otherwize there are many error during next Entity's modification - hibernate = getHibernateFactory().openSession(); - hibernate.setFlushMode(FlushMode.MANUAL); + hibernate = getHibernateFactory().createEntityManager(); // TODO AThimel 07/05/2012 Maybe provide a Map ? cf Configuration + hibernate.setFlushMode(FlushModeType.COMMIT); // TODO AThimel 07/05/2012 Was MANUAL - hibernate.beginTransaction(); +// hibernate.beginTransaction(); + hibernate.getTransaction().begin(); getFiresSupport().fireOnPostRollback(this); TopiaContextImplementor parent = getParentContext(); @@ -864,7 +863,7 @@ parent.getFiresSupport().fireOnPostRollback(this); } - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException( _("topia.persistence.error.on.rollback", eee.getMessage()), eee); @@ -926,10 +925,10 @@ @Override public void executeSQL(String sqlScript) throws TopiaException { - SQLWork sqlWork = new SQLWork(sqlScript); try { - getHibernate().doWork(sqlWork); - } catch (HibernateException e) { + Query nativeQuery = getHibernate().createNativeQuery(sqlScript); + nativeQuery.executeUpdate(); + } catch (PersistenceException e) { throw new TopiaException("Could not execute sql code", e); } } @@ -976,22 +975,22 @@ for (int j = 0; j < args.length; j += 2) { String name = (String) args[j]; Object value = args[j + 1]; - if (value.getClass().isArray()) { - query.setParameterList(name, (Object[]) value); - } else if (value instanceof Collection<?>) { - query.setParameterList(name, (Collection<?>) value); - } else { +// if (value.getClass().isArray()) { +// query.setParameterList(name, (Object[]) value); +// } else if (value instanceof Collection<?>) { +// query.setParameterList(name, (Collection<?>) value); +// } else { query.setParameter(name, value); - } +// } } // tchemit 2010-11-30 reproduce the same behaviour than before with the dao legacy if (useFlushMode) { - query.setFlushMode(FlushMode.AUTO); + query.setFlushMode(FlushModeType.AUTO); } - List result = query.list(); + List result = query.getResultList(); result = firesSupport.fireEntitiesLoad(this, result); return result; - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(_("topia.persistence.error.on.query", hql, eee.getMessage()), eee); } @@ -1008,22 +1007,22 @@ for (int j = 0; j < args.length; j += 2) { String name = (String) args[j]; Object value = args[j + 1]; - if (value.getClass().isArray()) { - query.setParameterList(name, (Object[]) value); - } else { +// if (value.getClass().isArray()) { +// query.setParameterList(name, (Object[]) value); +// } else { query.setParameter(name, value); - } +// } } query.setFirstResult(startIndex); query.setMaxResults(endIndex - startIndex + 1); // tchemit 2010-11-30 reproduce the same behaviour than before with the dao legacy if (useFlushMode) { - query.setFlushMode(FlushMode.AUTO); + query.setFlushMode(FlushModeType.AUTO); } - List result = query.list(); + List result = query.getResultList(); result = firesSupport.fireEntitiesLoad(this, result); return result; - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(_("topia.persistence.error.on.query", hql, eee.getMessage()), eee); } @@ -1049,7 +1048,7 @@ } int result = query.executeUpdate(); return result; - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(_("topia.persistence.error.on.query", hql, eee.getMessage()), eee); } @@ -1093,7 +1092,7 @@ Element entity = (Element) it.next(); try { sessionDom4j.replicate(entity, ReplicationMode.EXCEPTION); - } catch (HibernateException he) { + } catch (PersistenceException he) { log.warn(_("topia.persistence.error.replicate.entity", entity, he.getMessage()), he); } @@ -1166,7 +1165,7 @@ result.write(doc); result.close(); - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(_("topia.persistence.error.on.export", eee.getMessage()), eee); } catch (IOException eee) { @@ -1205,7 +1204,7 @@ " was sucessfully done."); } } - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(_("topia.persistence.error.on.replicate", eee.getMessage()), eee); } @@ -1270,9 +1269,9 @@ options += " COMPRESSION GZIP"; } - SQLQuery query = getHibernate().createSQLQuery( + Query query = getHibernate().createNativeQuery( "SCRIPT TO '" + file.getAbsolutePath() + "'" + options); - query.list(); + query.getResultList(); } catch (Exception eee) { throw new TopiaException(_( @@ -1317,7 +1316,7 @@ in.close(); } - SQLQuery query = getHibernate().createSQLQuery( + Query query = getHibernate().createNativeQuery( "RUNSCRIPT FROM '" + file.getAbsolutePath() + "'" + options); query.executeUpdate(); @@ -1346,7 +1345,7 @@ if (dropDatabase) { sql += " DELETE FILES"; } - Query query = tx.getHibernate().createSQLQuery(sql); + Query query = tx.getHibernate().createNativeQuery(sql); query.executeUpdate(); tx.closeContext(); root.finalize(); @@ -1498,12 +1497,14 @@ // si entityAndcondition est vide alors il faut le remplir // avec toutes les entités du mapping (class, null) if (entityAndCondition.length == 0) { - Map<?,?> classMetadata = getHibernateFactory().getAllClassMetadata(); +// Map<?,?> classMetadata = getHibernateFactory().getAllClassMetadata(); + Set<EntityType<?>> classMetadata = getHibernateFactory().getMetamodel().getEntities(); entityAndCondition = new Object[classMetadata.size() * 2]; int i = 0; - for (Object className : classMetadata.keySet()) { + for (EntityType entityType : classMetadata) { + String className = entityType.getName(); try { - entityAndCondition[i++] = Class.forName((String) className); + entityAndCondition[i++] = Class.forName(className); } catch (ClassNotFoundException e) { // should never happen! throw new TopiaException( @@ -1551,33 +1552,33 @@ // dettach entity to source session, to make possible copy of // collection without a hibernate exception (list opened in // two session...) - getHibernate().evict(entity); + getHibernate().detach(entity); dstContextImpl.getHibernate().replicate(entity, ReplicationMode.EXCEPTION); } - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(_("topia.persistence.error.on.replicate", eee.getMessage()), eee); } } - public static class SQLWork implements Work { - private final String script; - - public SQLWork(String script) { - this.script = script; - } - - @Override - public void execute(Connection connection) throws SQLException { - PreparedStatement sta = connection.prepareStatement(script); - try { - sta.execute(); - } finally { - sta.close(); - } - } - } +// public static class SQLWork implements Work { +// private final String script; +// +// public SQLWork(String script) { +// this.script = script; +// } +// +// @Override +// public void execute(Connection connection) throws SQLException { +// PreparedStatement sta = connection.prepareStatement(script); +// try { +// sta.execute(); +// } finally { +// sta.close(); +// } +// } +// } } //TopiaContextImpl Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -25,15 +25,15 @@ package org.nuiton.topia.framework; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.cfg.Configuration; +//import org.hibernate.cfg.Configuration; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaNotFoundException; import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaEntity; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; import java.util.List; import java.util.Map; import java.util.Properties; @@ -70,13 +70,13 @@ * @return Returns the hibernate. * @throws TopiaException si aucune transaction n'est ouverte */ - Session getHibernate() throws TopiaException; + EntityManager getHibernate() throws TopiaException; /** * @return Returns the hibernateFactory. * @throws TopiaNotFoundException */ - SessionFactory getHibernateFactory() throws TopiaNotFoundException; + EntityManagerFactory getHibernateFactory() throws TopiaNotFoundException; /** * @return Returns the hibernate configuration Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -28,23 +28,22 @@ import org.apache.commons.collections.map.IdentityMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.Session; -import org.hibernate.event.PostDeleteEvent; -import org.hibernate.event.PostDeleteEventListener; -import org.hibernate.event.PostInsertEvent; -import org.hibernate.event.PostInsertEventListener; -import org.hibernate.event.PostLoadEvent; -import org.hibernate.event.PostLoadEventListener; -import org.hibernate.event.PostUpdateEvent; -import org.hibernate.event.PostUpdateEventListener; -import org.hibernate.event.PreDeleteEvent; -import org.hibernate.event.PreDeleteEventListener; -import org.hibernate.event.PreInsertEvent; -import org.hibernate.event.PreInsertEventListener; -import org.hibernate.event.PreLoadEvent; -import org.hibernate.event.PreLoadEventListener; -import org.hibernate.event.PreUpdateEvent; -import org.hibernate.event.PreUpdateEventListener; +//import org.hibernate.event.PostDeleteEvent; +//import org.hibernate.event.PostDeleteEventListener; +//import org.hibernate.event.PostInsertEvent; +//import org.hibernate.event.PostInsertEventListener; +//import org.hibernate.event.PostLoadEvent; +//import org.hibernate.event.PostLoadEventListener; +//import org.hibernate.event.PostUpdateEvent; +//import org.hibernate.event.PostUpdateEventListener; +//import org.hibernate.event.PreDeleteEvent; +//import org.hibernate.event.PreDeleteEventListener; +//import org.hibernate.event.PreInsertEvent; +//import org.hibernate.event.PreInsertEventListener; +//import org.hibernate.event.PreLoadEvent; +//import org.hibernate.event.PreLoadEventListener; +//import org.hibernate.event.PreUpdateEvent; +//import org.hibernate.event.PreUpdateEventListener; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaVetoException; @@ -63,6 +62,7 @@ import org.nuiton.util.CategorisedListenerSet; import org.nuiton.util.ListenerSet; +import javax.persistence.EntityManager; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; @@ -564,7 +564,7 @@ * aucun TopiaContext n'utilise cette session. */ protected TopiaContextImplementor getContext( - TopiaContextImplementor parent, Session hibernate) { + TopiaContextImplementor parent, EntityManager hibernate) { TopiaContextImplementor result = null; // FD-20100421 : Ano #546 : no need to copy childContext, the Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -24,7 +24,6 @@ */ package org.nuiton.topia.framework; -import org.hibernate.jdbc.Work; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaRuntimeException; Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityHibernateMappingGenerator.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityHibernateMappingGenerator.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityHibernateMappingGenerator.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -488,8 +488,8 @@ /*{ column="<%=attrColumn%>" node="<%=attrName%>"<%=optionalAttributes%>}*/ if (attrIsEnumeration) { /*{> -<%=prefix%> <type name="org.hibernate.type.EnumType"> -<%=prefix%> <param name="<%=org.hibernate.type.EnumType.ENUM%>"><%=attrType%></param>}*/ +<%=prefix%> <type name="javax.persistence.EnumType"> +<%=prefix%> <param name="<%=javax.persistence.EnumType.ENUM%>"><%=attrType%></param>}*/ // if the user tuned the model to use name instead of // ordinal to store the values, we must add a clause @@ -498,7 +498,7 @@ String enumSQLType = String.valueOf(Types.VARCHAR); /*{ <%=prefix%> <!-- using name instead of ordinal to store enumeration value --> -<%=prefix%> <param name="<%=org.hibernate.type.EnumType.TYPE%>"><%=enumSQLType%></param>}*/ +<%=prefix%> <param name="<%=javax.persistence.EnumType.TYPE%>"><%=enumSQLType%></param>}*/ } /*{ Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaTagValues.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaTagValues.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaTagValues.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -331,7 +331,6 @@ * <p/> * If none given, will use the {@code org.nuiton.topia.persistence.TopiaDAOImpl}. * <p/> - * Other value possible is {@code org.nuiton.topia.persistence.TopiaDAOLegacy} * * @see TopiaGeneratorUtil#getDaoImplementationTagValue(ObjectModel) * @since 2.5 Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -453,7 +453,7 @@ * @return l'entite trouvé * @throws TopiaException if any pb while getting datas */ - E findByPrimaryKey(Map<String, Object> keys) throws TopiaException; + E findByNaturalIds(Map<String, Object> keys) throws TopiaException; /** * Recherche la classe en utilisant la cle naturelle, si la cle naturelle @@ -464,7 +464,7 @@ * @return l'entité trouvé * @throws TopiaException if any pb while getting datas */ - E findByPrimaryKey(Object... k) throws TopiaException; + E findByNaturalIds(Object... k) throws TopiaException; } //TopiaDAO Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -40,16 +40,16 @@ import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.HibernateException; -import org.hibernate.Session; -import org.hibernate.metadata.ClassMetadata; import org.nuiton.topia.TopiaException; import org.nuiton.topia.event.TopiaEntityListener; import org.nuiton.topia.event.TopiaEntityVetoable; import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.framework.TopiaQuery; -import java.io.Serializable; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceException; +import javax.persistence.metamodel.EntityType; +import javax.persistence.metamodel.Metamodel; import java.lang.reflect.InvocationTargetException; import java.security.Permission; import java.util.HashMap; @@ -93,47 +93,6 @@ } /** - * Retourne l'id de l'entity - * - * @param e l'entity - * @return l'id de l'entity ou null si pas trouvé - * @throws TopiaException Si une erreur survient durant la recherche - */ - protected Serializable getId(E e) throws TopiaException { - ClassMetadata meta = getClassMetadata(); - String idPropName = meta.getIdentifierPropertyName(); - - try { - Serializable result; - result = (Serializable) PropertyUtils.getSimpleProperty(e, - idPropName); - return result; - } catch (Exception eee) { - throw new TopiaException("Impossible de récuperer l'identifiant " - + idPropName + " de l'entite: " + e); - } - } - - /** - * Retourne l'id de l'entity representer comme une map - * - * @param map l'entity en representation map - * @return l'id de l'entity ou null si pas trouvé - * @throws TopiaException Si une erreur survient durant la recherche - */ - protected Serializable getId(Map map) throws TopiaException { - try { - ClassMetadata meta = getClassMetadata(); - String idPropName = meta.getIdentifierPropertyName(); - - Serializable id = (Serializable) map.get(idPropName); - return id; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - /** * When TopiaContextImpl create the TopiaDAOHibernate, it must call this * method just after. * @@ -223,10 +182,10 @@ entityAbstract.setTopiaContext(getContext()); // save entity - getSession().save(entity); + getSession().persist(entity); getContext().getFiresSupport().warnOnUpdateEntity(entity); return entity; - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(eee); } } @@ -295,7 +254,7 @@ // on fait un save maintenant, car puisqu'on a creer l'entity au // travers du DAO, on s'attend a l'avoir a disposition tout de // suite pour les requetes sans avoir a faire un update dessus - getSession().save(result); + getSession().persist(result); getContext().getFiresSupport().warnOnCreateEntity(result); return result; } @@ -303,10 +262,10 @@ @Override public E update(E e) throws TopiaException { try { - getSession().saveOrUpdate(e); + getSession().merge(e); getContext().getFiresSupport().warnOnUpdateEntity(e); return e; - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(eee); } } @@ -314,9 +273,9 @@ @Override public void delete(E e) throws TopiaException { try { - getSession().delete(e); + getSession().remove(e); getContext().getFiresSupport().warnOnDeleteEntity(e); - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(eee); } } @@ -546,17 +505,18 @@ } @Override - public E findByPrimaryKey(Map<String, Object> keys) + public E findByNaturalIds(Map<String, Object> keys) throws TopiaException { try { // we used hibernate meta information for all persistence type // it's more easy than create different for all persistence - ClassMetadata meta = getClassMetadata(); + EntityType<E> meta = getClassMetadata(); + // TODO AThimel 07/05/2012 See if it is possible to do it with EntityType if (meta.hasNaturalIdentifier()) { E result = findByProperties(keys); return result; } - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(eee); } throw new TopiaException("La classe " + entityClass.getName() @@ -565,12 +525,13 @@ } @Override - public E findByPrimaryKey(Object... k) throws TopiaException { + public E findByNaturalIds(Object... k) throws TopiaException { // TODO pour une meilleur gestion des problemes a la compilation // mettre un premier couple (propName, value) en argument ca evitera // de pouvoir appeler cette methode sans argument try { - ClassMetadata meta = getClassMetadata(); + EntityType<E> meta = getClassMetadata(); + // TODO AThimel 07/05/2012 See if it is possible to do it with EntityType if (meta.hasNaturalIdentifier()) { int[] ikeys = meta.getNaturalIdentifierProperties(); String[] pnames = meta.getPropertyNames(); @@ -583,7 +544,7 @@ E result = findByProperties(keys); return result; } - } catch (HibernateException eee) { + } catch (PersistenceException eee) { throw new TopiaException(eee); } throw new TopiaException("La classe " + entityClass.getName() @@ -597,7 +558,7 @@ * @return hibernate session * @throws TopiaException if any pb */ - Session getSession() throws TopiaException { + EntityManager getSession() throws TopiaException { return getContext().getHibernate(); } @@ -608,13 +569,15 @@ * @return the meta-data of the entity * @throws TopiaException if any pb */ - ClassMetadata getClassMetadata() throws TopiaException { - ClassMetadata meta = getContext().getHibernateFactory() - .getClassMetadata(entityClass); - if (meta == null) { - meta = getContext().getHibernateFactory().getClassMetadata( - getTopiaEntityEnum().getImplementationFQN()); - } + EntityType<E> getClassMetadata() throws TopiaException { + Metamodel metamodel = getContext().getHibernateFactory() + .getMetamodel(); + EntityType<E> meta = metamodel.entity(getEntityClass()); + // TODO AThimel 07/05/2012 Maybe there is no need to call this ? +// if (meta == null) { +// Class<E> implementationClass = getTopiaEntityEnum().getImplementation(); +// meta = metamodel.entity(implementationClass); +// } return meta; } Deleted: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -1,546 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -/* *##% - * ToPIA :: Persistence - * Copyright (C) 2004 - 2009 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * ##%*/ - -package org.nuiton.topia.persistence; - -import org.apache.commons.beanutils.PropertyUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.Criteria; -import org.hibernate.FlushMode; -import org.hibernate.HibernateException; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Order; -import org.hibernate.criterion.Restrictions; -import org.hibernate.metadata.ClassMetadata; -import org.nuiton.topia.TopiaException; - -import java.lang.reflect.InvocationTargetException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Surcharge du {@link TopiaDAOImpl} pour utiliser l'api criteria au lieu du hql - * pour tout ce qui est requétage. - * - * Created: 31 déc. 2005 13:10:34 - * - * @param <E> le type de l'entite - * @author bpoussin <poussin@codelutin.com> - * @author tchemit <chemit@codelutin.com> - */ - -public class TopiaDAOLegacy<E extends TopiaEntity> extends TopiaDAOImpl<E> { // TopiaDAOLegacy - - /** Logger. */ - private static Log log = LogFactory.getLog(TopiaDAOLegacy.class); - - @SuppressWarnings("unchecked") - protected E instanciateNew() throws TopiaException { - E result = newInstance(); - return result; - } - - @Override - public E create(Object... properties) throws TopiaException { - Map<String, Object> map = new HashMap<String, Object>(); - Object propertyName = null; - Object value = null; - try { - for (int i = 0; i < properties.length;) { - propertyName = properties[i++]; - value = properties[i++]; - map.put((String) propertyName, value); - } - } catch (ArrayIndexOutOfBoundsException eee) { - throw new IllegalArgumentException("Wrong number of argument " - + properties.length - + ", you must have even number. Last property name read: " - + propertyName); - } catch (ClassCastException eee) { - throw new IllegalArgumentException( - "Wrong argument type, wait property name as String and have " - + propertyName.getClass().getName()); - } - - E result = create(map); - return result; - } - - @Override - public E findByPrimaryKey(Map<String, Object> keys) - throws TopiaException { - try { - // we used hibernate meta information for all persistence type - // it's more easy than create different for all persistence - ClassMetadata meta = getClassMetadata(); - if (meta.hasNaturalIdentifier()) { - E result = findByProperties(keys); - return result; - } - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - throw new TopiaException("La classe " + entityClass.getName() - + " n'a pas de cle primaire naturelle"); - - } - - @Override - public E findByPrimaryKey(Object... k) throws TopiaException { - // TODO pour une meilleur gestion des problemes a la compilation - // mettre un premier couple (propName, value) en argument ca evitera - // de pouvoir appeler cette methode sans argument - try { - ClassMetadata meta = getClassMetadata(); - if (meta.hasNaturalIdentifier()) { - int[] ikeys = meta.getNaturalIdentifierProperties(); - String[] pnames = meta.getPropertyNames(); - - Map<String, Object> keys = new HashMap<String, Object>(); - for (int ikey : ikeys) { - keys.put(pnames[ikey], k[ikey]); - } - - E result = findByProperties(keys); - return result; - } - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - throw new TopiaException("La classe " + entityClass.getName() - + " n'a pas de cle primaire naturelle"); - - } - - @Override - public E findByProperties(String propertyName, Object value, - Object... others) throws TopiaException { - Map<String, Object> properties = new HashMap<String, Object>(); - properties.put(propertyName, value); - Object name = null; - for (int i = 0; i < others.length;) { - try { - name = others[i++]; - value = others[i++]; - properties.put((String) name, value); - } catch (ClassCastException eee) { - throw new IllegalArgumentException( - "Les noms des propriétés doivent être des chaines et non pas " - + propertyName.getClass().getName(), eee); - } catch (ArrayIndexOutOfBoundsException eee) { - throw new IllegalArgumentException( - "Le nombre d'argument n'est pas un nombre pair: " - + (others.length + 2) - + " La dernière propriété était: " + name, eee); - } - } - E result = findByProperties(properties); - return result; - } - - @Override - public List<E> findAllByProperties(String propertyName, Object value, - Object... others) throws TopiaException { - Map<String, Object> properties = new HashMap<String, Object>(); - properties.put(propertyName, value); - Object name = null; - for (int i = 0; i < others.length;) { - try { - name = others[i++]; - value = others[i++]; - properties.put((String) name, value); - } catch (ClassCastException eee) { - throw new IllegalArgumentException( - "Les noms des propriétés doivent être des chaines et non pas " - + propertyName.getClass().getName(), eee); - } catch (ArrayIndexOutOfBoundsException eee) { - throw new IllegalArgumentException( - "Le nombre d'argument n'est pas un nombre pair: " - + (others.length + 2) - + " La dernière propriété était: " + name, eee); - } - } - List<E> result = findAllByProperties(properties); - return result; - } - -// @Override -// public E findContainsProperties(Map<String, Collection<?>> properties) -// throws TopiaException { -// List<E> results = findAllContainsProperties(properties); -// E result = null; -// if (results.size() > 0) { -// result = results.get(0); -// } -// return result; -// } -// -// @Override -// public E findContainsProperties(String propertyName, -// Collection values, Object... others) throws TopiaException { -// Map<String, Collection<?>> properties = new HashMap<String, Collection<?>>(); -// properties.put(propertyName, values); -// Object name = null; -// for (int i = 0; i < others.length;) { -// try { -// name = others[i++]; -// values = (Collection) others[i++]; -// properties.put((String) name, values); -// } catch (ClassCastException eee) { -// throw new IllegalArgumentException( -// "Les noms des propriétés doivent être des chaines et non pas " -// + propertyName.getClass().getName(), eee); -// } catch (ArrayIndexOutOfBoundsException eee) { -// throw new IllegalArgumentException( -// "Le nombre d'argument n'est pas un nombre pair: " -// + (others.length + 2) -// + " La dernière propriété était: " + name, eee); -// } -// } -// E result = findContainsProperties(properties); -// return result; -// } -// -// /** -// * Find all entities with a specific rule : -// * When the entity have a Collection type property, you want to find all entites where some values are -// * contained in the collection type property. -// * Example entity parameter : private Collection<Date> historicalDates; -// * You want some dates to be contained in historicalDates. -// * Collection<Date> myDates... -// * myDates.add(date1) ... -// * Map<String, Collection> properties = new HashMap<String,Collection>(); -// * properties.put("historicalDates",myDates); -// * findAllContainsProperties(properties); -// * @param properties -// * @return the list of entities corresponding to the request -// * @throws org.nuiton.topia.TopiaException if any pb -// */ -// @Override -// public List<E> findAllContainsProperties(Map<String, Collection<?>> properties) throws TopiaException { -// List<E> all = findAll(); -// List<E> result = new ArrayList<E>(); -// for (E e : all) { -// boolean ok = true; -// try { -// for (Entry<String, Collection<?>> kv : properties.entrySet()) { -// Collection entityValues = (Collection) PropertyUtils -// .getProperty(e, kv.getKey()); -// Collection values = kv.getValue(); -// if (!entityValues.containsAll(values)) { -// ok = false; -// break; -// } -// } -// } catch (IllegalAccessException eee) { -// ok = false; -// if (log.isWarnEnabled()) { -// log.warn( -// "Impossible d'acceder a la methode demandé pour l'obbjet " -// + e, eee); -// } -// } catch (InvocationTargetException eee) { -// ok = false; -// if (log.isWarnEnabled()) { -// log.warn( -// "Impossible d'acceder a la methode demandé pour l'obbjet " -// + e, eee); -// } -// } catch (NoSuchMethodException eee) { -// ok = false; -// if (log.isWarnEnabled()) { -// log.warn( -// "Impossible d'acceder a la methode demandé pour l'obbjet " -// + e, eee); -// } -// } -// if (ok) { -// result.add(e); -// } -// } -// return result; -// } -// -// @Override -// public List<E> findAllContainsProperties(String propertyName, -// Collection values, Object... others) throws TopiaException { -// Map<String, Collection<?>> properties = new HashMap<String, Collection<?>>(); -// properties.put(propertyName, values); -// Object name = null; -// for (int i = 0; i < others.length;) { -// try { -// name = others[i++]; -// values = (Collection) others[i++]; -// properties.put((String) name, values); -// } catch (ClassCastException eee) { -// throw new IllegalArgumentException( -// "Les noms des propriétés doivent être des chaines et non pas " -// + propertyName.getClass().getName(), eee); -// } catch (ArrayIndexOutOfBoundsException eee) { -// throw new IllegalArgumentException( -// "Le nombre d'argument n'est pas un nombre pair: " -// + (others.length + 2) -// + " La dernière propriété était: " + name, eee); -// } -// } -// List<E> result = findAllContainsProperties(properties); -// return result; -// } - - @Override - public List<E> findAllByProperty(String propertyName, Object value) - throws TopiaException { - Map<String, Object> properties = new HashMap<String, Object>(); - properties.put(propertyName, value); - List<E> result = findAllByProperties(properties); - return result; - } - - @Override - public E findByProperty(String propertyName, Object value) - throws TopiaException { - Map<String, Object> properties = new HashMap<String, Object>(); - properties.put(propertyName, value); - E result = findByProperties(properties); - return result; - } - - /** - * Cette methode appelle fireVetoableCreate et fireOnCreated - * Si vous la surchargé, faites attention a appeler le super - * ou a appeler vous aussi ces deux methodes. - */ - @Override - public E create(Map<String, Object> properties) throws TopiaException { - E result = instanciateNew(); - - // TODO reflechir s'il ne faudrait pas creer l'id avant l'event precedent - // reflechir toujours dans un context on les E pourrait ne pas - // etre des TopiaEntity - if (result instanceof TopiaEntity) { - String topiaId = TopiaId.create(entityClass); - TopiaEntityAbstract entity = (TopiaEntityAbstract) result; - entity.setTopiaId(topiaId); - entity.setTopiaContext(getContext()); - } - try { - for (Map.Entry<String, Object> e : properties.entrySet()) { - String propertyName = e.getKey(); - Object value = e.getValue(); - PropertyUtils.setProperty(result, propertyName, value); - } - } catch (IllegalAccessException eee) { - throw new IllegalArgumentException( - "Can't put properties on new Object", eee); - } catch (InvocationTargetException eee) { - throw new IllegalArgumentException( - "Can't put properties on new Object", eee); - } catch (NoSuchMethodException eee) { - throw new IllegalArgumentException( - "Can't put properties on new Object", eee); - } - - // on fait un save maintenant, car puisqu'on a creer l'entity au - // travers du DAO, on s'attend a l'avoir a disposition tout de - // suite pour les requetes sans avoir a faire un update dessus - getSession().save(result); - getContext().getFiresSupport().warnOnCreateEntity(result); - return result; - } - - @Override - public E findByTopiaId(String k) throws TopiaException { - return query(Restrictions.idEq(k)); - } - - @Override - public List<E> findAll() throws TopiaException { - try { - Criteria criteria = createCriteria(FlushMode.AUTO); - List<E> result = (List<E>) criteria.list(); - result = getContext().getFiresSupport().fireEntitiesLoad(context, - result); - return result; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - @Override - public List<String> findAllIds() throws TopiaException { -// List<String> find = context.find("select src.topiaId from " + entityClass.getSimpleName() + "Impl src"); - List<String> find = context.find("select src.topiaId from " + getEntityClass() + " src"); - return find; - } - - - - @Override - public List<E> findAllWithOrder(String... propertyNames) - throws TopiaException { - try { - Criteria criteria = createCriteria(FlushMode.AUTO); - for (String propertyName : propertyNames) { - criteria.addOrder(Order.asc(propertyName)); - } - List<E> result = (List<E>) criteria.list(); - result = getContext().getFiresSupport().fireEntitiesLoad(context, - result); - return result; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - @Override - public E findByProperties(Map<String, Object> properties) - throws TopiaException { - return query(Restrictions.allEq(properties)); - } - - @Override - public List<E> findAllByProperties(Map<String, Object> properties) - throws TopiaException { - return queryAll(Restrictions.allEq(properties)); - } - - private List<E> queryAll(Criterion criterion) throws TopiaException { - try { - Criteria criteria = createCriteria(FlushMode.AUTO); - criteria.add(criterion); - List<E> result = (List<E>) criteria.list(); - result = getContext().getFiresSupport().fireEntitiesLoad(context, - result); - return result; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - private E query(Criterion criterion) throws TopiaException { - try { - Criteria criteria = createCriteria(FlushMode.AUTO); - criteria.add(criterion); - criteria.setMaxResults(1); - List<E> result = (List<E>) criteria.list(); - int sizeBefore = result != null ? result.size() : 0; - result = getContext().getFiresSupport().fireEntitiesLoad(context, - result); - int sizeAfter = result != null ? result.size() : 0; - if (sizeAfter < sizeBefore) { - if (log.isDebugEnabled()) { - log.debug((sizeBefore - sizeAfter) - + " element(s) removed. Filter entity: " - + entityClass.getName() + " - criterion: " - + criterion); - } - } - if (result != null && result.size() > 0) { - E elem = result.get(0); - return elem; - } - return null; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - //FIXME : Commenté car impossible de trouver le bon Criterion - // ATTENTION ancienne methode du TopiaDAOAbstract deja presente dans le fichier - - // /* (non-Javadoc) - // * @see org.nuiton.topia.persistence.TopiaDAO#findContainsProperties(java.util.Map) - // */ - // public E findContainsProperties(Map<String, Collection> properties) throws TopiaException { - // try { - // Criteria criteria = createCriteria(FlushMode.AUTO); - // for (Entry<String, Collection> entry : properties.entrySet()) { - // for (Object value : entry.getValue()) { - // criteria.add(Restrictions.eq(entry.getKey(), value)); - // } - // } - // criteria.setMaxResults(1); - // List<E> results = (List<E>)criteria.list(); - // if (results.size() > 0) { - // return (E)results.get(0); - // } else { - // return null; - // } - // } catch (HibernateException eee) { - // throw new TopiaException(eee); - // } - // } - // - // /* (non-Javadoc) - // * @see org.nuiton.topia.persistence.TopiaDAO#findAllContainsProperties(java.util.Map) - // */ - // public List<E> findAllContainsProperties(Map<String, Collection> properties) throws TopiaException { - // try { - // Criteria criteria = createCriteria(FlushMode.AUTO); - // for (Entry<String, Collection> entry : properties.entrySet()) { - // for (Object value : entry.getValue()) { - // criteria.add(Restrictions.eq(entry.getKey(), value)); - // } - // } - // List<E> result = (List<E>)criteria.list(); - // return result; - // } catch (HibernateException eee) { - // throw new TopiaException(eee); - // } - // } - - /** - * Renvoie un Criteria créé avec l'entityClass - * @param mode le FlushMode du Criteria - * @return le Criteria nouvellement créé - * @throws TopiaException if any pb - */ - private Criteria createCriteria(FlushMode mode) throws TopiaException { - Criteria criteria = getSession().createCriteria(entityClass); - criteria.setFlushMode(mode); - return criteria; - } - -} //TopiaDAOLegacy Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/DBMapping.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/DBMapping.java 2012-05-07 10:27:52 UTC (rev 2443) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/DBMapping.java 2012-05-07 15:52:54 UTC (rev 2444) @@ -27,13 +27,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.exception.SQLGrammarException; +//import org.hibernate.exception.SQLGrammarException; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaContextImpl; import org.nuiton.topia.persistence.TopiaEntity; -import org.hibernate.SQLQuery; -import org.hibernate.jdbc.Work; +//import org.hibernate.SQLQuery; +//import org.hibernate.jdbc.Work; import java.io.IOException; import java.io.InputStream;
participants (1)
-
athimel@users.nuiton.org