javax.persistence.ValidationMode Java Examples

The following examples show how to use javax.persistence.ValidationMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RdbmsPersistenceConfig.java    From devicehive-java-server with Apache License 2.0 7 votes vote down vote up
@Bean
@DependsOn(value = {"simpleApplicationContextHolder"})
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
    factoryBean.setValidationMode(ValidationMode.CALLBACK);
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    factoryBean.setPackagesToScan("com.devicehive.model");

    final Properties props = new Properties();
    props.putAll(this.properties.getHibernateProperties(this.dataSource));
    if (useNativeClient) {
        props.put("hibernate.cache.hazelcast.native_client_group", groupName);
        props.put("hibernate.cache.hazelcast.native_client_password", groupPassword);
        if (!CollectionUtils.isEmpty(clusterMembers)) {
            props.put("hibernate.cache.hazelcast.native_client_address", clusterMembers.get(0));
        }
    }

    factoryBean.setJpaProperties(props);
    return factoryBean;
}
 
Example #2
Source File: EntityManagerBean.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
void init(final PersistenceUnitInfo info, final BeanManager bm) {
    final PersistenceProvider provider;
    try {
        provider = PersistenceProvider.class.cast(
                Thread.currentThread().getContextClassLoader().loadClass(info.getPersistenceProviderClassName()).newInstance());
    } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new IllegalArgumentException("Bad provider: " + info.getPersistenceProviderClassName());
    }
    final EntityManagerFactory factory = provider.createContainerEntityManagerFactory(info, new HashMap() {{
        put("javax.persistence.bean.manager", bm);
        if (ValidationMode.NONE != info.getValidationMode()) {
            ofNullable(findValidatorFactory(bm)).ifPresent(factory -> put("javax.persistence.validation.factory", factory));
        }
    }});
    instanceFactory = synchronization == SynchronizationType.SYNCHRONIZED ? factory::createEntityManager : () -> factory.createEntityManager(synchronization);
}
 
Example #3
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
@ManagedOperation
@Description("change the validation mode if possible (value is ok)")
public void setValidationMode(final String value) {
    try {
        final ValidationMode mode = ValidationMode.valueOf(value.trim().toUpperCase());
        reloadableEntityManagerFactory.setValidationMode(mode);
    } catch (final Exception iae) {
        LOGGER.warning("Can't set validation mode " + value, iae);
        reloadableEntityManagerFactory.setProperty(JAVAX_PERSISTENCE_VALIDATION_MODE, value);
    }
}
 
Example #4
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
public synchronized void setValidationMode(final ValidationMode mode) {
    final PersistenceUnitInfoImpl info = entityManagerFactoryCallable.getUnitInfo();
    info.setValidationMode(mode);

    final Properties properties = entityManagerFactoryCallable.getUnitInfo().getProperties();
    if (properties.containsKey(JAVAX_PERSISTENCE_VALIDATION_MODE)) {
        properties.setProperty(JAVAX_PERSISTENCE_VALIDATION_MODE, mode.name());
    }
}
 
Example #5
Source File: PersistenceUnit.java    From tomee with Apache License 2.0 5 votes vote down vote up
public ValidationMode getValidationMode() {
    if (validationMode == null) {
        final String propConfig = getProperty("javax.persistence.validation.mode");
        if (propConfig != null) {
            try {
                validationMode = ValidationMode.valueOf(propConfig.toUpperCase());
            } catch (final IllegalArgumentException iae) { // can happen since some provider allow more than the enum
                // no-op
            }
        }
    }
    return (validationMode == null) ? ValidationMode.AUTO : validationMode;
}
 
Example #6
Source File: PersistenceUnitInfoImpl.java    From hibernate-master-class with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
    return ValidationMode.AUTO;
}
 
