Example usage for org.hibernate Session load

List of usage examples for org.hibernate Session load

Introduction

In this page you can find the example usage for org.hibernate Session load.

Prototype

void load(Object object, Serializable id);

Source Link

Document

Read the persistent state associated with the given identifier into the given transient instance.

Usage

From source file:com.gianburga.servicios.model.TecnicoDAOImpl.java

@Override
public Tecnico get(int id) {
    Session session = mySessionFactory.getCurrentSession();
    session.beginTransaction();/*from www . ja v a 2 s  .  co m*/
    return (Tecnico) session.load(Tecnico.class, id);
}

From source file:com.github.snd297.hibernatecollections.PublicCollectionTest.java

License:Apache License

@Test
public void fixedCollection() throws Exception {
    Session sess = null;
    Transaction trx = null;//from  w w w  .  ja v  a  2  s  .  c  om
    Long newWheel0Id = null;
    Long newWheel1Id = null;
    try {
        sess = HibernateUtil.getSessionFactory().openSession();

        trx = sess.beginTransaction();

        Bicycle bicycle = (Bicycle) sess.load(Bicycle.class, bicycleId);
        Set<Wheel> wheels = bicycle.getWheels();

        assertTrue(wheels instanceof PersistentCollection);

        Wheel newWheel0 = new Wheel(bicycle);
        Wheel newWheel1 = new Wheel(bicycle);

        sess.save(newWheel0);
        sess.save(newWheel1);

        Set<Wheel> newWheels = newHashSet(newWheel0, newWheel1);

        bicycle.getWheels().clear();
        bicycle.getWheels().addAll(newWheels);
        trx.commit();

        newWheel0Id = newWheel0.getId();
        newWheel1Id = newWheel1.getId();

    } catch (Exception e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }

    try {
        sess = HibernateUtil.getSessionFactory().openSession();

        trx = sess.beginTransaction();

        Bicycle bicycle = (Bicycle) sess.load(Bicycle.class, bicycleId);
        Set<Wheel> wheels = bicycle.getWheels();

        assertEquals(2, wheels.size());

        assertNotNull(find(wheels, compose(equalTo(newWheel0Id), IHasLongId.getId), null));

        assertNotNull(find(wheels, compose(equalTo(newWheel1Id), IHasLongId.getId), null));

        trx.commit();
    } catch (Exception e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }
}

From source file:com.github.snd297.yp.finalentities.persistence.FinalEntitiesTest.java

License:Apache License

@Test
public void finalNoProxy() throws Exception {
    SessionFactory sessFac = HibernateUtil.getSessionFactory();
    Session sess = null;
    Transaction trx = null;// w  w  w.  j a  v  a2s  .c  o  m
    try {
        sess = sessFac.openSession();
        trx = sess.beginTransaction();

        SpokeInFinalWheel spoke = (SpokeInFinalWheel) sess.load(SpokeInFinalWheel.class, finalSpokeId);

        FinalWheel wheel = spoke.getWheel();

        assertFalse((Object) wheel instanceof HibernateProxy);

        trx.commit();
    } catch (Exception e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }
}

From source file:com.github.snd297.yp.finalentities.persistence.FinalEntitiesTest.java

License:Apache License

@Test
public void proxy() throws Exception {
    SessionFactory sessFac = HibernateUtil.getSessionFactory();
    Session sess = null;
    Transaction trx = null;//from   w w w .ja  v  a2  s  . c  o m
    try {
        sess = sessFac.openSession();
        trx = sess.beginTransaction();

        Spoke spoke = (Spoke) sess.load(Spoke.class, spokeId);

        Wheel wheel = spoke.getWheel();

        assertTrue(wheel instanceof HibernateProxy);
        assertFalse(Hibernate.isInitialized(wheel));

        trx.commit();
    } catch (Exception e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }
}

From source file:com.github.snd297.yp.hibernatecollections.persistence.PublicCollectionTest.java

License:Apache License

