Search in sources :

Example 56 with Transaction

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

the class IdClassTest method testIdClassInSuperclass.

@Test
public void testIdClassInSuperclass() throws Exception {
    Tower tower = new Tower();
    tower.latitude = 10.3;
    tower.longitude = 45.4;
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(tower);
    s.flush();
    s.clear();
    Location loc = new Location();
    loc.latitude = tower.latitude;
    loc.longitude = tower.longitude;
    assertNotNull(s.get(Tower.class, loc));
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Tower(org.hibernate.test.annotations.id.sequences.entities.Tower) Session(org.hibernate.Session) Location(org.hibernate.test.annotations.id.sequences.entities.Location) Test(org.junit.Test)

Example 57 with Transaction

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

the class FormulaWithColumnTypesTest method testFormulaAnnotationWithTypeNames.

@Test
@TestForIssue(jiraKey = "HHH-9951")
public void testFormulaAnnotationWithTypeNames() {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();
    DisplayItem displayItem20 = new DisplayItem();
    displayItem20.setDisplayCode("20");
    DisplayItem displayItem03 = new DisplayItem();
    displayItem03.setDisplayCode("03");
    DisplayItem displayItem100 = new DisplayItem();
    displayItem100.setDisplayCode("100");
    session.persist(displayItem20);
    session.persist(displayItem03);
    session.persist(displayItem100);
    transaction.commit();
    session.close();
    // 1. Default sorting by display code natural ordering (resulting in 3-100-20).
    session = openSession();
    transaction = session.beginTransaction();
    List displayItems = session.createCriteria(DisplayItem.class).addOrder(Order.asc("displayCode")).list();
    assertNotNull(displayItems);
    assertEquals(displayItems.size(), 3);
    assertEquals("03", ((DisplayItem) displayItems.get(0)).getDisplayCode());
    assertEquals("100", ((DisplayItem) displayItems.get(1)).getDisplayCode());
    assertEquals("20", ((DisplayItem) displayItems.get(2)).getDisplayCode());
    transaction.commit();
    session.close();
    // 2. Sorting by the casted type (resulting in 3-20-100).
    session = openSession();
    transaction = session.beginTransaction();
    List displayItemsSortedByInteger = session.createCriteria(DisplayItem.class).addOrder(Order.asc("displayCodeAsInteger")).list();
    assertNotNull(displayItemsSortedByInteger);
    assertEquals(displayItemsSortedByInteger.size(), 3);
    assertEquals("03", ((DisplayItem) displayItemsSortedByInteger.get(0)).getDisplayCode());
    assertEquals("20", ((DisplayItem) displayItemsSortedByInteger.get(1)).getDisplayCode());
    assertEquals("100", ((DisplayItem) displayItemsSortedByInteger.get(2)).getDisplayCode());
    transaction.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 58 with Transaction

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

the class UnresolvedTypeTest method testAcceptsUnresolvedPropertyTypesIfATargetEntityIsExplicitlySet.

@Test
public void testAcceptsUnresolvedPropertyTypesIfATargetEntityIsExplicitlySet() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Gene item = new Gene();
    s.persist(item);
    s.flush();
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 59 with Transaction

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

the class UnresolvedTypeTest method testAcceptsUnresolvedPropertyTypesIfATypeExplicitlySet.

@Test
public void testAcceptsUnresolvedPropertyTypesIfATypeExplicitlySet() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Gene item = new Gene();
    item.setState(State.DORMANT);
    s.persist(item);
    s.flush();
    s.clear();
    item = (Gene) s.get(Gene.class, item.getId());
    assertEquals(State.DORMANT, item.getState());
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 60 with Transaction

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

the class BasicHibernateAnnotationsTest method testPolymorphism.

@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public void testPolymorphism() 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();
    s = openSession();
    tx = s.beginTransaction();
    Query query = s.createQuery("from java.lang.Object");
    assertEquals(0, query.list().size());
    query = s.createQuery("from Forest");
    assertTrue(0 < query.list().size());
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Session(org.hibernate.Session) 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