org.hibernate.PropertyValueException Java Examples

The following examples show how to use org.hibernate.PropertyValueException. 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: UnresolvedEntityInsertActions.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Throws {@link org.hibernate.PropertyValueException} if there are any unresolved
 * entity insert actions that depend on non-nullable associations with
 * a transient entity. This method should be called on completion of
 * an operation (after all cascades are completed) that saves an entity.
 *
 * @throws org.hibernate.PropertyValueException if there are any unresolved entity
 * insert actions; {@link org.hibernate.PropertyValueException#getEntityName()}
 * and {@link org.hibernate.PropertyValueException#getPropertyName()} will
 * return the entity name and property value for the first unresolved
 * entity insert action.
 */
public void checkNoUnresolvedActionsAfterOperation() throws PropertyValueException {
	if ( isEmpty() ) {
		LOG.trace( "No entity insert actions have non-nullable, transient entity dependencies." );
	}
	else {
		final AbstractEntityInsertAction firstDependentAction =
				dependenciesByAction.keySet().iterator().next();

		logCannotResolveNonNullableTransientDependencies( firstDependentAction.getSession() );

		final NonNullableTransientDependencies nonNullableTransientDependencies =
				dependenciesByAction.get( firstDependentAction );
		final Object firstTransientDependency =
				nonNullableTransientDependencies.getNonNullableTransientEntities().iterator().next();
		final String firstPropertyPath =
				nonNullableTransientDependencies.getNonNullableTransientPropertyPaths( firstTransientDependency ).iterator().next();

		throw new TransientPropertyValueException(
				"Not-null property references a transient value - transient instance must be saved before current operation",
				firstDependentAction.getSession().guessEntityName( firstTransientDependency ),
				firstDependentAction.getEntityName(),
				firstPropertyPath
		);
	}
}
 
Example #2
Source File: HibernateExceptionUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenNotNullPropertyNotSet_whenEntityIdSaved_thenPropertyValueException() {
    thrown.expect(isA(PropertyValueException.class));
    thrown.expectMessage(
        "not-null property references a null or transient value");

    Session session = null;
    Transaction transaction = null;

    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();

        Product product = new Product();
        product.setId(1);
        session.save(product);
        transaction.commit();
    } catch (Exception e) {
        rollbackTransactionQuietly(transaction);
        throw (e);
    } finally {
        closeSessionQuietly(session);
    }

}
 
Example #3
Source File: Nullability.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void checkNullability(
		final Object[] values,
		final EntityPersister persister,
		final NullabilityCheckType checkType) {

	/*
	 * Typically when Bean Validation is on, we don't want to validate null values
	 * at the Hibernate Core level. Hence the checkNullability setting.
	 */
	if ( checkNullability ) {
		/*
		  * Algorithm
		  * Check for any level one nullability breaks
		  * Look at non null components to
		  *   recursively check next level of nullability breaks
		  * Look at Collections containing components to
		  *   recursively check next level of nullability breaks
		  *
		  *
		  * In the previous implementation, not-null stuffs where checked
		  * filtering by level one only updateable
		  * or insertable columns. So setting a sub component as update="false"
		  * has no effect on not-null check if the main component had good checkeability
		  * In this implementation, we keep this feature.
		  * However, I never see any documentation mentioning that, but it's for
		  * sure a limitation.
		  */

		final boolean[] nullability = persister.getPropertyNullability();
		final boolean[] checkability = checkType == NullabilityCheckType.CREATE
				? persister.getPropertyInsertability()
				: persister.getPropertyUpdateability();
		final Type[] propertyTypes = persister.getPropertyTypes();

		for ( int i = 0; i < values.length; i++ ) {

			if ( checkability[i] && values[i]!= LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
				final Object value = values[i];
				if ( !nullability[i] && value == null ) {
					//check basic level one nullablilty
					throw new PropertyValueException(
							"not-null property references a null or transient value",
							persister.getEntityName(),
							persister.getPropertyNames()[i]
						);

				}
				else if ( value != null ) {
					//values is not null and is checkable, we'll look deeper
					final String breakProperties = checkSubElementsNullability( propertyTypes[i], value );
					if ( breakProperties != null ) {
						throw new PropertyValueException(
							"not-null property references a null or transient value",
							persister.getEntityName(),
							buildPropertyPath( persister.getPropertyNames()[i], breakProperties )
						);
					}

				}
			}

		}
	}
}
 
Example #4
Source File: Nullability.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Check nullability of the class persister properties
 *
 * @param values entity properties
 * @param persister class persister
 * @param isUpdate wether it is intended to be updated or saved
 * @throws org.hibernate.PropertyValueException Break the nullability of one property
 * @throws HibernateException error while getting Component values
 */
public void checkNullability(
		final Object[] values,
		final EntityPersister persister,
		final boolean isUpdate) 
throws PropertyValueException, HibernateException {

	/*
	  * Algorithm
	  * Check for any level one nullability breaks
	  * Look at non null components to
	  *   recursively check next level of nullability breaks
	  * Look at Collections contraining component to
	  *   recursively check next level of nullability breaks
	  *
	  *
	  * In the previous implementation, not-null stuffs where checked
	  * filtering by level one only updateable
	  * or insertable columns. So setting a sub component as update="false"
	  * has no effect on not-null check if the main component had good checkeability
	  * In this implementation, we keep this feature.
	  * However, I never see any documentation mentioning that, but it's for
	  * sure a limitation.
	  */

	final boolean[] nullability = persister.getPropertyNullability();
	final boolean[] checkability = isUpdate ?
		persister.getPropertyUpdateability() :
		persister.getPropertyInsertability();
	final Type[] propertyTypes = persister.getPropertyTypes();

	for ( int i = 0; i < values.length; i++ ) {
		
		if ( checkability[i] && values[i]!=LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
			final Object value = values[i];
			if ( !nullability[i] && value == null ) {
				
				//check basic level one nullablilty
				throw new PropertyValueException(
						"not-null property references a null or transient value",
						persister.getEntityName(),
						persister.getPropertyNames()[i]
					);
				
			}
			else if ( value != null ) {
				
				//values is not null and is checkable, we'll look deeper
				String breakProperties = checkSubElementsNullability( propertyTypes[i], value );
				if ( breakProperties != null ) {
					throw new PropertyValueException(
						"not-null property references a null or transient value",
						persister.getEntityName(),
						buildPropertyPath( persister.getPropertyNames()[i], breakProperties )
					);
				}
				
			}
		}
		
	}
}
 
Example #5
Source File: BaseSecurityITCase.java    From olat with Apache License 2.0 4 votes vote down vote up
@Test
public void testPolicy() {
    final Identity ident = getOrCreateIdentity("anIdentity");
    final Identity ident2 = baseSecurityManager.createAndPersistIdentity("gugus2", null, AUTHENTICATION_PROVIDER_OLAT, "uuu2", Encoder.encrypt("ppp"));

    final SecurityGroup secg = baseSecurityManager.createAndPersistSecurityGroup();
    final SecurityGroup secg2 = baseSecurityManager.createAndPersistSecurityGroup();

    baseSecurityManager.addIdentityToSecurityGroup(ident, secg);
    // resourceable type
    final OLATResourceable or1 = OresHelper.createOLATResourceableInstance("Forum", new Long("111"));
    // simulate some user gui clicks
    DBFactory.getInstance().closeSession();

    // resourceable (type and key)
    final OLATResourceable or2 = OresHelper.createOLATResourceableInstance("Forum", new Long("123"));

    baseSecurityManager.createAndPersistPolicy(secg2, Constants.PERMISSION_ACCESS, or1);
    Policy policy = baseSecurityManager.createAndPersistPolicy(secg, "read", or2); // instance
                                                                                   // resource
    assertNotNull(policy);
    assertNotNull(policy.getSecurityGroup());
    assertNotNull(policy.getSecurityGroup().getKey());
    assertTrue(policy.getSecurityGroup().getKey().equals(secg.getKey()));

    policy = baseSecurityManager.createAndPersistPolicy(secg, "read", or1); // type resource
    assertNotNull(policy);

    // assert we have instance access if we own the type policy
    assertTrue(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "read", or2));

    assertTrue(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "read", or1));
    assertFalse(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "write", or1));
    assertFalse(baseSecurityManager.isIdentityPermittedOnResourceable(ident2, "read", or1));
    assertTrue(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "read", or2));
    assertFalse(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "blub", or2));

    DBFactory.getInstance().closeSession();

    // test on deleting a securitygroup that is still referenced by a policy
    // (referential integrity)
    boolean r = true;
    try {
        baseSecurityManager.deleteSecurityGroup(secg);
        DBFactory.getInstance().closeSession();
    } catch (final Exception e) {
        if (((DBRuntimeException) e).getCause() instanceof PropertyValueException) {
            r = true;
        }
    }
    DBFactory.getInstance().closeSession();
    assertTrue(r);
}
 