@Test(expected = HibernateException.class)
public void orphanedCollection() {
    Session sess = null;
    Transaction trx = null;//from  www.  j a v  a2  s  . c om
    try {
        sess = HibernateUtil.getSessionFactory().openSession();

        trx = sess.beginTransaction();

        BadBicycle bicycle = (BadBicycle) sess.load(BadBicycle.class, badBicycleId);

        Set<WheelInBadBicycle> wheels = bicycle.getWheels();

        assertEquals(2, bicycle.getWheels().size());
        assertTrue(wheels instanceof PersistentCollection);

        WheelInBadBicycle newWheel0 = new WheelInBadBicycle(bicycle);
        WheelInBadBicycle newWheel1 = new WheelInBadBicycle(bicycle);

        sess.save(newWheel0);
        sess.save(newWheel1);

        Set<WheelInBadBicycle> newWheels = newHashSet(newWheel0, newWheel1);

        bicycle.setWheels(newWheels);
        sess.flush();
        try {
            trx.commit();
        } catch (HibernateException he) {
            assertTrue(he.getMessage().equals(
                    "A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: com.github.snd297.yp.hibernatecollections.model.BadBicycle.wheels"));
            throw he;
        }
    } catch (RuntimeException e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }
}

From source file:com.github.snd297.yp.hibernatecollections.persistence.PublicCollectionTest.java

License:Apache License

@Test
public void fixedCollection() {
    Session sess = null;
    Transaction trx = null;// www. j ava 2s . c o m
    Long newWheel0Id = null;
    Long newWheel1Id = null;
    try {
        sess = HibernateUtil.getSessionFactory().openSession();

        trx = sess.beginTransaction();

        Bicycle bicycle = (Bicycle) sess.load(Bicycle.class, bicycleId);
        Set<Wheel> wheels = bicycle.getWheels();

        assertEquals(2, bicycle.getWheels().size());
        assertTrue(wheels instanceof PersistentCollection);

        Wheel newWheel0 = new Wheel(bicycle);
        Wheel newWheel1 = new Wheel(bicycle);

        sess.save(newWheel0);
        sess.save(newWheel1);

        Set<Wheel> newWheels = newHashSet(newWheel0, newWheel1);

        bicycle.getWheels().clear();
        bicycle.getWheels().addAll(newWheels);
        trx.commit();

        newWheel0Id = newWheel0.getId();
        newWheel1Id = newWheel1.getId();

    } catch (RuntimeException e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }

    try {
        sess = HibernateUtil.getSessionFactory().openSession();

        trx = sess.beginTransaction();

        Bicycle bicycle = (Bicycle) sess.load(Bicycle.class, bicycleId);
        Set<Wheel> wheels = bicycle.getWheels();

        assertEquals(2, wheels.size());

        assertNotNull(find(wheels, compose(equalTo(newWheel0Id), IHasLongId.getId), null));

        assertNotNull(find(wheels, compose(equalTo(newWheel1Id), IHasLongId.getId), null));

        trx.commit();
    } catch (RuntimeException e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }
}

From source file:com.github.snd297.yp.hibernatecollections.persistence.PublicCollectionTest.java

License:Apache License

@Test(expected = HibernateException.class)
public void bandOwningSide() {
    Session sess = null;
    Transaction trx = null;/*w  w w  .j  a v a 2  s  . c o m*/
    try {
        sess = HibernateUtil.getSessionFactory().openSession();

        trx = sess.beginTransaction();

        BandOwningSide band = (BandOwningSide) sess.load(BandOwningSide.class, bandOwningSideId);

        assertEquals(2, band.getMembers().size());
        assertTrue(band.getMembers() instanceof PersistentCollection);

        BandMember newMember0 = new BandMember();
        sess.save(newMember0);
        BandMember newMember1 = new BandMember();
        sess.save(newMember1);

        Set<BandMember> newMembers = newHashSet(newMember0, newMember1);

        band.setMembers(newMembers);
        try {
            trx.commit();
        } catch (HibernateException he) {
            assertTrue(he.getMessage().equals(
                    "A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: "
                            + BandOwningSide.class.getName() + ".members"));
            throw he;
        }
    } catch (RuntimeException e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }
}

From source file:com.github.snd297.yp.hibernatecollections.persistence.PublicCollectionTest.java

