Author: drobiazko Date: Wed May 25 18:52:38 2011 New Revision: 1127628 URL: http://svn.apache.org/viewvc?rev=1127628&view=rev Log: Provided a way to bootstrap JPA progtrammatically without persitence.xml file
Added: tapestry/tapestry5/trunk/tapestry-jpa/src/test/app1/PersistThang.tml tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/entities/Thang.java (with props) tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/pages/PersistThang.java (with props) Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceContentHandler.java tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/AppConstants.java tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/services/AppModule.java tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/META-INF/persistence.xml Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java Wed May 25 18:52:38 2011 @@ -53,6 +53,7 @@ public class EntityManagerSourceImpl imp super(); this.logger = logger; + List<TapestryPersistenceUnitInfo> persistenceUnitInfos = null; final PersistenceParser parser = new PersistenceParser(); InputStream inputStream = null; @@ -70,19 +71,50 @@ public class EntityManagerSourceImpl imp InternalUtils.close(inputStream); } + final Map<String, PersistenceUnitConfigurer> remainingConfigurations = configure(configuration, persistenceUnitInfos); + + configureRemaining(persistenceUnitInfos, remainingConfigurations); + if(persistenceUnitInfos.size() == 1) packageNamePersistenceUnitConfigurer.configure(persistenceUnitInfos.get(0)); - + + this.persistenceUnitInfos = persistenceUnitInfos; + } + + private Map<String, PersistenceUnitConfigurer> configure(Map<String, PersistenceUnitConfigurer> configuration, List<TapestryPersistenceUnitInfo> persistenceUnitInfos) + { + final Map<String, PersistenceUnitConfigurer> remainingConfigurations = CollectionFactory.newMap(configuration); + for (final TapestryPersistenceUnitInfo info : persistenceUnitInfos) { - final PersistenceUnitConfigurer configurer = configuration.get(info - .getPersistenceUnitName()); + final String unitName = info.getPersistenceUnitName(); + + final PersistenceUnitConfigurer configurer = configuration.get(unitName); if (configurer != null) { configurer.configure(info); + + remainingConfigurations.remove(unitName) ; } } + + return remainingConfigurations; + } + + + private void configureRemaining(List<TapestryPersistenceUnitInfo> persistenceUnitInfos, Map<String, PersistenceUnitConfigurer> remainingConfigurations) + { + for(Entry<String, PersistenceUnitConfigurer> entry: remainingConfigurations.entrySet()) + { + final PersistenceUnitInfoImpl info = new PersistenceUnitInfoImpl(); + info.setPersistenceUnitName(entry.getKey()); + + final PersistenceUnitConfigurer configurer = entry.getValue(); + configurer.configure(info); + + persistenceUnitInfos.add(info); + } } /** Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceContentHandler.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceContentHandler.java?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceContentHandler.java (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceContentHandler.java Wed May 25 18:52:38 2011 @@ -51,7 +51,7 @@ public class PersistenceContentHandler i private static final String ATTRIBUTE_TRANSACTION_TYPE = "transaction-type"; private final List<TapestryPersistenceUnitInfo> persistenceUnits = CollectionFactory.newList(); - private TapestryPersistenceUnitInfo persistenceUnitInfo; + private PersistenceUnitInfoImpl persistenceUnitInfo; private StringBuilder characters; public List<TapestryPersistenceUnitInfo> getPersistenceUnits() Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java Wed May 25 18:52:38 2011 @@ -37,7 +37,7 @@ public class PersistenceUnitInfoImpl imp private String persistenceXMLSchemaVersion; - private PersistenceUnitTransactionType transactionType; + private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL; private DataSource nonJtaDataSource; @@ -47,6 +47,8 @@ public class PersistenceUnitInfoImpl imp private SharedCacheMode sharedCacheMode; + private boolean excludeUnlistedClasses = true; + private final List<String> managedClassNames = new ArrayList<String>(); private final List<String> mappingFilesNames = new ArrayList<String>(); @@ -195,7 +197,7 @@ public class PersistenceUnitInfoImpl imp */ public boolean excludeUnlistedClasses() { - return false; + return excludeUnlistedClasses; } /** @@ -246,9 +248,6 @@ public class PersistenceUnitInfoImpl imp return persistenceXMLSchemaVersion; } - /** - * {@inheritDoc} - */ public void setPersistenceXMLSchemaVersion(final String version) { persistenceXMLSchemaVersion = version; Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java Wed May 25 18:52:38 2011 @@ -40,7 +40,5 @@ public interface TapestryPersistenceUnit void setValidationMode(ValidationMode validationMode); - void setPersistenceXMLSchemaVersion(String version); - void addMappingFileName(String string); } Added: tapestry/tapestry5/trunk/tapestry-jpa/src/test/app1/PersistThang.tml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/app1/PersistThang.tml?rev=1127628&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/test/app1/PersistThang.tml (added) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/app1/PersistThang.tml Wed May 25 18:52:38 2011 @@ -0,0 +1,25 @@ +<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> + <body> + <p> + entity name: + <span id="name"> + <t:if test="thang">${thang.name}</t:if> + </span> + </p> + <p> + <t:eventlink event="createEntity">create entity</t:eventlink> + </p> + <p> + <t:eventlink event="changeName">change the name</t:eventlink> + </p> + <p> + <t:eventlink event="setToNull">set to null</t:eventlink> + </p> + <p> + <t:eventlink event="delete">delete</t:eventlink> + </p> + <p> + <t:eventlink event="setToTransient">set to transient</t:eventlink> + </p> + </body> +</html> \ No newline at end of file Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java Wed May 25 18:52:38 2011 @@ -66,6 +66,33 @@ public class JpaIntegrationTest extends } @Test + public void persist_thangs() + { + open("/persistthang"); + assertEquals(getText("//span[@id='name']").length(), 0); + + clickAndWait("link=create entity"); + assertText("//span[@id='name']", "name"); + + // shouldn't save the change to the name because it's reloaded every time + clickAndWait("link=change the name"); + assertText("//span[@id='name']", "name"); + + // can set back to null + clickAndWait("link=set to null"); + assertEquals(getText("//span[@id='name']").length(), 0); + + // deleting an entity that is still persisted. just remove the entity from the session if + // it's not found. + clickAndWait("link=delete"); + assertEquals(getText("//span[@id='name']").length(), 0); + + // transient objects cannot be persisted + clickAndWait("link=set to transient"); + assertTextPresent("Failed persisting the entity."); + } + + @Test public void sso_entities() { open("/ssoentity"); Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/AppConstants.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/AppConstants.java?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/AppConstants.java (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/AppConstants.java Wed May 25 18:52:38 2011 @@ -17,4 +17,6 @@ package org.example.app1; public class AppConstants { public static final String TEST_PERSISTENCE_UNIT = "TestUnit"; + + public static final String TEST_PERSISTENCE_UNIT_2 = "TestUnit2"; } Added: tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/entities/Thang.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/entities/Thang.java?rev=1127628&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/entities/Thang.java (added) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/entities/Thang.java Wed May 25 18:52:38 2011 @@ -0,0 +1,50 @@ +// Copyright 2011 The Apache Software Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.example.app1.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Thang +{ + @Id + @GeneratedValue + private Long id; + + private String name; + + public Long getId() + { + return id; + } + + public void setId(final Long id) + { + this.id = id; + } + + public String getName() + { + return name; + } + + public void setName(final String name) + { + this.name = name; + } + +} Propchange: tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/entities/Thang.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/pages/PersistThang.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/pages/PersistThang.java?rev=1127628&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/pages/PersistThang.java (added) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/pages/PersistThang.java Wed May 25 18:52:38 2011 @@ -0,0 +1,77 @@ +// Copyright 2011 The Apache Software Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.example.app1.pages; + +import org.apache.tapestry5.annotations.Persist; +import org.apache.tapestry5.annotations.Property; +import org.apache.tapestry5.jpa.JpaPersistenceConstants; +import org.apache.tapestry5.jpa.annotations.CommitAfter; +import org.example.app1.AppConstants; +import org.example.app1.entities.Thang; +import org.example.app1.entities.User; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; + +import static org.example.app1.AppConstants.TEST_PERSISTENCE_UNIT_2; + +public class PersistThang +{ + @PersistenceContext(unitName = TEST_PERSISTENCE_UNIT_2) + private EntityManager entityManager; + + @Persist(JpaPersistenceConstants.ENTITY) + @Property + private Thang thang; + + @CommitAfter + @PersistenceContext(unitName = TEST_PERSISTENCE_UNIT_2) + void onCreateEntity() + { + final Thang thang = new Thang(); + thang.setName("name"); + + entityManager.persist(thang); + + this.thang = thang; + } + + void onChangeName() + { + thang.setName("name2"); + + // No commit, so no real change. + } + + void onSetToTransient() + { + thang = new Thang(); + } + + void onSetToNull() + { + thang = null; + } + + @CommitAfter + @PersistenceContext(unitName = TEST_PERSISTENCE_UNIT_2) + void onDelete() + { + final List<Thang> thangs = entityManager.createQuery("select t from Thang t").getResultList(); + + entityManager.remove(thangs.get(0)); + } +} Propchange: tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/pages/PersistThang.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/services/AppModule.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/services/AppModule.java?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/services/AppModule.java (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app1/services/AppModule.java Wed May 25 18:52:38 2011 @@ -23,15 +23,14 @@ import org.apache.tapestry5.ioc.annotati import org.apache.tapestry5.ioc.annotations.SubModule; import org.apache.tapestry5.ioc.services.ApplicationDefaults; import org.apache.tapestry5.ioc.services.SymbolProvider; -import org.apache.tapestry5.jpa.EntityManagerSource; -import org.apache.tapestry5.jpa.JpaModule; -import org.apache.tapestry5.jpa.JpaTransactionAdvisor; -import org.apache.tapestry5.jpa.PersistenceUnitConfigurer; -import org.apache.tapestry5.jpa.TapestryPersistenceUnitInfo; +import org.apache.tapestry5.jpa.*; import org.example.app1.AppConstants; +import org.example.app1.entities.Thang; import org.example.app1.entities.User; import org.example.app1.services.impl.UserDAOImpl; +import java.util.Properties; + @SubModule(JpaModule.class) public class AppModule { @@ -62,6 +61,22 @@ public class AppModule }; configuration.add(AppConstants.TEST_PERSISTENCE_UNIT, configurer); + final PersistenceUnitConfigurer configurer2 = new PersistenceUnitConfigurer() + { + public void configure(final TapestryPersistenceUnitInfo unitInfo) + { + final Properties properties = unitInfo.getProperties(); + properties.put("javax.persistence.jdbc.driver", "org.h2.Driver"); + properties.put("javax.persistence.jdbc.url", "jdbc:h2:mem:test"); + properties.put("eclipselink.ddl-generation", "create-tables"); + properties.put("eclipselink.logging.level", "fine"); + + unitInfo.addManagedClass(Thang.class); + } + }; + + configuration.add(AppConstants.TEST_PERSISTENCE_UNIT_2, configurer2); + } @Match("*DAO") Modified: tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/META-INF/persistence.xml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/META-INF/persistence.xml?rev=1127628&r1=1127627&r2=1127628&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/META-INF/persistence.xml (original) +++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/META-INF/persistence.xml Wed May 25 18:52:38 2011 @@ -6,19 +6,9 @@ <properties> <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" /> - <property name="javax.persistence.jdbc.username" value="sa" /> <property name="eclipselink.ddl-generation" value="create-tables"/> <property name="eclipselink.logging.level" value="fine"/> </properties> </persistence-unit> - - - <persistence-unit name="TestUnit2" transaction-type="RESOURCE_LOCAL"> - <properties> - <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> - <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" /> - <property name="javax.persistence.jdbc.username" value="sa" /> - </properties> - </persistence-unit> </persistence> \ No newline at end of file