Search in sources :

Example 41 with Transaction

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

the class CompositeIdTest method testSecondaryTableWithCompositeId.

@Test
public void testSecondaryTableWithCompositeId() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Channel channel = new Channel();
    s.persist(channel);
    Presenter pres = new Presenter();
    pres.name = "Tim Russet";
    s.persist(pres);
    TvMagazinPk pk = new TvMagazinPk();
    TvProgram program = new TvProgram();
    program.time = new Date();
    program.id = pk;
    program.text = "Award Winning Programming";
    pk.channel = channel;
    pk.presenter = pres;
    s.persist(program);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    program = (TvProgram) s.createQuery("from TvProgram pr").uniqueResult();
    assertNotNull(program.id);
    assertNotNull(program.id.channel);
    assertEquals(channel.id, program.id.channel.id);
    assertNotNull(program.id.presenter);
    assertNotNull(program.text);
    assertEquals(pres.name, program.id.presenter.name);
    s.delete(program);
    s.delete(program.id.channel);
    s.delete(program.id.presenter);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 42 with Transaction

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

the class CompositeIdTest method testSecondaryTableWithIdClass.

@Test
public void testSecondaryTableWithIdClass() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Channel channel = new Channel();
    s.persist(channel);
    Presenter pres = new Presenter();
    pres.name = "Bob";
    s.persist(pres);
    TvProgramIdClass program = new TvProgramIdClass();
    program.time = new Date();
    program.channel = channel;
    program.presenter = pres;
    program.text = "Jump the shark programming";
    s.persist(program);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    program = (TvProgramIdClass) s.createQuery("from TvProgramIdClass pr").uniqueResult();
    assertNotNull(program.channel);
    assertEquals(channel.id, program.channel.id);
    assertNotNull(program.presenter);
    assertNotNull(program.text);
    assertEquals(pres.name, program.presenter.name);
    s.delete(program);
    s.delete(program.channel);
    s.delete(program.presenter);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 43 with Transaction

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

the class CompositeIdTest method testOneToOneInCompositePk.

@Test
public void testOneToOneInCompositePk() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    B b = new B();
    C c = new C();
    s.persist(b);
    s.persist(c);
    A a = new A();
    a.setAId(new AId());
    a.getAId().setB(b);
    a.getAId().setC(c);
    s.persist(a);
    s.flush();
    s.clear();
    a = (A) s.get(A.class, a.getAId());
    assertEquals(b.getId(), a.getAId().getB().getId());
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 44 with Transaction

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

the class EmbeddedTest method testTransientMergeComponentParent.

@Test
@TestForIssue(jiraKey = "HHH-3868")
public void testTransientMergeComponentParent() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Book b = new Book();
    b.setIsbn(UUID.randomUUID().toString());
    b.setSummary(new Summary());
    b = (Book) s.merge(b);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 45 with Transaction

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

the class EmbeddedTest method testEmbeddableInsideEmbeddable.

// make sure we support collection of embeddable objects inside embeddable objects
@Test
public void testEmbeddableInsideEmbeddable() throws Exception {
    Session s;
    Transaction tx;
    Collection<URLFavorite> urls = new ArrayList<URLFavorite>();
    URLFavorite urlFavorite = new URLFavorite();
    urlFavorite.setUrl("http://highscalability.com/");
    urls.add(urlFavorite);
    urlFavorite = new URLFavorite();
    urlFavorite.setUrl("http://www.jboss.org/");
    urls.add(urlFavorite);
    urlFavorite = new URLFavorite();
    urlFavorite.setUrl("http://www.hibernate.org/");
    urls.add(urlFavorite);
    urlFavorite = new URLFavorite();
    urlFavorite.setUrl("http://www.jgroups.org/");
    urls.add(urlFavorite);
    Collection<String> ideas = new ArrayList<String>();
    ideas.add("lionheart");
    ideas.add("xforms");
    ideas.add("dynamic content");
    ideas.add("http");
    InternetFavorites internetFavorites = new InternetFavorites();
    internetFavorites.setLinks(urls);
    internetFavorites.setIdeas(ideas);
    FavoriteThings favoriteThings = new FavoriteThings();
    favoriteThings.setWeb(internetFavorites);
    s = openSession();
    tx = s.beginTransaction();
    s.persist(favoriteThings);
    tx.commit();
    tx = s.beginTransaction();
    s.flush();
    favoriteThings = (FavoriteThings) s.get(FavoriteThings.class, favoriteThings.getId());
    assertTrue("has web", favoriteThings.getWeb() != null);
    assertTrue("has ideas", favoriteThings.getWeb().getIdeas() != null);
    assertTrue("has favorite idea 'http'", favoriteThings.getWeb().getIdeas().contains("http"));
    assertTrue("has favorite idea 'http'", favoriteThings.getWeb().getIdeas().contains("dynamic content"));
    urls = favoriteThings.getWeb().getLinks();
    assertTrue("has urls", urls != null);
    URLFavorite[] favs = new URLFavorite[4];
    urls.toArray(favs);
    assertTrue("has http://www.hibernate.org url favorite link", "http://www.hibernate.org/".equals(favs[0].getUrl()) || "http://www.hibernate.org/".equals(favs[1].getUrl()) || "http://www.hibernate.org/".equals(favs[2].getUrl()) || "http://www.hibernate.org/".equals(favs[3].getUrl()));
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) 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