Example #7
Source File: PersistenceUnitInfoBuilder.java    From quickperf with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfo build(final DataSource dataSource
                               , final Properties config
                               , final Class<?>... persistentClasses) {
    return new PersistenceUnitInfo(){

        @Override
        public String getPersistenceUnitName() {
            return "my pu";
        }

        @Override
        public String getPersistenceProviderClassName() {
            return "org.hibernate.jpa.HibernatePersistenceProvider";
        }

        @Override
        public PersistenceUnitTransactionType getTransactionType() {
            return PersistenceUnitTransactionType.RESOURCE_LOCAL;
        }

        @Override
        public DataSource getJtaDataSource() {
            return null;
        }

        @Override
        public DataSource getNonJtaDataSource() {
            return dataSource;
        }

        @Override
        public List<String> getMappingFileNames() {
            return emptyList();
        }

        @Override
        public List<URL> getJarFileUrls() {
            return emptyList();
        }

        @Override
        public URL getPersistenceUnitRootUrl() {
            return null;
        }

        @Override
        public List<String> getManagedClassNames() {
            return Arrays.stream(persistentClasses)
                                    .map(c -> c.getName())
                                    .collect(toList());
        }

        @Override
        public boolean excludeUnlistedClasses() {
            return false;
        }

        @Override
        public SharedCacheMode getSharedCacheMode() {
            return SharedCacheMode.NONE;
        }

        @Override
        public ValidationMode getValidationMode() {
            return ValidationMode.NONE;
        }

        @Override
        public Properties getProperties() {
            return config;
        }

        @Override
        public String getPersistenceXMLSchemaVersion() {
            return "2.1";
        }

        @Override
        public ClassLoader getClassLoader() {
            return this.getClass().getClassLoader();
        }

        @Override
        public void addTransformer(ClassTransformer transformer) {

        }

        @Override
        public ClassLoader getNewTempClassLoader() {
            return null;
        }

    };
}
 
Example #8
Source File: PersistenceUnitReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Parse the unit info DOM element.
 */
protected SpringPersistenceUnitInfo parsePersistenceUnitInfo(Element persistenceUnit, String version, URL rootUrl)
		throws IOException {

	SpringPersistenceUnitInfo unitInfo = new SpringPersistenceUnitInfo();

	// set JPA version (1.0 or 2.0)
	unitInfo.setPersistenceXMLSchemaVersion(version);

	// set persistence unit root URL
	unitInfo.setPersistenceUnitRootUrl(rootUrl);

	// set unit name
	unitInfo.setPersistenceUnitName(persistenceUnit.getAttribute(UNIT_NAME).trim());

	// set transaction type
	String txType = persistenceUnit.getAttribute(TRANSACTION_TYPE).trim();
	if (StringUtils.hasText(txType)) {
		unitInfo.setTransactionType(PersistenceUnitTransactionType.valueOf(txType));
	}

	// evaluate data sources
	String jtaDataSource = DomUtils.getChildElementValueByTagName(persistenceUnit, JTA_DATA_SOURCE);
	if (StringUtils.hasText(jtaDataSource)) {
		unitInfo.setJtaDataSource(this.dataSourceLookup.getDataSource(jtaDataSource.trim()));
	}

	String nonJtaDataSource = DomUtils.getChildElementValueByTagName(persistenceUnit, NON_JTA_DATA_SOURCE);
	if (StringUtils.hasText(nonJtaDataSource)) {
		unitInfo.setNonJtaDataSource(this.dataSourceLookup.getDataSource(nonJtaDataSource.trim()));
	}

	// provider
	String provider = DomUtils.getChildElementValueByTagName(persistenceUnit, PROVIDER);
	if (StringUtils.hasText(provider)) {
		unitInfo.setPersistenceProviderClassName(provider.trim());
	}

	// exclude unlisted classes
	Element excludeUnlistedClasses = DomUtils.getChildElementByTagName(persistenceUnit, EXCLUDE_UNLISTED_CLASSES);
	if (excludeUnlistedClasses != null) {
		String excludeText = DomUtils.getTextValue(excludeUnlistedClasses);
		unitInfo.setExcludeUnlistedClasses(!StringUtils.hasText(excludeText) || Boolean.valueOf(excludeText));
	}

	// set JPA 2.0 shared cache mode
	String cacheMode = DomUtils.getChildElementValueByTagName(persistenceUnit, SHARED_CACHE_MODE);
	if (StringUtils.hasText(cacheMode)) {
		unitInfo.setSharedCacheMode(SharedCacheMode.valueOf(cacheMode));
	}

	// set JPA 2.0 validation mode
	String validationMode = DomUtils.getChildElementValueByTagName(persistenceUnit, VALIDATION_MODE);
	if (StringUtils.hasText(validationMode)) {
		unitInfo.setValidationMode(ValidationMode.valueOf(validationMode));
	}

	parseProperties(persistenceUnit, unitInfo);
	parseManagedClasses(persistenceUnit, unitInfo);
	parseMappingFiles(persistenceUnit, unitInfo);
	parseJarFiles(persistenceUnit, unitInfo);

	return unitInfo;
}
 