License:Apache License

@Test(expected = HibernateException.class)
public void bandFk() {
    Session sess = null;
    Transaction trx = null;/*w  ww .  j a va 2s. c  o m*/
    try {
        sess = HibernateUtil.getSessionFactory().openSession();

        trx = sess.beginTransaction();

        BandWithForeignKey band = (BandWithForeignKey) sess.load(BandWithForeignKey.class, bandFkId);

        assertEquals(2, band.getMembers().size());
        assertTrue(band.getMembers() instanceof PersistentCollection);

        BandMemberWithForeignKey newMember0 = new BandMemberWithForeignKey();
        sess.save(newMember0);

        BandMemberWithForeignKey newMember1 = new BandMemberWithForeignKey();
        sess.save(newMember1);

        Set<BandMemberWithForeignKey> newMembers = newHashSet(newMember0, newMember1);

        band.setMembers(newMembers);
        try {
            trx.commit();
        } catch (HibernateException he) {
            assertTrue(he.getMessage().equals(
                    "A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: "
                            + BandWithForeignKey.class.getName() + ".members"));
            throw he;
        }
    } catch (RuntimeException e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(sess);
    }
}

From source file:com.github.snd297.yp.proxies.persistence.InstanceofTest.java

License:Apache License

@Test
public void whatToDo() {
    Session session = null;
    Transaction trx = null;/*from  w  w  w .  ja va 2 s.com*/
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        trx = session.beginTransaction();

        Shape squareShape = (Shape) session.load(Shape.class, squareId);
        assertTrue(squareShape instanceof HibernateProxy);
        Hibernate.initialize(squareShape);

        Shape circleShape = (Shape) session.load(Shape.class, circleId);
        assertTrue(circleShape instanceof HibernateProxy);
        Hibernate.initialize(circleShape);

        List<Shape> shapes = newArrayList(squareShape, circleShape);
        boolean foundRectangle = false, foundCircle = false;
        for (Shape shape : shapes) {
            if (HibernateUtil.isInstanceOf(shape, Rectangle.class)) {
                Rectangle squareRectangle = (Rectangle) session.load(Rectangle.class, shape.getId());
                assertTrue(squareRectangle instanceof HibernateProxy);
                Hibernate.initialize(squareRectangle);

                // This is something to be aware of, this is why we get the warning:
                // HHH000179: Narrowing proxy to class
                // com.github.snd297.yp.proxies.model.Square - this operation breaks
                // ==
                assertNotEquals(shape, squareRectangle);
                assertEquals(shape.getId(), squareRectangle.getId());

                foundRectangle = true;
            } else if (HibernateUtil.isInstanceOf(shape, Circle.class)) {
                Circle circle = (Circle) session.load(Circle.class, shape.getId());
                assertTrue(circle instanceof HibernateProxy);
                Hibernate.initialize(circle);

                // This is something to be aware of, this is why we get the warning:
                // HHH000179: Narrowing proxy to class
                // com.github.snd297.yp.proxies.model.Circle - this operation breaks
                // ==
                assertNotEquals(shape, circle);
                assertEquals(shape.getId(), circle.getId());

                foundCircle = true;
            }
        }
        trx.commit();
        assertTrue(foundRectangle);
        assertTrue(foundCircle);
    } catch (RuntimeException e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(session);
    }
}

From source file:com.github.snd297.yp.proxies.persistence.InstanceofTest.java

License:Apache License

@Test
public void instanceofASquare() {
    Session session = null;
    Transaction trx = null;/* w  w w.  j  av a 2s.c  om*/
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        trx = session.beginTransaction();

        Shape gotSquare = (Shape) session.get(Shape.class, squareId);
        assertTrue(gotSquare instanceof Square);
        assertFalse(gotSquare instanceof HibernateProxy);

        Shape loadedSquare = (Shape) session.load(Shape.class, squareId);
        assertSame(loadedSquare, gotSquare);

        trx.commit();
    } catch (RuntimeException e) {
        HibernateUtil.rollbackQuietly(trx);
        throw e;
    } finally {
        HibernateUtil.closeQuietly(session);
    }
}