Example #6
Source File: BaseSecurityITCase.java    From olat with Apache License 2.0 4 votes vote down vote up
@Test
public void testPolicy() {
    final Identity ident = getOrCreateIdentity("anIdentity");
    final Identity ident2 = baseSecurityManager.createAndPersistIdentity("gugus2", null, AUTHENTICATION_PROVIDER_OLAT, "uuu2", Encoder.bCryptEncode("ppp"));

    final SecurityGroup secg = baseSecurityManager.createAndPersistSecurityGroup();
    final SecurityGroup secg2 = baseSecurityManager.createAndPersistSecurityGroup();

    baseSecurityManager.addIdentityToSecurityGroup(ident, secg);
    // resourceable type
    final OLATResourceable or1 = OresHelper.createOLATResourceableInstance("Forum", new Long("111"));
    // simulate some user gui clicks
    DBFactory.getInstance().closeSession();

    // resourceable (type and key)
    final OLATResourceable or2 = OresHelper.createOLATResourceableInstance("Forum", new Long("123"));

    baseSecurityManager.createAndPersistPolicy(secg2, Constants.PERMISSION_ACCESS, or1);
    Policy policy = baseSecurityManager.createAndPersistPolicy(secg, "read", or2); // instance
                                                                                   // resource
    assertNotNull(policy);
    assertNotNull(policy.getSecurityGroup());
    assertNotNull(policy.getSecurityGroup().getKey());
    assertTrue(policy.getSecurityGroup().getKey().equals(secg.getKey()));

    policy = baseSecurityManager.createAndPersistPolicy(secg, "read", or1); // type resource
    assertNotNull(policy);

    // assert we have instance access if we own the type policy
    assertTrue(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "read", or2));

    assertTrue(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "read", or1));
    assertFalse(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "write", or1));
    assertFalse(baseSecurityManager.isIdentityPermittedOnResourceable(ident2, "read", or1));
    assertTrue(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "read", or2));
    assertFalse(baseSecurityManager.isIdentityPermittedOnResourceable(ident, "blub", or2));

    DBFactory.getInstance().closeSession();

    // test on deleting a securitygroup that is still referenced by a policy
    // (referential integrity)
    boolean r = true;
    try {
        baseSecurityManager.deleteSecurityGroup(secg);
        DBFactory.getInstance().closeSession();
    } catch (final Exception e) {
        if (((DBRuntimeException) e).getCause() instanceof PropertyValueException) {
            r = true;
        }
    }
    DBFactory.getInstance().closeSession();
    assertTrue(r);
}
 
