From: <hib...@li...> - 2006-05-02 23:33:27
|
Author: epbernard Date: 2006-05-02 19:33:15 -0400 (Tue, 02 May 2006) New Revision: 9859 Added: trunk/HibernateExt/ejb/src/test-resources/excludehbmpar/orm2.xml Removed: trunk/HibernateExt/ejb/src/test-resources/explicitpar/orm2.xml Modified: trunk/HibernateExt/ejb/lib/ejb3-persistence.jar trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java trunk/HibernateExt/ejb/src/test-resources/excludehbmpar/META-INF/persistence.xml trunk/HibernateExt/ejb/src/test-resources/explicitpar/META-INF/persistence.xml trunk/HibernateExt/ejb/src/test/log4j.properties trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/Wallet.java trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/xml/orm2.xml Log: EJB-169 META-INF was mandatory EJB-170 locate files by jar parsing before resource location start a cleaner error handling with PU name Modified: trunk/HibernateExt/ejb/lib/ejb3-persistence.jar =================================================================== (Binary files differ) Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java 2006-05-02 23:33:15 UTC (rev 9859) @@ -105,16 +105,16 @@ private EntityManagerFactory createFactory(PersistenceMetadata metadata, Map overrides) { log.debug( "Creating Factory: " + metadata.getName() ); + Map workingVars = new HashMap(); + workingVars.put( HibernatePersistence.PERSISTENCE_UNIT_NAME, metadata.getName() ); + if ( StringHelper.isNotEmpty( metadata.getJtaDatasource() ) ) { this.setProperty( Environment.DATASOURCE, metadata.getJtaDatasource() ); } else if ( StringHelper.isNotEmpty( metadata.getNonJtaDatasource() ) ) { this.setProperty( Environment.DATASOURCE, metadata.getNonJtaDatasource() ); } - - Map workingVars = new HashMap(); - //workingVars.put( HibernatePersistence.TRANSACTION_TYPE, metadata.getTransactionType() ); - defineTransactionType( metadata.getTransactionType(), metadata.getName() ); + defineTransactionType( metadata.getTransactionType(), workingVars ); if ( metadata.getClasses().size() > 0 ) { workingVars.put( HibernatePersistence.CLASS_NAMES, metadata.getClasses() ); } @@ -130,7 +130,6 @@ Properties props = new Properties(); props.putAll( metadata.getProps() ); if ( overrides != null ) props.putAll( overrides ); //yuk! - workingVars.put( HibernatePersistence.PERSISTENCE_UNIT_NAME, metadata.getName() ); return createEntityManagerFactory( props, workingVars ); } @@ -161,7 +160,7 @@ cfg.getEntityResolver() ); for ( PersistenceMetadata metadata : metadataFiles ) { - JarVisitor.Filter[] filters = getFilters( metadata.getProps(), integration ); + JarVisitor.Filter[] filters = getFilters( metadata.getProps(), integration, metadata.getMappingFiles() ); if ( metadata.getProvider() == null || IMPLEMENTATION_NAME.equalsIgnoreCase( metadata.getProvider() @@ -193,7 +192,12 @@ return null; } catch (Exception e) { - throw new PersistenceException( e ); + if ( e instanceof PersistenceException) { + throw (PersistenceException) e; + } + else { + throw new PersistenceException( e ); + } } } @@ -216,6 +220,7 @@ } else if ( filters[index] instanceof JarVisitor.FileFilter ) { hbmFiles.add( entry.getInputStream() ); + metadata.getMappingFiles().remove( entry.getName() ); } } } @@ -240,7 +245,9 @@ return null; } if ( info.getClassLoader() == null ) { - throw new IllegalStateException( "PersistenceUnitInfo.getClassLoader() id null" ); + throw new IllegalStateException( + "[PersistenceUnit: " + info.getPersistenceUnitName() == null ? "" : info.getPersistenceUnitName() + + "] " + "PersistenceUnitInfo.getClassLoader() id null" ); } //set the classloader Thread thread = Thread.currentThread(); @@ -265,7 +272,7 @@ // defineTransactionType( overridenTxType, info.getPersistenceUnitName() ); // } // else { - defineTransactionType( info.getTransactionType(), info.getPersistenceUnitName() ); + defineTransactionType( info.getTransactionType(), workingVars ); // } //workingVars.put( HibernatePersistence.TRANSACTION_TYPE, transactionType ); boolean[] detectArtifact = getDetectedArtifacts( info.getProperties(), null ); @@ -326,7 +333,7 @@ return entityManagerFactory; } - private void defineTransactionType(Object overridenTxType, String persistenceUnitName) { + private void defineTransactionType(Object overridenTxType, Map workingVars) { if ( overridenTxType == null ) { if ( transactionType == null ) { transactionType = PersistenceUnitTransactionType.JTA; //this is the default value @@ -339,9 +346,8 @@ transactionType = (PersistenceUnitTransactionType) overridenTxType; } else { - throw new PersistenceException( + throw new PersistenceException( getExceptionHeader( workingVars ) + HibernatePersistence.TRANSACTION_TYPE + " of the wrong class type" - + ( persistenceUnitName != null ? " in unit " + persistenceUnitName : "" ) + ": " + overridenTxType.getClass() ); } @@ -373,7 +379,7 @@ return result; } - private JarVisitor.Filter[] getFilters(Properties properties, Map overridenProperties) { + private JarVisitor.Filter[] getFilters(Properties properties, Map overridenProperties, final List<String> mappingFiles) { boolean[] result = getDetectedArtifacts( properties, overridenProperties ); int size = ( result[0] ? 2 : 0 ) + 1; //( result[1] ? 1 : 0 ); JarVisitor.Filter[] filters = new JarVisitor.Filter[size]; @@ -397,14 +403,19 @@ if ( result[1] ) { filters[size - 1] = new JarVisitor.FileFilter( true ) { public boolean accept(String javaElementName) { - return javaElementName.endsWith( "hbm.xml" ) || javaElementName.endsWith( META_INF_ORM_XML ); + log.fatal( javaElementName + "O-:-O" + mappingFiles.contains( javaElementName ) ); + return javaElementName.endsWith( "hbm.xml" ) + || javaElementName.endsWith( META_INF_ORM_XML ) + || mappingFiles.contains( javaElementName ); } }; } else { filters[size - 1] = new JarVisitor.FileFilter( true ) { public boolean accept(String javaElementName) { - return javaElementName.endsWith( META_INF_ORM_XML ); + log.fatal( javaElementName + "O:O" + mappingFiles.contains( javaElementName ) ); + return javaElementName.endsWith( META_INF_ORM_XML ) + || mappingFiles.contains( javaElementName ); } }; } @@ -559,10 +570,10 @@ if ( uncastObject != null && uncastObject instanceof String ) { String propertyKey = (String) uncastObject; if ( propertyKey.startsWith( HibernatePersistence.CLASS_CACHE_PREFIX ) ) { - setCacheStrategy( propertyKey, preparedProperties, true ); + setCacheStrategy( propertyKey, preparedProperties, true, workingVars ); } else if ( propertyKey.startsWith( HibernatePersistence.COLLECTION_CACHE_PREFIX ) ) { - setCacheStrategy( propertyKey, preparedProperties, false ); + setCacheStrategy( propertyKey, preparedProperties, false, workingVars ); } else if ( propertyKey.startsWith( HibernatePersistence.JACC_PREFIX ) && ! ( propertyKey.equals( HibernatePersistence.JACC_CONTEXT_ID ) @@ -581,21 +592,23 @@ cfg.setInterceptor( (Interceptor) interceptor.newInstance() ); } catch (ClassNotFoundException e) { - throw new PersistenceException( "Unable to find interceptor class: " + interceptorName, e ); + throw new PersistenceException( + getExceptionHeader(workingVars) + "Unable to find interceptor class: " + interceptorName, e + ); } catch (IllegalAccessException e) { throw new PersistenceException( - "Unable to access interceptor class: " + interceptorName, e + getExceptionHeader(workingVars) + "Unable to access interceptor class: " + interceptorName, e ); } catch (InstantiationException e) { throw new PersistenceException( - "Unable to instanciate interceptor class: " + interceptorName, e + getExceptionHeader(workingVars) + "Unable to instanciate interceptor class: " + interceptorName, e ); } catch (ClassCastException e) { throw new PersistenceException( - "Interceptor class does not implement Interceptor interface: " + interceptorName, e + getExceptionHeader(workingVars) + "Interceptor class does not implement Interceptor interface: " + interceptorName, e ); } } @@ -610,29 +623,29 @@ } catch (ClassNotFoundException e) { throw new PersistenceException( - "Unable to find naming strategy class: " + namingStrategyName, e + getExceptionHeader(workingVars) + "Unable to find naming strategy class: " + namingStrategyName, e ); } catch (IllegalAccessException e) { throw new PersistenceException( - "Unable to access naming strategy class: " + namingStrategyName, e + getExceptionHeader(workingVars) + "Unable to access naming strategy class: " + namingStrategyName, e ); } catch (InstantiationException e) { throw new PersistenceException( - "Unable to instanciate naming strategy class: " + namingStrategyName, e + getExceptionHeader(workingVars) + "Unable to instanciate naming strategy class: " + namingStrategyName, e ); } catch (ClassCastException e) { throw new PersistenceException( - "Naming strategyy class does not implement NmaingStrategy interface: " + namingStrategyName, + getExceptionHeader(workingVars) + "Naming strategyy class does not implement NmaingStrategy interface: " + namingStrategyName, e ); } } if ( jaccKeys.size() > 0 ) { - addSecurity( jaccKeys, preparedProperties ); + addSecurity( jaccKeys, preparedProperties, workingVars ); } //initialize listeners @@ -655,7 +668,7 @@ Collection<String> classNames = (Collection<String>) workingVars.get( HibernatePersistence.CLASS_NAMES ); - addNamedAnnotatedClasses( this, classNames ); + addNamedAnnotatedClasses( this, classNames, workingVars ); } if ( workingVars.containsKey( HibernatePersistence.LOADED_CLASSES ) ) { Collection<Class> classes = (Collection<Class>) workingVars.get( HibernatePersistence.LOADED_CLASSES ); @@ -676,7 +689,29 @@ HibernatePersistence.XML_FILE_NAMES ); for ( String xmlFile : xmlFiles ) { - cfg.addResource( xmlFile ); + Boolean useMetaInf = null; + try { + if ( xmlFile.endsWith( META_INF_ORM_XML ) ) useMetaInf = true; + cfg.addResource( xmlFile ); + } + catch( MappingException me ) { + if ( ! xmlFile.endsWith( META_INF_ORM_XML ) ) { + throw new PersistenceException( getExceptionHeader(workingVars) + + "Unable to find XML mapping file in classpath: " + xmlFile); + } + else { + useMetaInf = false; + //swallow it, the META-INF/orm.xml is optional + } + } + if ( log.isInfoEnabled() ) { + if ( Boolean.TRUE.equals( useMetaInf ) ) { + log.info( getExceptionHeader( workingVars ) + META_INF_ORM_XML + " found"); + } + else if (Boolean.FALSE.equals( useMetaInf ) ) { + log.info( getExceptionHeader( workingVars ) + "no " + META_INF_ORM_XML + " found"); + } + } } } if ( workingVars.containsKey( HibernatePersistence.HBXML_FILES ) ) { @@ -689,6 +724,18 @@ } } + private String getExceptionHeader(Map workingVars) { + if ( workingVars != null ) { + String puName = (String) workingVars.get( HibernatePersistence.PERSISTENCE_UNIT_NAME); + puName = puName == null ? "" : puName; + String header = "[PersistenceUnit: " + puName + "] "; + return header; + } + else { + return ""; + } + } + private Properties prepareProperties(Properties properties, Map workingVars) { Properties preparedProperties = new Properties(); @@ -704,7 +751,7 @@ if ( properties != null ) preparedProperties.putAll( properties ); defineTransactionType( preparedProperties.getProperty( HibernatePersistence.TRANSACTION_TYPE ), - (String) workingVars.get( HibernatePersistence.PERSISTENCE_UNIT_NAME ) + workingVars ); boolean hasTxStrategy = StringHelper.isNotEmpty( preparedProperties.getProperty( Environment.TRANSACTION_STRATEGY ) @@ -729,13 +776,11 @@ return preparedProperties; } - private Class classForName( - String className - ) throws ClassNotFoundException { + private Class classForName(String className) throws ClassNotFoundException { return ReflectHelper.classForName( className, this.getClass() ); } - private void setCacheStrategy(String propertyKey, Map properties, boolean isClass) { + private void setCacheStrategy(String propertyKey, Map properties, boolean isClass, Map workingVars) { String role = propertyKey.substring( ( isClass ? HibernatePersistence.CLASS_CACHE_PREFIX .length() : HibernatePersistence.COLLECTION_CACHE_PREFIX.length() ) @@ -750,7 +795,7 @@ isClass ? HibernatePersistence.CLASS_CACHE_PREFIX : HibernatePersistence.COLLECTION_CACHE_PREFIX ); error.append( ": " ).append( propertyKey ).append( " " ).append( value ); - throw new MappingException( error.toString() ); + throw new PersistenceException( getExceptionHeader(workingVars) + error.toString() ); } String usage = params.nextToken(); String region = null; @@ -769,10 +814,10 @@ } } - private void addSecurity(List<String> keys, Map properties) { + private void addSecurity(List<String> keys, Map properties, Map workingVars) { log.debug( "Adding security" ); if ( !properties.containsKey( HibernatePersistence.JACC_CONTEXT_ID ) ) { - throw new MappingException( + throw new PersistenceException( getExceptionHeader(workingVars) + "Entities have been configured for JACC, but " + HibernatePersistence.JACC_CONTEXT_ID + " has not been set" @@ -793,13 +838,14 @@ jaccCfg.addPermission( role, clazz, actions ); } catch (IndexOutOfBoundsException e) { - throw new MappingException( "Illegal usage of " + HibernatePersistence.JACC_PREFIX + ": " + key ); + throw new PersistenceException( getExceptionHeader(workingVars) + + "Illegal usage of " + HibernatePersistence.JACC_PREFIX + ": " + key ); } } } private void addNamedAnnotatedClasses( - Ejb3Configuration cfg, Collection<String> classNames + Ejb3Configuration cfg, Collection<String> classNames, Map workingVars ) { for ( String name : classNames ) { try { @@ -815,7 +861,7 @@ pkg = null; } if ( pkg == null ) { - throw new IllegalArgumentException( "class or package not found", cnfe ); + throw new PersistenceException( getExceptionHeader(workingVars) + "class or package not found", cnfe ); } else { cfg.addPackage( name ); Modified: trunk/HibernateExt/ejb/src/test/log4j.properties =================================================================== --- trunk/HibernateExt/ejb/src/test/log4j.properties 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test/log4j.properties 2006-05-02 23:33:15 UTC (rev 9859) @@ -18,6 +18,7 @@ #log4j.logger.org.hibernate=debug log4j.logger.org.hibernate.ejb=debug +log4j.logger.org.hibernate.reflection=debug #log4j.logger.org.hibernate.engine.Cascades=debug #log4j.logger.org.hibernate.hql=debug @@ -26,8 +27,8 @@ log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### -log4j.logger.org.hibernate.type=info -#log4j.logger.org.hibernate.type=debug +#log4j.logger.org.hibernate.type=info +log4j.logger.org.hibernate.type=debug ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug Modified: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/PackagedEntityManagerTest.java 2006-05-02 23:33:15 UTC (rev 9859) @@ -95,7 +95,7 @@ nested = nested.getCause(); if ( nested == null ) throw e; if ( ! ( nested instanceof ClassNotFoundException ) ) throw e; - fail( "Try to process hbm file" ); + fail( "Try to process hbm file: " + e.getMessage() ); } EntityManager em = emf.createEntityManager(); Caipirinha s = new Caipirinha( "Strong" ); Modified: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java 2006-05-02 23:33:15 UTC (rev 9859) @@ -3,8 +3,10 @@ import java.util.ArrayList; import java.util.List; +import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.Query; +import javax.persistence.TemporalType; import org.hibernate.Hibernate; @@ -182,6 +184,10 @@ query.setParameter( 1, "Lacoste" ); w = (Wallet) query.getSingleResult(); assertNotNull( w ); + query = em.createQuery( "select w from " + Wallet.class.getName() + " w where w.marketEntrance = ?1" ); + query.setParameter( 1, new Date(), TemporalType.DATE ); + //assertNull( query.getSingleResult() ); + assertEquals( 0, query.getResultList().size() ); em.remove( w ); em.getTransaction().commit(); em.close(); Modified: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/Wallet.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/Wallet.java 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/Wallet.java 2006-05-02 23:33:15 UTC (rev 9859) @@ -2,6 +2,7 @@ package org.hibernate.ejb.test; import java.io.Serializable; +import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; @@ -12,6 +13,7 @@ public class Wallet implements Serializable { private String serial; private String model; + private Date marketEntrance; private String brand; @Id @@ -38,4 +40,12 @@ public void setBrand(String brand) { this.brand = brand; } + + public Date getMarketEntrance() { + return marketEntrance; + } + + public void setMarketEntrance(Date marketEntrance) { + this.marketEntrance = marketEntrance; + } } Modified: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/xml/orm2.xml =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/xml/orm2.xml 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/xml/orm2.xml 2006-05-02 23:33:15 UTC (rev 9859) @@ -5,8 +5,8 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" version="1.0" > - <package>org.hibernate.ejb.test.pack.defaultpar</package> - <entity class="Lighter" access="FIELD" metadata-complete="true"> + <package>org.hibernate.ejb.test.xml</package> + <entity class="Lighter" name="ALighter" access="FIELD" metadata-complete="true"> <attributes> <id name="name"> <column name="fld_id"/> Modified: trunk/HibernateExt/ejb/src/test-resources/excludehbmpar/META-INF/persistence.xml =================================================================== --- trunk/HibernateExt/ejb/src/test-resources/excludehbmpar/META-INF/persistence.xml 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test-resources/excludehbmpar/META-INF/persistence.xml 2006-05-02 23:33:15 UTC (rev 9859) @@ -5,6 +5,7 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="excludehbmpar" transaction-type="RESOURCE_LOCAL"> + <mapping-file>orm2.xml</mapping-file> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> Copied: trunk/HibernateExt/ejb/src/test-resources/excludehbmpar/orm2.xml (from rev 9841, trunk/HibernateExt/ejb/src/test-resources/explicitpar/orm2.xml) =================================================================== --- trunk/HibernateExt/ejb/src/test-resources/explicitpar/orm2.xml 2006-05-01 03:56:56 UTC (rev 9841) +++ trunk/HibernateExt/ejb/src/test-resources/excludehbmpar/orm2.xml 2006-05-02 23:33:15 UTC (rev 9859) @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" + version="1.0" + > + <package>org.hibernate.ejb.test.xml</package> + <entity class="Light" access="FIELD" metadata-complete="true"> + <attributes> + <id name="name"> + <column name="fld_id"/> + </id> + <basic name="power"></basic> + </attributes> + </entity> +</entity-mappings> \ No newline at end of file Modified: trunk/HibernateExt/ejb/src/test-resources/explicitpar/META-INF/persistence.xml =================================================================== --- trunk/HibernateExt/ejb/src/test-resources/explicitpar/META-INF/persistence.xml 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test-resources/explicitpar/META-INF/persistence.xml 2006-05-02 23:33:15 UTC (rev 9859) @@ -4,13 +4,11 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL"> - <mapping-file>orm2.xml</mapping-file> <jar-file>./build/testresources/externaljar.jar</jar-file> <class>org.hibernate.ejb.test.Cat</class> <class>org.hibernate.ejb.test.Distributor</class> <class>org.hibernate.ejb.test.Item</class> <class>org.hibernate.ejb.test</class> - <class>org.hibernate.ejb.test.xml.Light</class> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> Deleted: trunk/HibernateExt/ejb/src/test-resources/explicitpar/orm2.xml =================================================================== --- trunk/HibernateExt/ejb/src/test-resources/explicitpar/orm2.xml 2006-05-02 22:29:55 UTC (rev 9858) +++ trunk/HibernateExt/ejb/src/test-resources/explicitpar/orm2.xml 2006-05-02 23:33:15 UTC (rev 9859) @@ -1,17 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> - -<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" - version="1.0" - > - <package>org.hibernate.test.annotations.xml.ejb3</package> - <entity class="Light" access="FIELD" metadata-complete="true"> - <attributes> - <id name="name"> - <column name="fld_id"/> - </id> - <basic name="power"></basic> - </attributes> - </entity> -</entity-mappings> \ No newline at end of file |