Search in sources :

Example 96 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 97 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 98 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 99 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(javax.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 100 with Transaction

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(javax.validation.ConstraintViolationException) NotNull(javax.validation.constraints.NotNull) BigDecimal(java.math.BigDecimal) Annotation(java.lang.annotation.Annotation) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Transaction (org.hibernate.Transaction)1272 Session (org.hibernate.Session)1250 Test (org.junit.Test)1124 List (java.util.List)239 ArrayList (java.util.ArrayList)143 Iterator (java.util.Iterator)87 TestForIssue (org.hibernate.testing.TestForIssue)87 Query (org.hibernate.Query)80 BigDecimal (java.math.BigDecimal)73 Date (java.util.Date)52 HashSet (java.util.HashSet)44 Criteria (org.hibernate.Criteria)39 SkipForDialect (org.hibernate.testing.SkipForDialect)36 ScrollableResults (org.hibernate.ScrollableResults)35 Set (java.util.Set)30 PersistenceException (javax.persistence.PersistenceException)30 HashMap (java.util.HashMap)29 Map (java.util.Map)25 FailureExpected (org.hibernate.testing.FailureExpected)23 Serializable (java.io.Serializable)22