Example usage for org.hibernate Session getIdentifier

List of usage examples for org.hibernate Session getIdentifier

Introduction

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

Prototype

Serializable getIdentifier(Object object);

Source Link

Document

Return the identifier value of the given entity as associated with this session.

Usage

From source file:com.autobizlogic.abl.hibernate.HibernateUtil.java

License:Open Source License

/**
 * Given a persistent bean, get (if available) the values originally loaded from the database
 * when the object was read.//w ww .  j ava 2 s.  c om
 * @param bean The persistent bean (must be Pojo)
 * @param session The current Hibernate session
 * @return Null if the object is not known by Hibernate, otherwise an ObjectState with the original
 * values.
 */
public static PersistentBean getOldStateForObject(Object bean, Session session) {

    if (!session.contains(bean))
        return null;

    Serializable pk = session.getIdentifier(bean);

    Class<?> beanClass = getEntityClassForBean(bean);
    String beanClassName = beanClass.getName();
    EntityPersister persister = HibernateSessionUtil.getEntityPersister(session, beanClassName, bean);
    Object[] cachedValues = persister.getDatabaseSnapshot(pk, (SessionImplementor) session);

    return HibPersistentBeanFactory.createPersistentBeanCopyFromState(cachedValues, bean, persister, session);
}

From source file:com.booleanworks.kryptopterus.application.MainHibernateUtil.java

License:Apache License

public Object saveOrUpdate(Object object, Session session) {
    System.out.println("com.booleanworks.kryptopterus.application.MainHibernateUtil.saveOrUpdate()");
    System.out.println("session  => " + session.hashCode());

    Object result = null;/* w w w.  ja  va  2s.co  m*/

    if (session == null || !session.isConnected() || !session.isOpen()) {
        session = this.getResidentSession();
    }

    if (session.isJoinedToTransaction()) {

        session.saveOrUpdate(object);
        if (!(session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK)) {
            session.flush();
        }
        result = session.get(object.getClass(), session.getIdentifier(object));

    } else {
        Transaction transaction = this.beginTransaction(session, false);

        session.saveOrUpdate(object);
        if (!transaction.getRollbackOnly() && session.getFlushMode() != FlushModeType.AUTO) {
            session.flush();
        }
        result = session.get(object.getClass(), session.getIdentifier(object));

        this.commitTransaction(session, transaction);
    }

    return result;
}

From source file:com.booleanworks.kryptopterus.application.MainHibernateUtilTest.java

License:Apache License

@Test
public void basicFunctionalityCheck() {
    MainHibernateUtil mhu = MainHibernateUtil.getInstance();
    //Checks in we obtain a mhu
    assertNotNull(mhu);//from  ww  w. ja  v  a2s .c  o  m

    AppActivity aa1 = new AppActivity();
    //New entity as no id
    assertNull(aa1.getId());

    aa1.setDisplayName("basicFunctionalityCheck");
    aa1.setBusinessIdentifier("basicFunctionalityCheck");

    //
    assertNotNull(aa1.getRelationsAsFirstActivity());
    assertEquals(aa1.getRelationsAsFirstActivity().size(), 0);

    Session s1 = mhu.getNewSession();
    //Got a session
    assertNotNull(s1);

    Session s2 = mhu.getNewSession();
    //Got a second session
    assertNotNull(s2);

    assertNotEquals(s1, s2);
    assertTrue(s1.isOpen());

    //With no ongoing transaction
    assertFalse(s1.isJoinedToTransaction());

    Transaction t1 = mhu.beginTransaction(s1);

    //Now, we have a transaction
    assertTrue(s1.isJoinedToTransaction());

    //which dont' contain our transient entity
    assertFalse(s1.contains(aa1));
    //assertNull(s1.getIdentifier(aa1));

    //Using saveOrUpdate
    mhu.saveOrUpdate(aa1, s1);

    //We have an id
    assertNotNull(aa1.getId());

    //The session knowns the entity
    assertTrue(s1.contains(aa1));

    //and its identifier is known as well
    assertNotNull(s1.getIdentifier(aa1));

    Long id1 = (Long) s1.getIdentifier(aa1);
    aa1 = s1.get(aa1.getClass(), aa1.getId());

    //If we get this object, we assume that is OK
    assertNotNull(aa1);

    //
    assertNotNull(aa1.getRelationsAsFirstActivity());
    assertEquals(aa1.getRelationsAsFirstActivity().size(), 0);

    mhu.saveOrUpdate(aa1, s1);

    AppActivityRelation aar1 = new AppActivityRelation();
    aar1.setDisplayName("basicFunctionalityCheck");
    aa1.getRelationsAsFirstActivity().add(aar1);
    aa1.getRelationsAsSecondActivity().add(aar1);
    aar1.setFirstActivity(aa1);
    aar1.setSecondActivity(aa1);

    mhu.saveOrUpdate(aa1, s1);
    mhu.saveOrUpdate(aar1, s1);

    //
    assertNotNull(aa1.getRelationsAsFirstActivity());
    assertEquals(aa1.getRelationsAsFirstActivity().size(), 1);

    mhu.commitTransaction(s1, t1);
    Transaction t2 = mhu.beginTransaction(s1);

    aa1 = (AppActivity) mhu.saveOrUpdate(aa1, s1);
    aar1 = (AppActivityRelation) mhu.saveOrUpdate(aar1, s1);

    //
    assertNotNull(aa1.getRelationsAsFirstActivity());
    assertEquals(aa1.getRelationsAsFirstActivity().size(), 1);

    mhu.commitTransaction(s1, t2);
    mhu.closeSession(s1);

    aa1 = null;

    Session s3 = mhu.getNewSession();
    Transaction t3 = mhu.beginTransaction(s3);

    AppActivity aa2 = s3.get(AppActivity.class, id1);

    //
    assertNotNull(aa2.getRelationsAsFirstActivity());
    assertEquals(aa2.getRelationsAsFirstActivity().size(), 1);

    mhu.commitTransaction(s3, t3);
    //assertFalse(s1.isJoinedToTransaction());
    //assertFalse(s1.isReadOnly(aa1));

}

