Search in sources :

Example 21 with Transaction

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

the class SortTest method testOrderBy.

@Test
@SuppressWarnings({ "unchecked" })
public void testOrderBy() {
    Search s = new Search("Hibernate");
    s.getSearchResults().add("jboss.com");
    s.getSearchResults().add("hibernate.org");
    s.getSearchResults().add("HiA");
    Session sess = openSession();
    Transaction tx = sess.beginTransaction();
    sess.persist(s);
    sess.flush();
    sess.clear();
    s = (Search) sess.createCriteria(Search.class).uniqueResult();
    assertFalse(Hibernate.isInitialized(s.getSearchResults()));
    Iterator iter = s.getSearchResults().iterator();
    assertEquals(iter.next(), "HiA");
    assertEquals(iter.next(), "hibernate.org");
    assertEquals(iter.next(), "jboss.com");
    assertFalse(iter.hasNext());
    sess.clear();
    s = (Search) sess.createCriteria(Search.class).setFetchMode("searchResults", FetchMode.JOIN).uniqueResult();
    assertTrue(Hibernate.isInitialized(s.getSearchResults()));
    iter = s.getSearchResults().iterator();
    assertEquals(iter.next(), "HiA");
    assertEquals(iter.next(), "hibernate.org");
    assertEquals(iter.next(), "jboss.com");
    assertFalse(iter.hasNext());
    sess.clear();
    s = (Search) sess.createQuery("from Search s left join fetch s.searchResults").uniqueResult();
    assertTrue(Hibernate.isInitialized(s.getSearchResults()));
    iter = s.getSearchResults().iterator();
    assertEquals(iter.next(), "HiA");
    assertEquals(iter.next(), "hibernate.org");
    assertEquals(iter.next(), "jboss.com");
    assertFalse(iter.hasNext());
    sess.delete(s);
    tx.commit();
    sess.close();
}
Also used : Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with Transaction

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

the class VersionTest method testVersionShortCircuitFlush.

@Test
public void testVersionShortCircuitFlush() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Person gavin = new Person("Gavin");
    new Thing("Passport", gavin);
    s.persist(gavin);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Thing passp = (Thing) s.get(Thing.class, "Passport");
    passp.setLongDescription("blah blah blah");
    s.createQuery("from Person").list();
    s.createQuery("from Person").list();
    s.createQuery("from Person").list();
    t.commit();
    s.close();
    assertEquals(passp.getVersion(), 1);
    s = openSession();
    t = s.beginTransaction();
    s.createQuery("delete from Thing").executeUpdate();
    s.createQuery("delete from Person").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with Transaction

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

the class DbVersionTest method testCollectionVersion.

@Test
public void testCollectionVersion() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    User steve = new User("steve");
    s.persist(steve);
    Group admin = new Group("admin");
    s.persist(admin);
    t.commit();
    s.close();
    Timestamp steveTimestamp = steve.getTimestamp();
    // For dialects (Oracle8 for example) which do not return "true
    // timestamps" sleep for a bit to allow the db date-time increment...
    Thread.sleep(1500);
    s = openSession();
    t = s.beginTransaction();
    steve = (User) s.get(User.class, steve.getId());
    admin = (Group) s.get(Group.class, admin.getId());
    steve.getGroups().add(admin);
    admin.getUsers().add(steve);
    t.commit();
    s.close();
    assertFalse("owner version not incremented", StandardBasicTypes.TIMESTAMP.isEqual(steveTimestamp, steve.getTimestamp()));
    steveTimestamp = steve.getTimestamp();
    Thread.sleep(1500);
    s = openSession();
    t = s.beginTransaction();
    steve = (User) s.get(User.class, steve.getId());
    steve.getGroups().clear();
    t.commit();
    s.close();
    assertFalse("owner version not incremented", StandardBasicTypes.TIMESTAMP.isEqual(steveTimestamp, steve.getTimestamp()));
    s = openSession();
    t = s.beginTransaction();
    s.delete(s.load(User.class, steve.getId()));
    s.delete(s.load(Group.class, admin.getId()));
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Timestamp(java.sql.Timestamp) Session(org.hibernate.Session) Test(org.junit.Test)

Example 24 with Transaction

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

the class DbVersionTest method testCollectionNoVersion.

@Test
public void testCollectionNoVersion() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    User steve = new User("steve");
    s.persist(steve);
    Permission perm = new Permission("silly", "user", "rw");
    s.persist(perm);
    t.commit();
    s.close();
    Timestamp steveTimestamp = steve.getTimestamp();
    s = openSession();
    t = s.beginTransaction();
    steve = (User) s.get(User.class, steve.getId());
    perm = (Permission) s.get(Permission.class, perm.getId());
    steve.getPermissions().add(perm);
    t.commit();
    s.close();
    assertTrue("owner version was incremented", StandardBasicTypes.TIMESTAMP.isEqual(steveTimestamp, steve.getTimestamp()));
    s = openSession();
    t = s.beginTransaction();
    steve = (User) s.get(User.class, steve.getId());
    steve.getPermissions().clear();
    t.commit();
    s.close();
    assertTrue("owner version was incremented", StandardBasicTypes.TIMESTAMP.isEqual(steveTimestamp, steve.getTimestamp()));
    s = openSession();
    t = s.beginTransaction();
    s.delete(s.load(User.class, steve.getId()));
    s.delete(s.load(Permission.class, perm.getId()));
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Timestamp(java.sql.Timestamp) Session(org.hibernate.Session) Test(org.junit.Test)

Example 25 with Transaction

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

the class SybaseTimestampVersioningTest method testCollectionNoVersion.

@Test
@SuppressWarnings({ "unchecked" })
public void testCollectionNoVersion() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    User steve = new User("steve");
    s.persist(steve);
    Permission perm = new Permission("silly", "user", "rw");
    s.persist(perm);
    t.commit();
    s.close();
    byte[] steveTimestamp = steve.getTimestamp();
    s = openSession();
    t = s.beginTransaction();
    steve = (User) s.get(User.class, steve.getId());
    perm = (Permission) s.get(Permission.class, perm.getId());
    steve.getPermissions().add(perm);
    t.commit();
    s.close();
    assertTrue("owner version was incremented", BinaryType.INSTANCE.isEqual(steveTimestamp, steve.getTimestamp()));
    s = openSession();
    t = s.beginTransaction();
    steve = (User) s.get(User.class, steve.getId());
    steve.getPermissions().clear();
    t.commit();
    s.close();
    assertTrue("owner version was incremented", BinaryType.INSTANCE.isEqual(steveTimestamp, steve.getTimestamp()));
    s = openSession();
    t = s.beginTransaction();
    s.delete(s.load(User.class, steve.getId()));
    s.delete(s.load(Permission.class, perm.getId()));
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) 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