Search in sources :

Example 86 with Transaction

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

the class SubclassTest method testFormula.

@Test
public void testFormula() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Rock guns = new Rock();
    guns.setAvgBeat(90);
    guns.setType(2);
    Noise white = new Noise();
    white.setAvgBeat(0);
    white.setType(null);
    s.persist(guns);
    s.persist(white);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    List result = s.createCriteria(Noise.class).list();
    assertNotNull(result);
    assertEquals(1, result.size());
    white = (Noise) result.get(0);
    assertNull(white.getType());
    s.delete(white);
    result = s.createCriteria(Rock.class).list();
    assertEquals(1, result.size());
    s.delete(result.get(0));
    result = s.createCriteria(Funk.class).list();
    assertEquals(0, result.size());
    tx.commit();
    s.close();
}
Also used : Rock(org.hibernate.test.annotations.inheritance.singletable.Rock) Transaction(org.hibernate.Transaction) Noise(org.hibernate.test.annotations.inheritance.singletable.Noise) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 87 with Transaction

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

the class SubclassTest method testEmbeddedSuperclass.

@Test
public void testEmbeddedSuperclass() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Plane p = new Plane();
    //sic
    p.setAlive(true);
    p.setAltitude(10000);
    p.setMetricAltitude(3000);
    p.setNbrOfSeats(150);
    p.setSerial("0123456789");
    s.persist(p);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    p = (Plane) s.get(Plane.class, p.getId());
    assertNotNull(p);
    assertEquals(true, p.isAlive());
    assertEquals(150, p.getNbrOfSeats());
    assertEquals(10000, p.getAltitude());
    assertEquals("0123456789", p.getSerial());
    assertFalse(3000 == p.getMetricAltitude());
    s.delete(p);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Plane(org.hibernate.test.annotations.Plane) Session(org.hibernate.Session) Test(org.junit.Test)

Example 88 with Transaction

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

the class ManyToManyTest method testReferencedColumnNameToSuperclass.

@Test
public void testReferencedColumnNameToSuperclass() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    BuildingCompany comp = new BuildingCompany();
    comp.setFoundedIn(new Date());
    comp.setName("Builder century corp.");
    s.persist(comp);
    Building building = new Building();
    building.setCompany(comp);
    s.persist(building);
    s.flush();
    s.clear();
    building = (Building) s.get(Building.class, building.getId());
    assertEquals(comp.getName(), building.getCompany().getName());
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 89 with Transaction

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

the class ManyToManyTest method testJoinedSubclassManyToMany.

@Test
public void testJoinedSubclassManyToMany() throws Exception {
    Session s = openSession();
    Zone a = new Zone();
    InspectorPrefixes ip = new InspectorPrefixes("dgi");
    Transaction tx = s.beginTransaction();
    s.save(a);
    s.save(ip);
    ip.getAreas().add(a);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    ip = (InspectorPrefixes) s.get(InspectorPrefixes.class, ip.getId());
    assertNotNull(ip);
    assertEquals(1, ip.getAreas().size());
    assertEquals(a.getId(), ip.getAreas().get(0).getId());
    s.delete(ip);
    s.delete(ip.getAreas().get(0));
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 90 with Transaction

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

the class ManyToManyTest method testOrderByEmployee.

@Test
public void testOrderByEmployee() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Employer employer = new Employer();
    Employee employee1 = new Employee();
    employee1.setName("Emmanuel");
    Employee employee2 = new Employee();
    employee2.setName("Alice");
    s.persist(employee1);
    s.persist(employee2);
    Set erColl = new HashSet();
    Collection eeColl = new ArrayList();
    Collection eeColl2 = new ArrayList();
    erColl.add(employee1);
    erColl.add(employee2);
    eeColl.add(employer);
    eeColl2.add(employer);
    employer.setEmployees(erColl);
    employee1.setEmployers(eeColl);
    employee2.setEmployers(eeColl2);
    s.flush();
    s.clear();
    employer = (Employer) s.get(Employer.class, employer.getId());
    assertNotNull(employer);
    assertNotNull(employer.getEmployees());
    assertEquals(2, employer.getEmployees().size());
    Employee eeFromDb = (Employee) employer.getEmployees().iterator().next();
    assertEquals(employee2.getName(), eeFromDb.getName());
    tx.rollback();
    s.close();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Session(org.hibernate.Session) HashSet(java.util.HashSet) 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