use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testCollectionAssocNotValidated.
@Test
public void testCollectionAssocNotValidated() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Screen screen = new Screen();
screen.setStopButton(new Button());
screen.getStopButton().setName("STOOOOOP");
PowerSupply ps = new PowerSupply();
screen.setPowerSupply(ps);
Color c = new Color();
c.setName("Blue");
s.persist(c);
c.setName(null);
screen.getDisplayColors().add(c);
try {
s.persist(screen);
s.flush();
fail("Associated objects should not be validated");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals(Color.class, constraintViolation.getRootBeanClass());
}
tx.rollback();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testEmbeddedCollection.
@Test
public void testEmbeddedCollection() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Screen screen = new Screen();
PowerSupply ps = new PowerSupply();
screen.setPowerSupply(ps);
DisplayConnector conn = new DisplayConnector();
conn.setNumber(0);
screen.getConnectors().add(conn);
try {
s.persist(screen);
s.flush();
fail("Collection of embedded objects should be validated");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals(Screen.class, constraintViolation.getRootBeanClass());
// toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
assertEquals("connectors[].number", constraintViolation.getPropertyPath().toString());
}
tx.rollback();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class ArrayTest method testOneToMany.
@Test
public void testOneToMany() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Competitor c1 = new Competitor();
c1.setName("Renault");
Competitor c2 = new Competitor();
c2.setName("Ferrari");
Contest contest = new Contest();
contest.setResults(new Competitor[] { c1, c2 });
contest.setHeldIn(new Month[] { Month.January, Month.December });
s.persist(contest);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
contest = (Contest) s.get(Contest.class, contest.getId());
assertNotNull(contest);
assertNotNull(contest.getResults());
assertEquals(2, contest.getResults().length);
assertEquals(c2.getName(), contest.getResults()[1].getName());
assertEquals(2, contest.getHeldIn().length);
assertEquals(Month.January, contest.getHeldIn()[0]);
tx.commit();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BeanValidationAutoTest method testListeners.
@Test
public void testListeners() {
CupHolder ch = new CupHolder();
ch.setRadius(new BigDecimal("12"));
Session s = openSession();
Transaction tx = s.beginTransaction();
try {
s.persist(ch);
s.flush();
fail("invalid object should not be persisted");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
}
tx.rollback();
s.close();
}
use of org.hibernate.Transaction in project hibernate-orm by hibernate.
the class BeanValidationGroupsTest method testListeners.
@Test
public void testListeners() {
CupHolder ch = new CupHolder();
ch.setRadius(new BigDecimal("12"));
Session s = openSession();
Transaction tx = s.beginTransaction();
try {
s.persist(ch);
s.flush();
} catch (ConstraintViolationException e) {
fail("invalid object should not be validated");
}
try {
ch.setRadius(null);
s.flush();
} catch (ConstraintViolationException e) {
fail("invalid object should not be validated");
}
try {
s.delete(ch);
s.flush();
fail("invalid object should not be persisted");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
// TODO - seems this explicit case is necessary with JDK 5 (at least on Mac). With Java 6 there is no problem
Annotation annotation = e.getConstraintViolations().iterator().next().getConstraintDescriptor().getAnnotation();
assertEquals(NotNull.class, annotation.annotationType());
}
tx.rollback();
s.close();
}
Aggregations