[hibernate-commits] Hibernate SVN: r14396 - in entitymanager/trunk: src/java/org/hibernate/ejb and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Mar 6 15:24:03 EST 2008


Author: epbernard
Date: 2008-03-06 15:24:03 -0500 (Thu, 06 Mar 2008)
New Revision: 14396

Added:
   entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/hibernate.cfg.xml
Modified:
   entitymanager/trunk/doc/reference/en/modules/configuration.xml
   entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java
   entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java
Log:
EJB-330 configure(String) now is lazily executed

Modified: entitymanager/trunk/doc/reference/en/modules/configuration.xml
===================================================================
--- entitymanager/trunk/doc/reference/en/modules/configuration.xml	2008-03-06 17:04:50 UTC (rev 14395)
+++ entitymanager/trunk/doc/reference/en/modules/configuration.xml	2008-03-06 20:24:03 UTC (rev 14396)
@@ -434,13 +434,13 @@
 
       <programlisting>Ejb3Configuration cfg = new Ejb3Configuration();
 EntityManagerFactory emf = 
-  cfg.configure("/mypath/hibernate.cfg.xml") //add a regular hibernate.cfg.xml
-     .addProperties( properties ) //add some properties
+  cfg.addProperties( properties ) //add some properties
      .setInterceptor( myInterceptorImpl ) // set an interceptor
      .addAnnotatedClass( MyAnnotatedClass.class ) //add a class to be mapped
      .addClass( NonAnnotatedClass.class ) //add an hbm.xml file using the Hibernate convention
      .addRerousce( "mypath/MyOtherCLass.hbm.xml ) //add an hbm.xml file
      .addRerousce( "mypath/orm.xml ) //add an EJB3 deployment descriptor
+     .configure("/mypath/hibernate.cfg.xml") //add a regular hibernate.cfg.xml
      .buildEntityManagerFactory(); //Create the entity manager factory</programlisting>
     </section>
   </section>

Modified: entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java	2008-03-06 17:04:50 UTC (rev 14395)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/Ejb3Configuration.java	2008-03-06 20:24:03 UTC (rev 14396)
@@ -106,6 +106,7 @@
 	private static EntityNotFoundDelegate ejb3EntityNotFoundDelegate = new Ejb3EntityNotFoundDelegate();
 	private static Configuration DEFAULT_CONFIGURATION = new AnnotationConfiguration();
 	private String persistenceUnitName;
+	private String cfgXmlResource;
 
 	private static class Ejb3EntityNotFoundDelegate implements EntityNotFoundDelegate, Serializable {
 		public void handleEntityNotFound(String entityName, Serializable id) {
@@ -960,6 +961,10 @@
 		preparedProperties.setProperty( Environment.USE_IDENTIFIER_ROLLBACK, "false" );
 		preparedProperties.setProperty( Environment.FLUSH_BEFORE_COMPLETION, "false" );
 		preparedProperties.setProperty( HibernatePersistence.DISCARD_PC_ON_CLOSE, "false" );
+		if (cfgXmlResource != null) {
+			preparedProperties.setProperty( HibernatePersistence.CFG_FILE, cfgXmlResource );
+			cfgXmlResource = null;
+		}
 
 		//override the new defaults with the user defined ones
 		//copy programmatically defined properties
@@ -1133,22 +1138,11 @@
 	}
 
 	public Ejb3Configuration configure(String resource) throws HibernateException {
-		Thread thread = null;
-		ClassLoader contextClassLoader = null;
-		if (overridenClassLoader != null) {
-			thread = Thread.currentThread();
-			contextClassLoader = thread.getContextClassLoader();
-			thread.setContextClassLoader( overridenClassLoader );
-		}
-		try {
-			Properties properties = new Properties();
-			properties.setProperty( HibernatePersistence.CFG_FILE, resource);
-			configure( properties, new HashMap() );
-			return this;
-		}
-		finally {
-			if (thread != null) thread.setContextClassLoader( contextClassLoader );
-		}
+		//delay the call to configure to allow proper addition of all annotated classes (EJB-330)
+		if (cfgXmlResource != null)
+			throw new PersistenceException("configure(String) method already called for " + cfgXmlResource);
+		this.cfgXmlResource = resource;
+		return this;
 	}
 
 	public Ejb3Configuration addPackage(String packageName) throws MappingException {

Modified: entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java	2008-03-06 17:04:50 UTC (rev 14395)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java	2008-03-06 20:24:03 UTC (rev 14396)
@@ -36,6 +36,24 @@
 		emf.close();
 	}
 
+	public void testProgrammaticCfg() throws Exception {
+		Ejb3Configuration conf = new Ejb3Configuration();
+		conf.configure( "org/hibernate/ejb/test/ejb3configuration/hibernate.cfg.xml" );
+		EntityManagerFactory emf = conf.buildEntityManagerFactory();
+		EntityManager em = emf.createEntityManager();
+		Cat cat = new Cat();
+		cat.setAge( 23 );
+		cat.setDateOfBirth( new Date() );
+		cat.setLength( 32 );
+		cat.setName( "Tomy" );
+		em.getTransaction().begin();
+		em.persist( cat );
+		em.flush();
+		assertNotNull( em.find(Cat.class, cat.getId() ) );
+		em.getTransaction().rollback();
+		emf.close();
+	}
+
 	protected Properties getProperties() throws IOException {
 		Properties properties = new Properties( );
 		InputStream stream = ConfigHelper.getResourceAsStream("/hibernate.properties");

Added: entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/hibernate.cfg.xml
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/hibernate.cfg.xml	                        (rev 0)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/hibernate.cfg.xml	2008-03-06 20:24:03 UTC (rev 14396)
@@ -0,0 +1,9 @@
+<!DOCTYPE hibernate-configuration PUBLIC
+        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+    <session-factory>
+        <mapping class="org.hibernate.ejb.test.Cat"/>
+    </session-factory>
+</hibernate-configuration>
\ No newline at end of file




More information about the hibernate-commits mailing list