Search in sources :

Example 1 with Footballer

use of org.hibernate.test.annotations.id.entities.Footballer in project hibernate-orm by hibernate.

the class IdTest method testIdClass.

@Test
public void testIdClass() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Footballer fb = new Footballer("David", "Beckam", "Arsenal");
    GoalKeeper keeper = new GoalKeeper("Fabien", "Bartez", "OM");
    s.persist(fb);
    s.persist(keeper);
    tx.commit();
    s.clear();
    // lookup by id
    tx = s.beginTransaction();
    FootballerPk fpk = new FootballerPk("David", "Beckam");
    fb = (Footballer) s.get(Footballer.class, fpk);
    FootballerPk fpk2 = new FootballerPk("Fabien", "Bartez");
    keeper = (GoalKeeper) s.get(GoalKeeper.class, fpk2);
    assertNotNull(fb);
    assertNotNull(keeper);
    assertEquals("Beckam", fb.getLastname());
    assertEquals("Arsenal", fb.getClub());
    assertEquals(1, s.createQuery("from Footballer f where f.firstname = 'David'").list().size());
    tx.commit();
    // reattach by merge
    tx = s.beginTransaction();
    fb.setClub("Bimbo FC");
    s.merge(fb);
    tx.commit();
    // reattach by saveOrUpdate
    tx = s.beginTransaction();
    fb.setClub("Bimbo FC SA");
    s.saveOrUpdate(fb);
    tx.commit();
    // clean up
    s.clear();
    tx = s.beginTransaction();
    fpk = new FootballerPk("David", "Beckam");
    fb = (Footballer) s.get(Footballer.class, fpk);
    assertEquals("Bimbo FC SA", fb.getClub());
    s.delete(fb);
    s.delete(keeper);
    tx.commit();
    s.close();
}
Also used : Footballer(org.hibernate.test.annotations.id.entities.Footballer) GoalKeeper(org.hibernate.test.annotations.id.entities.GoalKeeper) Transaction(org.hibernate.Transaction) FootballerPk(org.hibernate.test.annotations.id.entities.FootballerPk) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 Footballer (org.hibernate.test.annotations.id.entities.Footballer)1 FootballerPk (org.hibernate.test.annotations.id.entities.FootballerPk)1 GoalKeeper (org.hibernate.test.annotations.id.entities.GoalKeeper)1 Test (org.junit.Test)1