Example #9
Source File: MutablePersistenceUnitInfo.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void setValidationMode(ValidationMode validationMode) {
	this.validationMode = validationMode;
}
 
Example #10
Source File: MutablePersistenceUnitInfo.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
	return this.validationMode;
}
 
Example #11
Source File: PersistenceUnitInfoBuilder.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public ValidationMode getValidationMode() {
    return validationMode;
}
 
Example #12
Source File: PersistenceUnitInfoBuilder.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfoBuilder setValidationMode(final ValidationMode validationMode) {
    this.validationMode = validationMode;
    return this;
}
 
Example #13
Source File: PersistenceUnitInfoBuilder.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public PersistenceUnitInfo toInfo() {
    if (providerClass == null) {
        providerClass = ServiceLoader.load(PersistenceProvider.class).iterator().next().getClass().getName();
    }
    requireNonNull(dataSource, "datasource not provided");
    return new PersistenceUnitInfo() {
        @Override
        public String getPersistenceUnitName() {
            return unitName;
        }

        @Override
        public String getPersistenceProviderClassName() {
            return providerClass;
        }

        @Override
        public PersistenceUnitTransactionType getTransactionType() {
            return transactionType;
        }

        @Override
        public DataSource getJtaDataSource() {
            return jtaDataSource;
        }

        @Override
        public DataSource getNonJtaDataSource() {
            return dataSource;
        }

        @Override
        public List<String> getMappingFileNames() {
            return mappingFiles;
        }

        @Override
        public List<URL> getJarFileUrls() {
            return jarFiles;
        }

        @Override
        public URL getPersistenceUnitRootUrl() {
            return rootUrl;
        }

        @Override
        public List<String> getManagedClassNames() {
            return managedClasses;
        }

        @Override
        public boolean excludeUnlistedClasses() {
            return excludeUnlistedClasses;
        }

        @Override
        public SharedCacheMode getSharedCacheMode() {
            return sharedCacheMode;
        }

        @Override
        public ValidationMode getValidationMode() {
            return validationMode;
        }

        @Override
        public Properties getProperties() {
            return properties;
        }

        @Override
        public String getPersistenceXMLSchemaVersion() {
            return version;
        }

        @Override
        public ClassLoader getClassLoader() {
            return loader;
        }

        @Override
        public void addTransformer(final ClassTransformer transformer) {
            // no-op: not supported
        }

        @Override
        public ClassLoader getNewTempClassLoader() {
            return new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader());
        }
    };
}
 
Example #14
Source File: PersistenceUnitInfoImpl.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
    return ValidationMode.AUTO;
}
 
Example #15
Source File: CommonEntityManagerFactoryConf.java    From syncope with Apache License 2.0 4 votes vote down vote up
public ValidationMode getValidationMode() {
    return validationMode;
}
 
Example #16
Source File: CommonEntityManagerFactoryConf.java    From syncope with Apache License 2.0 4 votes vote down vote up
public void setValidationMode(final ValidationMode validationMode) {
    this.validationMode = validationMode;
}
 
Example #17
Source File: CustomDescriptor.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
	return descriptor.getValidationMode();
}
 
Example #18
Source File: RapidoidPersistenceUnitInfo.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
	return null;
}
 