Example #7
Source File: ReactiveActionQueue.java    From hibernate-reactive with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Throws {@link org.hibernate.PropertyValueException} if there are any unresolved entity insert actions that depend
 * on non-nullable associations with a transient entity. This method should be called on completion of an operation
 * (after all cascades are completed) that saves an entity.
 *
 * @throws org.hibernate.PropertyValueException if there are any unresolved entity insert actions;
 * {@link org.hibernate.PropertyValueException#getEntityName()} and
 * {@link org.hibernate.PropertyValueException#getPropertyName()} will return the entity name and property value for
 * the first unresolved entity insert action.
 */
public void checkNoUnresolvedActionsAfterOperation() throws PropertyValueException {
	if ( unresolvedInsertions != null ) {
		unresolvedInsertions.checkNoUnresolvedActionsAfterOperation();
	}
}
 
Example #8
Source File: ActionQueue.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Throws {@link org.hibernate.PropertyValueException} if there are any unresolved entity insert actions that depend
 * on non-nullable associations with a transient entity. This method should be called on completion of an operation
 * (after all cascades are completed) that saves an entity.
 *
 * @throws org.hibernate.PropertyValueException if there are any unresolved entity insert actions;
 * {@link org.hibernate.PropertyValueException#getEntityName()} and
 * {@link org.hibernate.PropertyValueException#getPropertyName()} will return the entity name and property value for
 * the first unresolved entity insert action.
 */
public void checkNoUnresolvedActionsAfterOperation() throws PropertyValueException {
	if(unresolvedInsertions != null) {
		unresolvedInsertions.checkNoUnresolvedActionsAfterOperation();
	}
}