private EntityManagerFactory setupH2EMF(String ddlMode) throws IOException { Map<String, Object> configuration = new HashMap<>(); String jdbcUri = template("jdbc:h2:file:{{datapath}}/datastore") .add("datapath", BiliomiContainer.getParameters().getWorkingDir().getCanonicalPath()) .apply(); configuration.put("hibernate.connection.url", jdbcUri); configuration.put("hibernate.hbm2ddl.auto", ddlMode); // Add entity classes configuration.put(AvailableSettings.LOADED_CLASSES, scanForEntityClasses()); // Create Entity manager factory logger.debug("Creating entity manager factory for local H2 database..."); HibernatePersistenceProvider provider = new HibernatePersistenceProvider(); return provider.createEntityManagerFactory("Biliomi-H2-DS", configuration); }
@Test public void testCascadeLockOnManagedEntityWithAssociationsInitializedAndJpa() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Post post = entityManager.createQuery( "select p " + "from Post p " + "join fetch p.details " + "where p.id = :id", Post.class) .setParameter("id", 1L) .getSingleResult(); entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap( AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED )); }); }
@Test public void configureRunsBeforeSessionFactoryCreation(){ final String expectedFactoryName = "Dropwizard Hibernate JPA Test Factory"; final EntityManagerFactoryFactory customFactory = new EntityManagerFactoryFactory() { @Override protected void configure(PersistenceUnitConfig configuration) { super.configure(configuration); configuration.setProperty(AvailableSettings.ENTITY_MANAGER_FACTORY_NAME, expectedFactoryName); } }; entityManagerFactory = customFactory.build(bundle, environment, config, ImmutableList.<Class<?>>of(Person.class)); assertThat(entityManagerFactory.getProperties().get(AvailableSettings.ENTITY_MANAGER_FACTORY_NAME)).isSameAs(expectedFactoryName); }
@Contribute(EntityManagerSource.class) public static void configurePersistenceUnit( MappedConfiguration<String, PersistenceUnitConfigurer> cfg, final ObjectLocator objectLocator) { PersistenceUnitConfigurer configurer = new PersistenceUnitConfigurer() { @Override public void configure(TapestryPersistenceUnitInfo unitInfo) { unitInfo.transactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL) .persistenceProviderClassName("org.hibernate.jpa.HibernatePersistenceProvider") .excludeUnlistedClasses(false) .addProperty("javax.persistence.jdbc.user", "sa") .addProperty("javax.persistence.jdbc.password", "sa") .addProperty("javax.persistence.jdbc.driver", "org.h2.Driver") .addProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:jpatest_hibernate") .addProperty("hibernate.hbm2ddl.auto", "update") .addProperty("hibernate.show_sql", "true"); unitInfo.getProperties().put(AvailableSettings.CDI_BEAN_MANAGER, objectLocator.autobuild(TapestryCDIBeanManagerForJPAEntityListeners.class)); } }; cfg.add("jpatest", configurer); }
private EntityManagerFactory setupMySQLEMF(String ddlMode) { Map<String, Object> configuration = new HashMap<>(); USMySQL mySQL = yamlCoreSettings.getBiliomi().getDatabase().getMySQL(); boolean useSSL = mySQL.isUsessl(); // Biliomi doesn't need the MySQL server to be in the correct timezone since all dates // are persisted as ISO8601, but a server might stall connection if this isn't set. TimeZone timeZone = Calendar.getInstance().getTimeZone(); configuration.put("hibernate.connection.serverTimezone", timeZone.getID()); String jdbcUri = template("jdbc:mysql://{{host}}:{{port}}/{{database}}") .add("host", mySQL::getHost) .add("port", mySQL::getPort) .add("database", mySQL::getDatabase) .apply(); configuration.put("hibernate.connection.url", jdbcUri); configuration.put("hibernate.connection.username", mySQL.getUsername()); configuration.put("hibernate.connection.password", mySQL.getPassword()); configuration.put("hibernate.connection.useSSL", String.valueOf(useSSL)); configuration.put("hibernate.hbm2ddl.auto", ddlMode); // Add entity classes configuration.put(AvailableSettings.LOADED_CLASSES, scanForEntityClasses()); // Create Entity manager factory logger.debug("Creating entity manager factory for MySQL database..."); HibernatePersistenceProvider provider = new HibernatePersistenceProvider(); return provider.createEntityManagerFactory("Biliomi-MySQL-DS", configuration); }
@Test public void testCascadeLockOnManagedEntityWithJPA() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Post post = entityManager.find(Post.class, 1L); entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap( AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED )); }); }
@Test public void testCascadeLockOnManagedEntityWithAssociationsUninitializedAndJpa() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Post post = entityManager.find(Post.class, 1L); entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap( AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED )); }); }
@Test public void testSetEnhancerPropertyGeneratesWarning() { persistenceUnitInfo.setProperty(AvailableSettings.ENHANCER_ENABLE_ASSOCIATION_MANAGEMENT, "true"); verify(logger).warn(contains(AvailableSettings.ENHANCER_ENABLE_ASSOCIATION_MANAGEMENT)); }