Example #19
Source File: PersistenceUnitInfoImpl.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
    return ValidationMode.AUTO;
}
 
Example #20
Source File: HammockPersistenceUnitInfo.java    From hammock with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
   return ValidationMode.NONE;
}
 
Example #21
Source File: MinnalPersistenceUnitInfo.java    From minnal with Apache License 2.0 4 votes vote down vote up
public ValidationMode getValidationMode() {
	return ValidationMode.AUTO;
}
 
Example #22
Source File: PersistenceUnit.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void setValidationMode(final ValidationMode validationMode) {
    this.validationMode = validationMode;
}
 
Example #23
Source File: PersistenceUnitInfoImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
public ValidationMode getValidationMode() {
    return this.validationMode;
}
 
Example #24
Source File: PersistenceUnitInfoImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
/**
 * @param validationMode the validationMode to set
 */
public void setValidationMode(final ValidationMode validationMode) {
    this.validationMode = validationMode;
}
 
Example #25
Source File: EntityManagerFactoryCallable.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public EntityManagerFactory call() throws Exception {
    final ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(appClassLoader);
    try {
        final Class<?> clazz = appClassLoader.loadClass(persistenceProviderClassName);
        final PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance();

        // Create entity manager factories with the validator factory
        final Map<String, Object> properties = new HashMap<>();
        if (!ValidationMode.NONE.equals(unitInfo.getValidationMode())) {
            properties.put("javax.persistence.validation.factory",
                    potentialValidators != null && potentialValidators.size() == 1 ? // optim to avoid lookups
                            ensureSerializable(potentialValidators.values().iterator().next()) :
                            new ValidatorFactoryWrapper(potentialValidators));
        }
        if (cdi && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty("tomee.jpa.cdi", "true"))
                && "true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jpa.cdi", "true"))) {
            properties.put("javax.persistence.bean.manager",
                    Proxy.newProxyInstance(appClassLoader, new Class<?>[]{BeanManager.class}, new BmHandler()));
        }

        customizeProperties(properties);

        // ensure no tx is there cause a managed connection would fail if the provider setAutocCommit(true) and some hib* have this good idea
        final Transaction transaction;
        if (unitInfo.isLazilyInitialized()) {
            transaction = OpenEJB.getTransactionManager().suspend();
        } else {
            transaction = null;
        }
        final EntityManagerFactory emf;
        try {
            emf = persistenceProvider.createContainerEntityManagerFactory(unitInfo, properties);
        } finally {
            if (unitInfo.isLazilyInitialized() && transaction != null) {
                OpenEJB.getTransactionManager().resume(transaction);
            }
        }

        if (unitInfo.getProperties() != null
                && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty(OPENEJB_JPA_INIT_ENTITYMANAGER))
                || SystemInstance.get().getOptions().get(OPENEJB_JPA_INIT_ENTITYMANAGER, false)) {
            emf.createEntityManager().close();
        }

        if (unitInfo.getNonJtaDataSource() != null) {
            final ImportSql importer = new ImportSql(appClassLoader, unitInfo.getPersistenceUnitName(), unitInfo.getNonJtaDataSource());
            if (importer.hasSomethingToImport()) {
                emf.createEntityManager().close(); // to let OpenJPA create the database if configured this way
                importer.doImport();
            }
        }

        return emf;
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
 
Example #26
Source File: PersistenceUnitInfoImpl.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
    return ValidationMode.AUTO;
}
 
Example #27
Source File: HibernatePersistenceUnitInfo.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
    return ValidationMode.AUTO;
}
 
Example #28
Source File: DelegatingPuInfo.java    From eclipselink-maven-plugin with Apache License 2.0 4 votes vote down vote up
public ValidationMode getValidationMode()
{
    return delegate.getValidationMode();
}
 
Example #29
Source File: DatabaseImpl.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
    return ValidationMode.AUTO;
}
 
Example #30
Source File: DynamicEntityUnitInfo.java    From we-cmdb with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationMode getValidationMode() {
    return ValidationMode.AUTO;
}