Author: epbernard
Date: 2007-02-19 14:36:40 -0500 (Mon, 19 Feb 2007)
New Revision: 11218
Added:
branches/Branch_3_2/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java
Modified:
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/Ejb3Configuration.java
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/EventListenerConfigurator.java
Log:
EJB-257 Ejb3configuration can be build wocalling configure(String), nor any of those
methods
Modified:
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/Ejb3Configuration.java
===================================================================
---
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/Ejb3Configuration.java 2007-02-19
15:41:06 UTC (rev 11217)
+++
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/Ejb3Configuration.java 2007-02-19
19:36:40 UTC (rev 11218)
@@ -102,6 +102,7 @@
private static final String META_INF_ORM_XML = "META-INF/orm.xml";
private static Log log = LogFactory.getLog( Ejb3Configuration.class );
private static EntityNotFoundDelegate ejb3EntityNotFoundDelegate = new
Ejb3EntityNotFoundDelegate();
+ private static Configuration DEFAULT_CONFIGURATION = new AnnotationConfiguration();
private static class Ejb3EntityNotFoundDelegate implements EntityNotFoundDelegate,
Serializable {
public void handleEntityNotFound(String entityName, Serializable id) {
@@ -121,6 +122,7 @@
private boolean discardOnClose;
//made transient and not restored in deserialization on purpose, should no longer be
called after restoration
private transient ClassLoader overridenClassLoader;
+ private boolean isConfigurationProcessed = false;
public Ejb3Configuration() {
@@ -719,6 +721,7 @@
thread.setContextClassLoader( overridenClassLoader );
}
try {
+ configure( (Properties)null, null );
NamingHelper.bind(this);
return new EntityManagerFactoryImpl(
cfg.buildSessionFactory(),
@@ -769,6 +772,9 @@
private Ejb3Configuration configure(
Properties properties, Map workingVars
) {
+ //TODO check for people calling more than once this method (except buildEMF)
+ if (isConfigurationProcessed) return this;
+ isConfigurationProcessed = true;
Properties preparedProperties = prepareProperties( properties, workingVars );
if ( workingVars == null ) workingVars = CollectionHelper.EMPTY_MAP;
@@ -785,9 +791,8 @@
List<String> jaccKeys = new ArrayList<String>();
- Configuration defaultConf = new AnnotationConfiguration();
- Interceptor defaultInterceptor = defaultConf.getInterceptor();
- NamingStrategy defaultNamingStrategy = defaultConf.getNamingStrategy();
+ Interceptor defaultInterceptor = DEFAULT_CONFIGURATION.getInterceptor();
+ NamingStrategy defaultNamingStrategy = DEFAULT_CONFIGURATION.getNamingStrategy();
Iterator propertyIt = preparedProperties.keySet().iterator();
while ( propertyIt.hasNext() ) {
@@ -991,7 +996,12 @@
preparedProperties.setProperty( HibernatePersistence.DISCARD_PC_ON_CLOSE,
"false" );
//override the new defaults with the user defined ones
+ //copy programmatically defined properties
+ if ( cfg.getProperties() != null ) preparedProperties.putAll( cfg.getProperties() );
+ //copy them co;ing from configuration
if ( properties != null ) preparedProperties.putAll( properties );
+ //note we don't copy cfg.xml properties, since they have to be overriden
+
if (transactionType == null) {
//if it has not been set, the user use a programmatic way
transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
Modified:
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/EventListenerConfigurator.java
===================================================================
---
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2007-02-19
15:41:06 UTC (rev 11217)
+++
branches/Branch_3_2/HibernateExt/entitymanager/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2007-02-19
19:36:40 UTC (rev 11218)
@@ -144,7 +144,6 @@
configuration.setListeners( type, listeners.toArray( new String[ listeners.size() ] )
);
}
}
- ;
}
public void configure() {
Added:
branches/Branch_3_2/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java
(rev 0)
+++
branches/Branch_3_2/HibernateExt/entitymanager/src/test/org/hibernate/ejb/test/ejb3configuration/ProgrammaticConfTest.java 2007-02-19
19:36:40 UTC (rev 11218)
@@ -0,0 +1,57 @@
+//$Id: $
+package org.hibernate.ejb.test.ejb3configuration;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.Properties;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.hibernate.cfg.Environment;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.hibernate.ejb.test.Cat;
+import org.hibernate.util.ConfigHelper;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class ProgrammaticConfTest extends junit.framework.TestCase {
+
+ public void testProgrammaticAPI() throws Exception {
+ Ejb3Configuration conf = new Ejb3Configuration();
+ conf.addAnnotatedClass( Cat.class );
+ 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();
+ }
+
+ protected Properties getProperties() throws IOException {
+ Properties properties = new Properties( );
+ InputStream stream =
ConfigHelper.getResourceAsStream("/hibernate.properties");
+ try {
+ properties.load(stream);
+ }
+
+ finally {
+ try{
+ stream.close();
+ }
+ catch (IOException ioe){
+ }
+ }
+ properties.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
+ return properties;
+ }
+
+
+}
Show replies by date