Search in sources :

Example 46 with Transaction

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

the class EmbeddedTest method testManyToOneInsideComponent.

@Test
public void testManyToOneInsideComponent() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Person p = new Person();
    Country bornIn = new Country();
    bornIn.setIso2("FR");
    bornIn.setName("France");
    p.bornIn = bornIn;
    p.name = "Emmanuel";
    AddressType type = new AddressType();
    type.setName("Primary Home");
    s.persist(type);
    Country currentCountry = new Country();
    currentCountry.setIso2("US");
    currentCountry.setName("USA");
    Address add = new Address();
    add.address1 = "4 square street";
    add.city = "San diego";
    add.country = currentCountry;
    add.type = type;
    p.address = add;
    s.persist(p);
    tx.commit();
    s = openSession();
    tx = s.beginTransaction();
    Query q = s.createQuery("select p from Person p where p.address.city = :city");
    q.setString("city", add.city);
    List result = q.list();
    Person samePerson = (Person) result.get(0);
    assertNotNull(samePerson.address.type);
    assertEquals(type.getName(), samePerson.address.type.getName());
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 47 with Transaction

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

the class EmbeddedTest method testEmbeddedAndOneToManyHql.

@Test
@TestForIssue(jiraKey = "HHH-9642")
public void testEmbeddedAndOneToManyHql() throws Exception {
    Session s;
    s = openSession();
    Transaction tx = s.beginTransaction();
    InternetProvider provider = new InternetProvider();
    provider.setBrandName("Fido");
    LegalStructure structure = new LegalStructure();
    structure.setCountry("Canada");
    structure.setName("Rogers");
    provider.setOwner(structure);
    s.persist(provider);
    Manager manager = new Manager();
    manager.setName("Bill");
    manager.setEmployer(provider);
    structure.getTopManagement().add(manager);
    s.persist(manager);
    tx.commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    InternetProvider internetProviderQueried = (InternetProvider) s.createQuery("from InternetProvider").uniqueResult();
    assertFalse(Hibernate.isInitialized(internetProviderQueried.getOwner().getTopManagement()));
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    internetProviderQueried = (InternetProvider) s.createQuery("from InternetProvider i join fetch i.owner.topManagement").uniqueResult();
    assertTrue(Hibernate.isInitialized(internetProviderQueried.getOwner().getTopManagement()));
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    internetProviderQueried = (InternetProvider) s.createQuery("from InternetProvider i join fetch i.owner o join fetch o.topManagement").uniqueResult();
    assertTrue(Hibernate.isInitialized(internetProviderQueried.getOwner().getTopManagement()));
    s.getTransaction().commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    provider = (InternetProvider) s.get(InternetProvider.class, provider.getId());
    manager = provider.getOwner().getTopManagement().iterator().next();
    s.delete(manager);
    s.delete(provider);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 48 with Transaction

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

the class EmbeddedTest method testQueryWithEmbeddedParameterAllNull.

@Test
@TestForIssue(jiraKey = "HHH-8172")
@FailureExpected(jiraKey = "HHH-8172")
public void testQueryWithEmbeddedParameterAllNull() throws Exception {
    Session s;
    Transaction tx;
    Person person = new Person();
    Address a = new Address();
    Country c = new Country();
    Country bornCountry = new Country();
    assertNull(bornCountry.getIso2());
    assertNull(bornCountry.getName());
    a.address1 = "colorado street";
    a.city = "Springfield";
    a.country = c;
    person.address = a;
    person.bornIn = bornCountry;
    person.name = "Homer";
    TransactionUtil.doInHibernate(this::sessionFactory, session -> {
        session.persist(person);
    });
    TransactionUtil.doInHibernate(this::sessionFactory, session -> {
        Person p = (Person) session.createQuery("from Person p where p.bornIn = :b").setParameter("b", person.bornIn).uniqueResult();
        assertNotNull(p);
        assertNotNull(p.address);
        assertEquals("Springfield", p.address.city);
        assertNotNull(p.address.country);
        assertEquals("DM", p.address.country.getIso2());
        assertNull(p.bornIn);
    });
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Example 49 with Transaction

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

the class IdClassGeneratedValueManyToOneTest method testComplexIdClass.

@Test
public void testComplexIdClass() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Customer c1 = new Customer("foo", "bar", "contact1", "100", new BigDecimal(1000), new BigDecimal(1000), new BigDecimal(1000));
    s.persist(c1);
    s.flush();
    s.clear();
    //      why does this cause a failure?        
    //		Customer c2 = new Customer(
    //              "foo1", "bar1", "contact2", "200", new BigDecimal( 2000 ), new BigDecimal( 2000 ), new BigDecimal( 2000 ));
    //		s.persist( c2 );
    //		s.flush();
    //        s.clear();
    Item boat = new Item();
    boat.setId("1");
    boat.setName("cruiser");
    boat.setPrice(new BigDecimal(500));
    boat.setDescription("a boat");
    boat.setCategory(42);
    s.persist(boat);
    s.flush();
    s.clear();
    c1.addInventory(boat, 10, new BigDecimal(5000));
    s.merge(c1);
    s.flush();
    s.clear();
    Customer c12 = (Customer) s.createQuery("select c from Customer c").uniqueResult();
    List<CustomerInventory> inventory = c12.getInventories();
    assertEquals(1, inventory.size());
    assertEquals(10, inventory.get(0).getQuantity());
    tx.rollback();
    s.close();
    assertTrue(true);
}
Also used : Transaction(org.hibernate.Transaction) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 50 with Transaction

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

the class EmbeddedTest method testParent.

@Test
public void testParent() 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(loadedBook, loadedBook.getSummary().getSummarizedBook());
    s.delete(loadedBook);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) 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