use of org.hibernate.PropertyValueException in project hibernate-orm by hibernate.
the class Nullability method checkNullability.
/**
* Check nullability of the class persister properties
*
* @param values entity properties
* @param persister class persister
* @param isUpdate whether it is intended to be updated or saved
*
* @throws 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 HibernateException {
/*
* 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 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
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));
}
}
}
}
}
}
use of org.hibernate.PropertyValueException in project hibernate-orm by hibernate.
the class ComponentNotNullTest method testCompositeElement.
@Test
public void testCompositeElement() throws Exception {
//composite-element nullable
Session s = openSession();
Transaction t = s.beginTransaction();
ComponentNotNullMaster master = new ComponentNotNullMaster();
ComponentNotNull nullable = new ComponentNotNull();
ComponentNotNull supercomp = new ComponentNotNull();
ComponentNotNull subcomp = new ComponentNotNull();
master.setNullable(nullable);
subcomp.setProp1Subcomp("test");
supercomp.setSubcomp(subcomp);
master.setSupercomp(supercomp);
master.setComponents(new ArrayList());
ComponentNotNullMaster.ContainerInnerClass cc = new ComponentNotNullMaster.ContainerInnerClass();
master.getComponents().add(cc);
try {
s.save(master);
t.commit();
fail("Inserting not-null many-to-one should fail");
} catch (PropertyValueException e) {
//success
}
t.rollback();
s.close();
//null nested component having not-null column
//
s = openSession();
t = s.beginTransaction();
master = new ComponentNotNullMaster();
nullable = new ComponentNotNull();
supercomp = new ComponentNotNull();
subcomp = new ComponentNotNull();
master.setNullable(nullable);
subcomp.setProp1Subcomp("test");
supercomp.setSubcomp(subcomp);
master.setSupercomp(supercomp);
master.setComponentsImplicit(new ArrayList());
ComponentNotNullMaster.ContainerInnerClass nestedCc = new ComponentNotNullMaster.ContainerInnerClass();
cc = new ComponentNotNullMaster.ContainerInnerClass();
cc.setNested(nestedCc);
master.getComponentsImplicit().add(cc);
try {
s.save(master);
t.commit();
fail("Inserting not-null null property should fail");
} catch (PropertyValueException e) {
//succeed
}
t.rollback();
s.close();
//nested component having not-null column
//
s = openSession();
t = s.beginTransaction();
master = new ComponentNotNullMaster();
nullable = new ComponentNotNull();
supercomp = new ComponentNotNull();
subcomp = new ComponentNotNull();
master.setNullable(nullable);
subcomp.setProp1Subcomp("test");
supercomp.setSubcomp(subcomp);
master.setSupercomp(supercomp);
master.setComponentsImplicit(new ArrayList());
nestedCc = new ComponentNotNullMaster.ContainerInnerClass();
cc = new ComponentNotNullMaster.ContainerInnerClass();
cc.setNested(nestedCc);
nestedCc.setNestedproperty("test");
master.getComponentsImplicit().add(cc);
s.save(master);
t.commit();
s.close();
}
use of org.hibernate.PropertyValueException in project hibernate-orm by hibernate.
the class HHH4851Test method testHHH4851.
@Test
public void testHHH4851() throws Exception {
Session session = openSession();
Transaction trx = session.beginTransaction();
Owner org = new Owner();
org.setName("root");
session.saveOrUpdate(org);
ManagedDevice lTerminal = new ManagedDevice();
lTerminal.setName("test");
lTerminal.setOwner(org);
session.saveOrUpdate(lTerminal);
Device terminal = new Device();
terminal.setTag("test");
terminal.setOwner(org);
try {
session.saveOrUpdate(terminal);
} catch (PropertyValueException e) {
fail("not-null checking should not be raised: " + e.getMessage());
}
trx.commit();
session.close();
}
use of org.hibernate.PropertyValueException in project hibernate-orm by hibernate.
the class ComponentNotNullTest method testComponentNotNull.
@Test
public void testComponentNotNull() throws Exception {
//everything not null
//
Session s = openSession();
Transaction t = s.beginTransaction();
ComponentNotNullMaster master = new ComponentNotNullMaster();
ComponentNotNull nullable = new ComponentNotNull();
ComponentNotNull supercomp = new ComponentNotNull();
ComponentNotNull subcomp = new ComponentNotNull();
master.setNullable(nullable);
subcomp.setProp1Subcomp("test");
supercomp.setSubcomp(subcomp);
master.setSupercomp(supercomp);
s.save(master);
t.commit();
s.close();
//null prop of a subcomp
//
s = openSession();
t = s.beginTransaction();
master = new ComponentNotNullMaster();
nullable = new ComponentNotNull();
supercomp = new ComponentNotNull();
subcomp = new ComponentNotNull();
master.setNullable(nullable);
// do not set property
//subcomp.setProp1Subcomp("test");
supercomp.setSubcomp(subcomp);
master.setSupercomp(supercomp);
try {
s.save(master);
t.commit();
fail("Inserting not-null null property should fail");
} catch (PropertyValueException e) {
//succeed
}
t.rollback();
s.close();
//null component having not-null column
//
s = openSession();
t = s.beginTransaction();
master = new ComponentNotNullMaster();
nullable = new ComponentNotNull();
supercomp = new ComponentNotNull();
subcomp = new ComponentNotNull();
master.setNullable(nullable);
try {
s.save(master);
t.commit();
fail("Inserting not-null null property should fail");
} catch (PropertyValueException e) {
//succeed
}
t.rollback();
s.close();
}
Aggregations