<dependency>
<groupId>toplink.essentials</groupId>
<artifactId>toplink-essentials</artifactId>
- <version>2.0-36</version>
+ <version>2.1-60f</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
@Override
public void customize(PersistenceUnitDescription aPersistenceUnit,
Map<String, String> aJpaProperties) {
- // Hack to make JNDI lookup of the datasource work with toplink
- aJpaProperties.put("eclipselink.session.customizer",
- JndiSessionCustomizer.class.getName());
-
+
// DDL generation
aJpaProperties.put("eclipselink.ddl-generation", "create-tables");
- // Use JTA transaction type
- aJpaProperties.put("javax.persistence.transactionType", "JTA");
-
// DDL generation
FileSystemUtils.createDir(new File("target/sql"));
aJpaProperties.put("eclipselink.application-location", "target/sql");
import java.util.logging.Level;
import java.util.logging.Logger;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
-import javax.sql.DataSource;
import org.wamblee.support.jndi.StubInitialContextFactory;
}
private PersistenceUnitDescription persistenceUnit;
- private DataSource dataSource;
+ private String url;
+ private String user;
+ private String password;
private EntityManagerFactory factory;
/**
* Constructs the builder.
*
- * @param aDataSource
- * Datasource of database.
+ * @param aUrl JDBC URL
+ * @param aUser User name
+ * @param aPassword Password.
* @param aPersistenceUnit
* Persistence unit.
*/
- public JpaBuilder(DataSource aDataSource,
+ public JpaBuilder(String aUrl, String aUser, String aPassword,
PersistenceUnitDescription aPersistenceUnit) {
persistenceUnit = aPersistenceUnit;
- dataSource = aDataSource;
- StubInitialContextFactory.register();
+ url = aUrl;
+ user = aUser;
+ password = aPassword;
}
/**
* manager factory, and forces creation of the database schema.
*/
public void start() throws Exception {
- try {
- InitialContext ctx = new InitialContext();
- ctx.bind(persistenceUnit.getJndiName(), dataSource);
- } catch (NamingException e) {
- throw new RuntimeException("JNDI problem", e);
- }
factory = createFactory();
try {
execute(new JpaUnitOfWork<Void>() {
*/
public EntityManagerFactory createFactory() {
Map<String, String> jpaProps = new TreeMap<String, String>();
+
+ jpaProps.put("javax.persistence.jtaDataSource", null);
+ jpaProps.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
+ jpaProps.put("javax.persistence.jdbc.url", url);
+ jpaProps.put("javax.persistence.jdbc.user", user);
+ jpaProps.put("javax.persistence.jdbc.password", password);
JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit,
jpaProps);
-
+
// jpaProps.put("javax.persistence.provider",
// HibernatePersistence.class.getName());
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
*/
package org.wamblee.support.persistence;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
import javax.sql.DataSource;
import org.dbunit.IDatabaseTester;
+import org.wamblee.support.jndi.StubInitialContextFactory;
/**
* This class is the entry point for JPA tests. Test code should construct a
public void start() throws Exception {
db = DatabaseBuilder.getDatabase();
dataSource = db.start();
+
+ // NOTE: adding datasource to JNDI is no longer needed for
+ // JPA testing, but is nice to have available for other uses.
+ StubInitialContextFactory.register();
+ try {
+ InitialContext ctx = new InitialContext();
+ ctx.bind(persistenceUnit.getJndiName(), dataSource);
+ } catch (NamingException e) {
+ throw new RuntimeException("JNDI problem", e);
+ }
dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
dbUtils.start();
dbUtils.dropTables();
dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
- jpaBuilder = new JpaBuilder(dataSource, persistenceUnit);
+ jpaBuilder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
jpaBuilder.start();
// db tester should be created after Jpa builder because jpa builder
dbutils.dropTables();
dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
- builder = new JpaBuilder(dataSource, persistenceUnit);
+ builder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
builder.start();
assertEquals(0, db.getActiveConnections());
@Override
public void customize(PersistenceUnitDescription aPersistenceUnit,
Map<String, String> aJpaProperties) {
- // Hibernate: Override transaction type and datasource
- aJpaProperties.put("javax.persistence.transactionType",
- "RESOURCE_LOCAL");
- aJpaProperties.put("javax.persistence.jtaDataSource", null);
- aJpaProperties.put("javax.persistence.nonJtaDataSource",
- aPersistenceUnit.getJndiName());
+
+ System.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "true");
+
+ // Set non-JPA connection properties for older versions of hibernate
+ System.getProperties().remove("hibernate.connection.datasource");
+ if (System.getProperty("hibernate.connection.datasource") != null ) {
+ throw new RuntimeException("ERROR");
+ }
+ System.setProperty("hibernate.connection.url", aJpaProperties.get("javax.persistence.jdbc.url"));
+ System.setProperty("hibernate.connection.username", aJpaProperties.get("javax.persistence.jdbc.user"));
+ System.setProperty("hibernate.connection.password", aJpaProperties.get("javax.persistence.jdbc.password"));
// Hibernate schema generation
aJpaProperties.put("hibernate.hbm2ddl.auto", "create");
@Override
public void customize(PersistenceUnitDescription aPersistenceUnit,
Map<String, String> aJpaProperties) {
- // Hack to make JNDI lookup of the datasource work with toplink
- aJpaProperties.put("toplink.session.customizer",
- JndiSessionCustomizer.class.getName());
+
+ // Custom datasource properties.
+ aJpaProperties.put("toplink.jdbc.url", aJpaProperties.get("javax.persistence.jdbc.url"));
+ aJpaProperties.put("toplink.jdbc.user", aJpaProperties.get("javax.persistence.jdbc.user"));
+ aJpaProperties.put("toplink.jdbc.password", aJpaProperties.get("javax.persistence.jdbc.password"));
// DDL generation for toplink
aJpaProperties.put("toplink.ddl-generation", "create-tables");
- // Use JTA transaction type
- aJpaProperties.put("javax.persistence.transactionType", "JTA");
-
// DDL generation
FileSystemUtils.createDir(new File("target/sql"));
aJpaProperties.put("toplink.create-ddl-jdbc-file-name", "target/sql/create-schema.sql");