Search in sources :

Example 56 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class InheritedEntityGraphTest method singleAttributeNodeInheritanceTest.

@Test
@TestForIssue(jiraKey = "HHH-10261")
public void singleAttributeNodeInheritanceTest() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Bar bar = new Bar();
    em.persist(bar);
    Foo foo = new Foo();
    foo.bar = bar;
    em.persist(foo);
    em.getTransaction().commit();
    em.clear();
    em.getTransaction().begin();
    EntityGraph<Foo> entityGraph = em.createEntityGraph(Foo.class);
    entityGraph.addSubgraph("bar");
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("javax.persistence.loadgraph", entityGraph);
    Foo result = em.find(Foo.class, foo.id, properties);
    assertTrue(Hibernate.isInitialized(result));
    assertTrue(Hibernate.isInitialized(result.bar));
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) HashMap(java.util.HashMap) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 57 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class OneToOneOrphanTest method testFlushTransientOneToOneNoCascade.

@Test
@TestForIssue(jiraKey = "HHH-9568")
public void testFlushTransientOneToOneNoCascade() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    B b = new B();
    A a = new A();
    a.setB(b);
    try {
        em.persist(a);
        em.flush();
        em.getTransaction().commit();
        fail("should have raised an IllegalStateException");
    } catch (IllegalStateException ex) {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
    // IllegalStateException caught as expected
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 58 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class DeleteMultiLevelOrphansTest method testDirectAndNestedAssociationsOrphanedWhileManaged.

@Test
@TestForIssue(jiraKey = "HHH-9091")
public void testDirectAndNestedAssociationsOrphanedWhileManaged() {
    createData();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    List results = em.createQuery("from Tranchenmodell").getResultList();
    assertEquals(1, results.size());
    results = em.createQuery("from Preisregelung").getResultList();
    assertEquals(1, results.size());
    Preisregelung preisregelung = (Preisregelung) results.get(0);
    Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
    assertNotNull(tranchenmodell);
    assertNotNull(tranchenmodell.getX());
    assertEquals(2, tranchenmodell.getTranchen().size());
    assertNotNull(tranchenmodell.getTranchen().get(0).getY());
    preisregelung.setTranchenmodell(null);
    tranchenmodell.setX(null);
    tranchenmodell.getTranchen().get(0).setY(null);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    preisregelung = (Preisregelung) em.find(Preisregelung.class, preisregelung.getId());
    assertNull(preisregelung.getTranchenmodell());
    results = em.createQuery("from Tranchenmodell").getResultList();
    assertEquals(0, results.size());
    results = em.createQuery("from Tranche").getResultList();
    assertEquals(0, results.size());
    results = em.createQuery("from X").getResultList();
    assertEquals(0, results.size());
    results = em.createQuery("from Y").getResultList();
    assertEquals(0, results.size());
    results = em.createQuery("from Preisregelung").getResultList();
    assertEquals(1, results.size());
    em.getTransaction().commit();
    em.close();
    cleanupData();
}
Also used : EntityManager(javax.persistence.EntityManager) List(java.util.List) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 59 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class DeleteMultiLevelOrphansTest method testReplacedDirectAssociationWhileManaged.

@Test
@TestForIssue(jiraKey = "HHH-9091")
public void testReplacedDirectAssociationWhileManaged() {
    createData();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    List results = em.createQuery("from Tranchenmodell").getResultList();
    assertEquals(1, results.size());
    results = em.createQuery("from Preisregelung").getResultList();
    assertEquals(1, results.size());
    Preisregelung preisregelung = (Preisregelung) results.get(0);
    Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
    assertNotNull(tranchenmodell);
    assertNotNull(tranchenmodell.getX());
    assertEquals(2, tranchenmodell.getTranchen().size());
    assertNotNull(tranchenmodell.getTranchen().get(0).getY());
    // Create a new Tranchenmodell with new direct and nested associations
    Tranchenmodell tranchenmodellNew = new Tranchenmodell();
    X xNew = new X();
    tranchenmodellNew.setX(xNew);
    xNew.setTranchenmodell(tranchenmodellNew);
    Tranche trancheNew = new Tranche();
    tranchenmodellNew.getTranchen().add(trancheNew);
    trancheNew.setTranchenmodell(tranchenmodellNew);
    Y yNew = new Y();
    trancheNew.setY(yNew);
    yNew.setTranche(trancheNew);
    // Replace with a new Tranchenmodell instance containing new direct and nested associations
    preisregelung.setTranchenmodell(tranchenmodellNew);
    tranchenmodellNew.setPreisregelung(preisregelung);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    results = em.createQuery("from Tranche").getResultList();
    assertEquals(1, results.size());
    results = em.createQuery("from Tranchenmodell").getResultList();
    assertEquals(1, results.size());
    results = em.createQuery("from X").getResultList();
    assertEquals(1, results.size());
    results = em.createQuery("from Y").getResultList();
    assertEquals(1, results.size());
    results = em.createQuery("from Preisregelung").getResultList();
    assertEquals(1, results.size());
    preisregelung = (Preisregelung) results.get(0);
    tranchenmodell = preisregelung.getTranchenmodell();
    assertNotNull(tranchenmodell);
    assertEquals(tranchenmodellNew.getId(), tranchenmodell.getId());
    assertNotNull(tranchenmodell.getX());
    assertEquals(xNew.getId(), tranchenmodell.getX().getId());
    assertEquals(1, tranchenmodell.getTranchen().size());
    assertEquals(trancheNew.getId(), tranchenmodell.getTranchen().get(0).getId());
    assertEquals(yNew.getId(), tranchenmodell.getTranchen().get(0).getY().getId());
    // Replace with a new Tranchenmodell instance with no associations
    tranchenmodellNew = new Tranchenmodell();
    preisregelung.setTranchenmodell(tranchenmodellNew);
    tranchenmodellNew.setPreisregelung(preisregelung);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    results = em.createQuery("from Tranchenmodell").getResultList();
    assertEquals(1, results.size());
    tranchenmodell = (Tranchenmodell) results.get(0);
    assertEquals(tranchenmodellNew.getId(), tranchenmodell.getId());
    results = em.createQuery("from Preisregelung").getResultList();
    assertEquals(1, results.size());
    preisregelung = (Preisregelung) results.get(0);
    assertEquals(tranchenmodell, preisregelung.getTranchenmodell());
    results = em.createQuery("from Tranche").getResultList();
    assertEquals(0, results.size());
    results = em.createQuery("from X").getResultList();
    assertEquals(0, results.size());
    results = em.createQuery("from Y").getResultList();
    assertEquals(0, results.size());
    em.getTransaction().commit();
    em.close();
    cleanupData();
}
Also used : EntityManager(javax.persistence.EntityManager) List(java.util.List) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 60 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class JarVisitorTest method testGetBytesFromInputStream.

@Test
@TestForIssue(jiraKey = "HHH-7835")
public void testGetBytesFromInputStream() throws Exception {
    File file = buildLargeJar();
    long start = System.currentTimeMillis();
    InputStream stream = new BufferedInputStream(new FileInputStream(file));
    int oldLength = getBytesFromInputStream(stream).length;
    stream.close();
    long oldTime = System.currentTimeMillis() - start;
    start = System.currentTimeMillis();
    stream = new BufferedInputStream(new FileInputStream(file));
    int newLength = ArchiveHelper.getBytesFromInputStream(stream).length;
    stream.close();
    long newTime = System.currentTimeMillis() - start;
    assertEquals(oldLength, newLength);
    assertTrue(oldTime > newTime);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TestForIssue (org.hibernate.testing.TestForIssue)649 Test (org.junit.Test)647 Session (org.hibernate.Session)357 EntityManager (javax.persistence.EntityManager)97 List (java.util.List)91 Transaction (org.hibernate.Transaction)88 MetadataSources (org.hibernate.boot.MetadataSources)47 ArrayList (java.util.ArrayList)38 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)38 Query (org.hibernate.Query)28 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)25 Metadata (org.hibernate.boot.Metadata)24 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)24 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Map (java.util.Map)22 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)19 HashMap (java.util.HashMap)18 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)18 PersistentClass (org.hibernate.mapping.PersistentClass)18 HibernateException (org.hibernate.HibernateException)16