Search in sources :

Example 16 with FailureExpected

use of org.hibernate.testing.orm.junit.FailureExpected in project hibernate-orm by hibernate.

the class CallbacksTest method testPostUpdateCollection.

@Test
@FailureExpected(reason = "collection change does not trigger an event", jiraKey = "EJB-288")
public void testPostUpdateCollection(EntityManagerFactoryScope scope) {
    scope.inEntityManager(entityManager -> {
        try {
            // create a cat
            Cat cat = new Cat();
            cat.setLength(23);
            cat.setAge(2);
            cat.setName("Beetle");
            cat.setDateOfBirth(new Date());
            entityManager.getTransaction().begin();
            entityManager.persist(cat);
            entityManager.getTransaction().commit();
            // assert it is persisted
            List ids = Cat.getIdList();
            Object id = Cat.getIdList().get(ids.size() - 1);
            assertNotNull(id);
            // add a kitten to the cat - triggers PostCollectionRecreateEvent
            int postVersion = Cat.postVersion;
            entityManager.getTransaction().begin();
            Kitten kitty = new Kitten();
            kitty.setName("kitty");
            List kittens = new ArrayList<Kitten>();
            kittens.add(kitty);
            cat.setKittens(kittens);
            entityManager.getTransaction().commit();
            assertEquals(postVersion + 1, Cat.postVersion, "Post version should have been incremented.");
            Kitten tom = new Kitten();
            tom.setName("Tom");
            // add another kitten - triggers PostCollectionUpdateEvent.
            postVersion = Cat.postVersion;
            entityManager.getTransaction().begin();
            cat.getKittens().add(tom);
            entityManager.getTransaction().commit();
            assertEquals(postVersion + 1, Cat.postVersion, "Post version should have been incremented.");
            // delete a kitty - triggers PostCollectionUpdateEvent
            postVersion = Cat.postVersion;
            entityManager.getTransaction().begin();
            cat.getKittens().remove(tom);
            entityManager.getTransaction().commit();
            assertEquals(postVersion + 1, Cat.postVersion, "Post version should have been incremented.");
        } catch (Exception e) {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            throw e;
        }
    });
}
Also used : Cat(org.hibernate.orm.test.jpa.Cat) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) Kitten(org.hibernate.orm.test.jpa.Kitten) Test(org.junit.jupiter.api.Test) FailureExpected(org.hibernate.testing.orm.junit.FailureExpected)

Example 17 with FailureExpected

use of org.hibernate.testing.orm.junit.FailureExpected in project hibernate-orm by hibernate.

the class PersistentSetTest method testCompositeElementCollectionDirtyChecking.

@Test
@FailureExpected(jiraKey = "HHH-2485")
public void testCompositeElementCollectionDirtyChecking(SessionFactoryScope scope) {
    Container c = new Container("p1");
    scope.inTransaction(session -> {
        Container.Content c1 = new Container.Content("c1");
        c.getContents().add(c1);
        session.save(c);
    });
    CollectionStatistics stats = scope.getSessionFactory().getStatistics().getCollectionStatistics(Container.class.getName() + ".contents");
    long recreateCount = stats.getRecreateCount();
    long updateCount = stats.getUpdateCount();
    scope.inTransaction(session -> {
        Container c1 = session.get(Container.class, c.getId());
        assertEquals(1, c1.getContents().size());
    });
    assertEquals(1, c.getContents().size());
    assertEquals(recreateCount, stats.getRecreateCount());
    assertEquals(updateCount, stats.getUpdateCount());
    scope.inTransaction(session -> {
        Container c1 = session.get(Container.class, c.getId());
        assertEquals(1, c1.getContents().size());
        session.delete(c1);
    });
}
Also used : CollectionStatistics(org.hibernate.stat.CollectionStatistics) Test(org.junit.jupiter.api.Test) FailureExpected(org.hibernate.testing.orm.junit.FailureExpected)

Example 18 with FailureExpected

use of org.hibernate.testing.orm.junit.FailureExpected in project hibernate-orm by hibernate.

the class ComponentTest method testComponentQueryMethodNoParensFailureExpected.

