Search in sources :

Example 61 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class BasicHibernateAnnotationsTest method testSerialized.

@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public void testSerialized() throws Exception {
    Forest forest = new Forest();
    forest.setName("Shire");
    Country country = new Country();
    country.setName("Middle Earth");
    forest.setCountry(country);
    Set<Country> near = new HashSet<Country>();
    country = new Country();
    country.setName("Mordor");
    near.add(country);
    country = new Country();
    country.setName("Gondor");
    near.add(country);
    country = new Country();
    country.setName("Eriador");
    near.add(country);
    forest.setNear(near);
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    s.persist(forest);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    forest = (Forest) s.get(Forest.class, forest.getId());
    assertNotNull(forest);
    country = forest.getCountry();
    assertNotNull(country);
    assertEquals(country.getName(), forest.getCountry().getName());
    near = forest.getNear();
    assertTrue("correct number of nearby countries", near.size() == 3);
    for (Iterator iter = near.iterator(); iter.hasNext(); ) {
        country = (Country) iter.next();
        String name = country.getName();
        assertTrue("found expected nearby country " + name, (name.equals("Mordor") || name.equals("Gondor") || name.equals("Eriador")));
    }
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) HashSet(java.util.HashSet) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature)

Example 62 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class BasicHibernateAnnotationsTest method testType.

@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public void testType() throws Exception {
    Forest f = new Forest();
    f.setName("Broceliande");
    String description = "C'est une enorme foret enchantee ou vivais Merlin et toute la clique";
    f.setLongDescription(description);
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    s.persist(f);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    f = (Forest) s.get(Forest.class, f.getId());
    assertNotNull(f);
    assertEquals(description, f.getLongDescription());
    s.delete(f);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature)

Example 63 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class BasicHibernateAnnotationsTest method testNonLazy.

@Test
public void testNonLazy() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Forest f = new Forest();
    Tree t = new Tree();
    t.setName("Basic one");
    s.persist(f);
    s.persist(t);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    f = (Forest) s.load(Forest.class, f.getId());
    t = (Tree) s.load(Tree.class, t.getId());
    assertFalse("Default should be lazy", Hibernate.isInitialized(f));
    assertTrue("Tree is not lazy", Hibernate.isInitialized(t));
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 64 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class BasicHibernateAnnotationsTest method testFilter.

@Test
public void testFilter() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    s.createQuery("delete Forest").executeUpdate();
    Forest f1 = new Forest();
    f1.setLength(2);
    s.persist(f1);
    Forest f2 = new Forest();
    f2.setLength(20);
    s.persist(f2);
    Forest f3 = new Forest();
    f3.setLength(200);
    s.persist(f3);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    s.enableFilter("betweenLength").setParameter("minLength", 5).setParameter("maxLength", 50);
    long count = ((Long) s.createQuery("select count(*) from Forest").iterate().next()).intValue();
    assertEquals(1, count);
    s.disableFilter("betweenLength");
    s.enableFilter("minLength").setParameter("minLength", 5);
    count = ((Long) s.createQuery("select count(*) from Forest").iterate().next()).longValue();
    assertEquals(2l, count);
    s.disableFilter("minLength");
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 65 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class BasicHibernateAnnotationsTest method testVersioning.

@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
@SkipForDialect(value = TeradataDialect.class, comment = "One transaction hangs the other")
public void testVersioning() throws Exception {
    Forest forest = new Forest();
    forest.setName("Fontainebleau");
    forest.setLength(33);
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    s.persist(forest);
    tx.commit();
    s.close();
    Session parallelSession = openSession();
    Transaction parallelTx = parallelSession.beginTransaction();
    s = openSession();
    tx = s.beginTransaction();
    forest = (Forest) parallelSession.get(Forest.class, forest.getId());
    Forest reloadedForest = (Forest) s.get(Forest.class, forest.getId());
    reloadedForest.setLength(11);
    assertNotSame(forest, reloadedForest);
    tx.commit();
    s.close();
    forest.setLength(22);
    try {
        parallelTx.commit();
        fail("All optimistic locking should have make it fail");
    } catch (OptimisticLockException e) {
        if (parallelTx != null)
            parallelTx.rollback();
    } finally {
        parallelSession.close();
    }
    s = openSession();
    tx = s.beginTransaction();
    s.delete(s.get(Forest.class, forest.getId()));
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) OptimisticLockException(javax.persistence.OptimisticLockException) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature)

Aggregations

Transaction (org.hibernate.Transaction)1273 Session (org.hibernate.Session)1251 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