Search in sources :

Example 26 with Transaction

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

the class SybaseTimestampVersioningTest method testCollectionVersion.

@Test
@SuppressWarnings({ "unchecked" })
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();
    byte[] steveTimestamp = steve.getTimestamp();
    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", BinaryType.INSTANCE.isEqual(steveTimestamp, steve.getTimestamp()));
    steveTimestamp = steve.getTimestamp();
    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", BinaryType.INSTANCE.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) Session(org.hibernate.Session) Test(org.junit.Test)

Example 27 with Transaction

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

the class TypeParameterTest method deleteData.

private void deleteData() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.createQuery("delete from Widget").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 28 with Transaction

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

the class TypeParameterTest method testLoading.

@Test
public void testLoading() throws Exception {
    initData();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Widget obj = (Widget) s.createQuery("from Widget o where o.string = :string").setString("string", "all-normal").uniqueResult();
    assertEquals("Non-Default value incorrectly loaded", obj.getValueOne(), 7);
    assertEquals("Non-Default value incorrectly loaded", obj.getValueTwo(), 8);
    assertEquals("Non-Default value incorrectly loaded", obj.getValueThree(), 9);
    assertEquals("Non-Default value incorrectly loaded", obj.getValueFour(), 10);
    obj = (Widget) s.createQuery("from Widget o where o.string = :string").setString("string", "all-default").uniqueResult();
    assertEquals("Default value incorrectly loaded", obj.getValueOne(), 1);
    assertEquals("Default value incorrectly loaded", obj.getValueTwo(), 2);
    assertEquals("Default value incorrectly loaded", obj.getValueThree(), -1);
    assertEquals("Default value incorrectly loaded", obj.getValueFour(), -5);
    t.commit();
    s.close();
    deleteData();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 29 with Transaction

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

the class TypeParameterTest method initData.

private void initData() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Widget obj = new Widget();
    obj.setValueOne(7);
    obj.setValueTwo(8);
    obj.setValueThree(9);
    obj.setValueFour(10);
    obj.setString("all-normal");
    s.save(obj);
    obj = new Widget();
    obj.setValueOne(1);
    obj.setValueTwo(2);
    obj.setValueThree(-1);
    obj.setValueFour(-5);
    obj.setString("all-default");
    s.save(obj);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 30 with Transaction

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

the class UnconstrainedTest method testUnconstrained.

@Test
public void testUnconstrained() {
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    Person p = new Person("gavin");
    p.setEmployeeId("123456");
    session.persist(p);
    tx.commit();
    session.close();
    session = openSession();
    tx = session.beginTransaction();
    p = (Person) session.get(Person.class, "gavin");
    assertNull(p.getEmployee());
    p.setEmployee(new Employee("123456"));
    tx.commit();
    session.close();
    session = openSession();
    tx = session.beginTransaction();
    p = (Person) session.get(Person.class, "gavin");
    assertTrue(Hibernate.isInitialized(p.getEmployee()));
    assertNotNull(p.getEmployee());
    session.delete(p);
    tx.commit();
    session.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