Search in sources :

Example 51 with Transaction

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

the class EmbeddedTest method testEmbeddedInSecondaryTable.

@Test
public void testEmbeddedInSecondaryTable() throws Exception {
    Session s;
    s = openSession();
    s.getTransaction().begin();
    Book book = new Book();
    book.setIsbn("1234");
    book.setName("HiA Second Edition");
    Summary summary = new Summary();
    summary.setText("This is a HiA SE summary");
    summary.setSize(summary.getText().length());
    book.setSummary(summary);
    s.persist(book);
    s.getTransaction().commit();
    s.clear();
    Transaction tx = s.beginTransaction();
    Book loadedBook = (Book) s.get(Book.class, book.getIsbn());
    assertNotNull(loadedBook.getSummary());
    assertEquals(book.getSummary().getText(), loadedBook.getSummary().getText());
    s.delete(loadedBook);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 52 with Transaction

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

the class FetchingTest method testHibernateFetchingLazy.

@Test
public void testHibernateFetchingLazy() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Person p = new Person("Gavin", "King", "JBoss Inc");
    Stay stay = new Stay(null, new Date(), new Date(), "A380", "Blah", "Blah");
    Stay stay2 = new Stay(null, new Date(), new Date(), "A320", "Blah", "Blah");
    Stay stay3 = new Stay(null, new Date(), new Date(), "A340", "Blah", "Blah");
    stay.setOldPerson(p);
    stay2.setVeryOldPerson(p);
    stay3.setVeryOldPerson(p);
    p.addOldStay(stay);
    p.addVeryOldStay(stay2);
    p.addVeryOldStay(stay3);
    s.persist(p);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    p = (Person) s.createQuery("from Person p where p.firstName = :name").setParameter("name", "Gavin").uniqueResult();
    assertFalse(Hibernate.isInitialized(p.getOldStays()));
    assertEquals(1, p.getOldStays().size());
    assertFalse("lazy extra is failing", Hibernate.isInitialized(p.getOldStays()));
    s.clear();
    stay = (Stay) s.get(Stay.class, stay.getId());
    assertTrue(!Hibernate.isInitialized(stay.getOldPerson()));
    s.clear();
    stay3 = (Stay) s.get(Stay.class, stay3.getId());
    assertTrue("FetchMode.JOIN should overrides lazy options", Hibernate.isInitialized(stay3.getVeryOldPerson()));
    s.delete(stay3.getVeryOldPerson());
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 53 with Transaction

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

the class FetchingTest method testExtraLazy.

@Test
public void testExtraLazy() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Person p = new Person("Gavin", "King", "JBoss Inc");
    Stay stay = new Stay(p, new Date(), new Date(), "A380", "Blah", "Blah");
    p.getOrderedStay().add(stay);
    s.persist(p);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    p = (Person) s.createQuery("from Person p where p.firstName = :name").setParameter("name", "Gavin").uniqueResult();
    assertFalse(Hibernate.isInitialized(p.getOrderedStay()));
    assertEquals(1, p.getOrderedStay().size());
    assertFalse(Hibernate.isInitialized(p.getOrderedStay()));
    assertEquals("A380", p.getOrderedStay().get(0).getVessel());
    assertFalse(Hibernate.isInitialized(p.getOrderedStay()));
    s.delete(p);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 54 with Transaction

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

the class EnumIdTest method testEnumAsId.

@Test
public void testEnumAsId() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    PlanetCheatSheet mercury = new PlanetCheatSheet();
    mercury.setPlanet(Planet.MERCURY);
    mercury.setMass(3.303e+23);
    mercury.setRadius(2.4397e6);
    mercury.setNumberOfInhabitants(0);
    s.persist(mercury);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    PlanetCheatSheet mercuryFromDb = (PlanetCheatSheet) s.get(PlanetCheatSheet.class, mercury.getPlanet());
    assertNotNull(mercuryFromDb);
    log.debug(mercuryFromDb.toString());
    s.delete(mercuryFromDb);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    mercury = (PlanetCheatSheet) s.get(PlanetCheatSheet.class, Planet.MERCURY);
    assertNull(mercury);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) PlanetCheatSheet(org.hibernate.test.annotations.id.sequences.entities.PlanetCheatSheet) Session(org.hibernate.Session) Test(org.junit.Test)

Example 55 with Transaction

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

the class HibernateSequenceTest method testHibernateSequenceNextVal.

@Test
public void testHibernateSequenceNextVal() {
    Session session = openSession();
    Transaction txn = session.beginTransaction();
    HibernateSequenceEntity entity = new HibernateSequenceEntity();
    entity.setText("sample text");
    session.save(entity);
    txn.commit();
    session.close();
    Assert.assertNotNull(entity.getId());
}
Also used : Transaction(org.hibernate.Transaction) HibernateSequenceEntity(org.hibernate.test.annotations.id.sequences.entities.HibernateSequenceEntity) 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