Search in sources :

Example 81 with Transaction

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

the class VersionedLobTest method testVersionUnchangedPrimitiveByteArray.

@Test
public void testVersionUnchangedPrimitiveByteArray() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    VersionedCompiledCode cc = createCompiledCode();
    int codeSize = 5;
    byte[] full = new byte[codeSize];
    for (int i = 0; i < codeSize; i++) {
        full[i] = (byte) (1 + i);
    }
    cc.setFullCode(full);
    s.persist(cc);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    VersionedCompiledCode recompiled = getCompiledCodeClass().cast(s.get(getCompiledCodeClass(), getId(cc)));
    assertEquals(recompiled.getFullCode()[codeSize - 1], cc.getFullCode()[codeSize - 1]);
    assertEquals(recompiled.getVersion(), Integer.valueOf(0));
    s.flush();
    assertEquals(recompiled.getVersion(), Integer.valueOf(0));
    s.delete(recompiled);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 82 with Transaction

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

the class VersionedLobTest method testVersionUnchangedPrimitiveCharArray.

@Test
public void testVersionUnchangedPrimitiveCharArray() throws Exception {
    VersionedBook book = createBook();
    Editor editor = new Editor();
    editor.setName("O'Reilly");
    book.setEditor(editor);
    book.setCode2(new char[] { 'r' });
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    s.persist(book);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    VersionedBook loadedBook = getBookClass().cast(s.get(getBookClass(), getId(book)));
    assertEquals(loadedBook.getVersion(), Integer.valueOf(0));
    s.flush();
    assertEquals(loadedBook.getVersion(), Integer.valueOf(0));
    s.delete(loadedBook);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 83 with Transaction

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

the class ManyToManyTest method testCanUseCriteriaQuery.

@Test
public void testCanUseCriteriaQuery() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Store fnac = new Store();
    fnac.setName("Fnac");
    Supplier emi = new Supplier();
    emi.setName("Emmanuel");
    emi.setSuppStores(new HashSet<Store>());
    fnac.setSuppliers(new HashSet<Supplier>());
    fnac.getSuppliers().add(emi);
    emi.getSuppStores().add(fnac);
    s.persist(fnac);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    List result = s.createCriteria(Supplier.class).createAlias("suppStores", "s").add(Restrictions.eq("s.name", "Fnac")).list();
    assertEquals(1, result.size());
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 84 with Transaction

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

the class EagerIndexedCollectionTest method testTemporalKeyMap.

@Test
public void testTemporalKeyMap() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Atmosphere atm = new Atmosphere();
    atm.colorPerDate.put(new Date(1234567000), "red");
    s.persist(atm);
    s.flush();
    s.clear();
    atm = (Atmosphere) s.get(Atmosphere.class, atm.id);
    assertEquals(1, atm.colorPerDate.size());
    final Date date = atm.colorPerDate.keySet().iterator().next();
    final long diff = new Date(1234567000).getTime() - date.getTime();
    assertTrue("24h diff max", diff >= 0 && diff < 24 * 60 * 60 * 1000);
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 85 with Transaction

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

the class EagerIndexedCollectionTest method testEntityKeyElementTarget.

@Test
public void testEntityKeyElementTarget() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Atmosphere atm = new Atmosphere();
    Gas o2 = new Gas();
    o2.name = "oxygen";
    atm.composition.put(o2, 94.3);
    s.persist(o2);
    s.persist(atm);
    s.flush();
    s.clear();
    atm = (Atmosphere) s.get(Atmosphere.class, atm.id);
    assertTrue(Hibernate.isInitialized(atm.composition));
    assertEquals(1, atm.composition.size());
    assertEquals(o2.name, atm.composition.keySet().iterator().next().name);
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Gas(org.hibernate.test.annotations.indexcoll.Gas) Session(org.hibernate.Session) Test(org.junit.Test)

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