@Test
@RequiresDialect(value = SybaseASEDialect.class)
@FailureExpected(jiraKey = "HHH-3150")
public void testComponentQueryMethodNoParensFailureExpected() {
    // Sybase should translate "current_timestamp" in HQL to "getdate()";
    // This fails currently due to HHH-3510. The following test should be
    // deleted and testComponentQueries() should be updated (as noted
    // in that test case) when HHH-3510 is fixed.
    inTransaction(s -> {
        Employee emp = new Employee();
        emp.setHireDate(new Date());
        emp.setPerson(new Person());
        emp.getPerson().setName("steve");
        emp.getPerson().setDob(new Date());
        s.save(emp);
        s.createQuery("from Employee e where e.person = (current_timestamp, 'steve')").list();
        s.delete(emp);
    });
}
Also used : Date(java.util.Date) BaseSessionFactoryFunctionalTest(org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest) Test(org.junit.jupiter.api.Test) FailureExpected(org.hibernate.testing.orm.junit.FailureExpected) RequiresDialect(org.hibernate.testing.orm.junit.RequiresDialect)

Example 19 with FailureExpected

use of org.hibernate.testing.orm.junit.FailureExpected in project hibernate-orm by hibernate.

the class DeleteOneToManyOrphansTest method testOrphanedWhileManagedMergeOwner.

@Test
@TestForIssue(jiraKey = "HHH-9568")
@FailureExpected(jiraKey = "HHH-9568")
public void testOrphanedWhileManagedMergeOwner(EntityManagerFactoryScope scope) {
    Long productId = scope.fromTransaction(entityManager -> {
        List results = entityManager.createQuery("from Feature").getResultList();
        assertEquals(1, results.size());
        results = entityManager.createQuery("from Product").getResultList();
        assertEquals(1, results.size());
        Product product = (Product) results.get(0);
        assertEquals(1, product.getFeatures().size());
        product.getFeatures().clear();
        entityManager.merge(product);
        return product.getId();
    });
    scope.inTransaction(entityManager -> {
        Product _product = entityManager.find(Product.class, productId);
        assertEquals(0, _product.getFeatures().size());
        List results = entityManager.createQuery("from Feature").getResultList();
        assertEquals(0, results.size());
        results = entityManager.createQuery("from Product").getResultList();
        assertEquals(1, results.size());
    });
}
Also used : List(java.util.List) Test(org.junit.jupiter.api.Test) FailureExpected(org.hibernate.testing.orm.junit.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Example 20 with FailureExpected

use of org.hibernate.testing.orm.junit.FailureExpected in project hibernate-orm by hibernate.

the class DeleteOneToManyOrphansTest method testReplacedWhileManaged.

@Test
@TestForIssue(jiraKey = "HHH-9568")
@FailureExpected(jiraKey = "HHH-9568")
public void testReplacedWhileManaged(EntityManagerFactoryScope scope) {
    Long featureNewId = scope.fromTransaction(entityManager -> {
        List results = entityManager.createQuery("from Feature").getResultList();
        assertEquals(1, results.size());
        results = entityManager.createQuery("from Product").getResultList();
        assertEquals(1, results.size());
        Product product = (Product) results.get(0);
        assertEquals(1, product.getFeatures().size());
        // Replace with a new Feature instance
        product.getFeatures().remove(0);
        Feature featureNew = new Feature();
        featureNew.setName("Feature 2");
        featureNew.setProduct(product);
        product.getFeatures().add(featureNew);
        entityManager.persist(featureNew);
        return featureNew.getId();
    });
    scope.inTransaction(entityManager -> {
        List results = entityManager.createQuery("from Feature").getResultList();
        assertEquals(1, results.size());
        Feature featureQueried = (Feature) results.get(0);
        assertEquals(featureNewId, featureQueried.getId());
        results = entityManager.createQuery("from Product").getResultList();
        assertEquals(1, results.size());
        Product productQueried = (Product) results.get(0);
        assertEquals(1, productQueried.getFeatures().size());
        assertEquals(featureQueried, productQueried.getFeatures().get(0));
    });
}
Also used : List(java.util.List) Test(org.junit.jupiter.api.Test) FailureExpected(org.hibernate.testing.orm.junit.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

FailureExpected (org.hibernate.testing.orm.junit.FailureExpected)25 Test (org.junit.jupiter.api.Test)25 SQLStatementInspector (org.hibernate.testing.jdbc.SQLStatementInspector)6 JiraKey (org.hibernate.testing.orm.junit.JiraKey)6 List (java.util.List)5 TestForIssue (org.hibernate.testing.TestForIssue)4 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)3 Query (org.hibernate.query.Query)3 SelectionQuery (org.hibernate.query.SelectionQuery)3 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 UniqueConstraint (jakarta.persistence.UniqueConstraint)2 Date (java.util.Date)2 CollectionStatistics (org.hibernate.stat.CollectionStatistics)2 BaseUnitTest (org.hibernate.testing.orm.junit.BaseUnitTest)2 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)1 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)1 Predicate (jakarta.persistence.criteria.Predicate)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1