From source file:com.db4o.drs.hibernate.impl.ObjectLifeCycleEventsListenerImpl.java

License:Open Source License

private long getId(Object obj) {
    Session session = getSession();
    return (Long) session.getIdentifier(obj);
}

From source file:com.db4o.drs.hibernate.impl.Util.java

License:Open Source License

public static Uuid getUuid(Session session, Object obj) {
    long id = Util.castAsLong(session.getIdentifier(obj));

    ObjectReference of = getObjectReferenceById(session, obj.getClass().getName(), id);
    if (of == null)
        return null;
    return of.getUuid();
}

From source file:com.itseekers.sistemaplandeproduccion.dao.impl.UsuarioDaoImpl.java

@Transactional
public int insertUsuario(UsuarioDto usuarioDto) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.saveOrUpdate(usuarioDto);//from  ww w .  j  av  a 2 s  .  c  om
    tx.commit();
    Serializable id = session.getIdentifier(usuarioDto);
    session.close();
    return (Integer) id;
}

From source file:com.itseekers.sistemaplandeproduccion.dao.impl.UsuarioDaoImpl.java

public int updateUsuarioDto(UsuarioDto usuarioDto) {
    Serializable id = null;/*w  ww .  j av a  2 s  . c  o  m*/
    try {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.update(usuarioDto);
        tx.commit();
        id = session.getIdentifier(usuarioDto);
        session.close();
    } catch (Exception e) {
        logger.error("JAJAJAJAJA >>" + e);
    }
    return (Integer) id;
}

From source file:com.itseekers.sistemaplandeproduccion.dao.impl.UsuarioDaoImpl.java

public int deleteUsuarioDtoById(int id) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    UsuarioDto usuarioDto = (UsuarioDto) session.load(UsuarioDto.class, id);
    session.delete(usuarioDto);/*  w  w w  . j a  v  a  2  s . c  o m*/
    tx.commit();
    Serializable ids = session.getIdentifier(usuarioDto);
    session.close();
    return (Integer) ids;
}

From source file:com.mg.framework.support.report.ReportUtils.java

License:Open Source License

/**
 * ? ? ?  ?  ??/*w w w .  ja va 2s.  com*/
 *
 * @param entity ??
 * @return ? ?
 */
public static String createEntityParam(Object entity) {
    if (entity == null)
        return null;
    // ??  ?,       
    //${entity:<entity name>#<identificator value>}
    Session session = (Session) ServerUtils.getPersistentManager().getDelegate();
    session.lock(entity, LockMode.NONE);//reassociate a transient instance with a session
    return createEntityParam(session.getEntityName(entity), session.getIdentifier(entity));
}

From source file:corner.service.EntityService.java

License:Apache License

/**
 * ?// w w  w .  j  a  v a  2  s  . c o m
 * 
 * @param object
 *            ?
 * @return 
 * @since 2.0.3
 */
public Serializable getIdentifier(final Object object) {
    return (Serializable) ((HibernateObjectRelativeUtils) getObjectRelativeUtils()).getHibernateTemplate()
            .execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    return session.getIdentifier(object);

                }
            });

}