Example usage for org.hibernate Session getEntityName

List of usage examples for org.hibernate Session getEntityName

Introduction

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

Prototype

String getEntityName(Object object);

Source Link

Document

Return the entity name for a persistent entity.

Usage

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

License:Open Source License

/**
 * ? ? ?  ?  ??//ww w.ja  v  a 2  s.c  o m
 *
 * @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:fr.keyconsulting.oliphant.test.unitTests.java

License:Open Source License

public void update(Class theClass, boolean magic, boolean stale, boolean cached) {
    long id = 0;/*  ww  w  . j  av a  2s .c o m*/
    SessionFactory factory = magic ? magicSessionFactory : sessionFactory;

    Session session = factory.getCurrentSession();

    System.out.println("Hibernate: starting transaction");
    Transaction tx = session.beginTransaction();

    if (cached) {
        System.out.println("Hibernate: pre-loading object " + id);
        Object o = theClass.cast(session.load(theClass, id));
    } else {
        factory.evict(theClass);
    }
    System.out.println("Hibernate: loading " + theClass + " " + id);
    PersistentVersionedObject o = (PersistentVersionedObject) session.load(theClass, id);
    o.setChampString("" + System.currentTimeMillis());

    if (stale) {
        try {
            System.out.println("Updating " + theClass + " " + id + " outside Hibernate");
            Statement st = conn.createStatement();
            String entityName = session.getEntityName(o);
            PersistentClass c = config.getClassMapping(entityName);
            st.executeUpdate("UPDATE " + c.getTable().getName() + " SET version=version+1 WHERE id=" + id);
            st.close();
        } catch (SQLException e) {
            fail();
            System.out.println(e);
            System.out.println("  in " + e.getStackTrace()[0]);
        }
    }
    try {
        session.persist(o);
    } catch (StaleObjectStateException e) {
        assertTrue(magic && stale);
        return;
    }
    assertFalse(stale && magic); // We should have had an exception
    try {
        tx.commit();
    } catch (StaleObjectStateException e) {
        assertTrue(stale);
        return;
    }
    assertFalse(stale); // We should have had an exception
}

From source file:org.apache.tapestry5.internal.hibernate.EntityPersistentFieldStrategyTest.java

License:Apache License

public void not_an_entity() {
    String nonEntity = "foo";
    Session session = newMock(Session.class);
    EntityPersistentFieldStrategy strategy = new EntityPersistentFieldStrategy(session, null);

    expect(session.contains(nonEntity)).andReturn(true);
    expect(session.getEntityName(nonEntity)).andThrow(new HibernateException("error"));

    replay();//from  w  w  w.  j av  a2 s  . co m

    try {
        strategy.postChange("pageName", "", "fieldName", nonEntity);

        unreachable();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "Failed persisting an entity in the session. entity: foo");
    }

    verify();
}

From source file:org.apache.tapestry5.internal.hibernate.EntityPersistentFieldStrategyTest.java

License:Apache License

public void persistent_entity() {
    SampleEntity entity = new SampleEntity();
    SampleEntity restoredEntity = new SampleEntity();
    Session session = newMock(Session.class);
    EntityPersistentFieldStrategy strategy = new EntityPersistentFieldStrategy(session, null);

    expect(session.contains(entity)).andReturn(true);
    expect(session.getEntityName(entity)).andReturn("SampleEntity");
    expect(session.getIdentifier(entity)).andReturn(42);

    expect(session.get("SampleEntity", 42)).andReturn(restoredEntity);

    replay();//  ww w  .  jav a 2s.  c o m

    Object persisted = strategy.convertApplicationValueToPersisted(entity);

    assert persisted instanceof SessionRestorable;

    Object restored = strategy.convertPersistedToApplicationValue(persisted);

    assertSame(restored, restoredEntity);

    verify();

}

From source file:org.eclipse.emf.teneo.hibernate.resource.HibernateResource.java

License:Open Source License

/**
 * Override point, the default implementation will check if Hibernate
 * already assigned an id to an eobject. If so it is restored back to null.
 * See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=331953
 *//*from w w  w .  ja v  a2  s  . c o m*/
protected void beforeSaveRollback() {
    // assigned ids
    Session session = sessionWrapper.getHibernateSession();
    for (EObject eobject : super.getNewEObjects()) {
        String entityName = session.getEntityName(eobject);
        String identifierName = getIdentifierName(eobject, session);
        Serializable id = session.getIdentifier(eobject);
        Criteria exists = session.createCriteria(entityName).add(Restrictions.eq(identifierName, id));
        if (exists.uniqueResult() == null) {
            rollbackID(eobject, identifierName);
        }
    }
}

From source file:org.eclipse.emf.teneo.hibernate.resource.HibernateResource.java

License:Open Source License

protected String getIdentifierName(EObject eobject, Session hs) {
    String entityName = hs.getEntityName(eobject);
    if (entityName == null) {
        return null;
    }//from w  ww .j  a  v  a  2  s  .c o m
    ClassMetadata entityMetaData = hs.getSessionFactory().getClassMetadata(entityName);
    if (entityMetaData == null) {
        return null;
    }
    String identifierName = entityMetaData.getIdentifierPropertyName();
    return identifierName;
}

From source file:org.jboss.tools.hibernate3_5.console.EntityPropertySource.java

License:Open Source License

public EntityPropertySource(final Object object, final Session currentSession, HibernateExtension extension) {
    this.currentSession = currentSession;
    this.extension = extension;
    reflectedObject = object;/*  w  ww  . ja  v a 2  s  .c om*/
    if (currentSession.isOpen()) {
        classMetadata = currentSession.getSessionFactory()
                .getClassMetadata(currentSession.getEntityName(reflectedObject));
    } else {
        classMetadata = currentSession.getSessionFactory()
                .getClassMetadata(HibernateProxyHelper.getClassWithoutInitializingProxy(reflectedObject));
    }

}

From source file:test.hibernate.FunctionalTestCase.java

License:Open Source License

protected void assertAllDataRemoved() {
    if (!createSchema()) {
        return; // no tables were created...
    }//w  w  w.j  a  va2  s.c  o  m
    if (!Boolean.getBoolean("hibernate.test.validateDataCleanup")) {
        return;
    }

    Session tmpSession = getSessions().openSession();
    try {
        List list = tmpSession.createQuery("select o from java.lang.Object o").list();

        Map items = new HashMap();
        if (!list.isEmpty()) {
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                Object element = iter.next();
                Integer l = (Integer) items.get(tmpSession.getEntityName(element));
                if (l == null) {
                    l = new Integer(0);
                }
                l = new Integer(l.intValue() + 1);
                items.put(tmpSession.getEntityName(element), l);
                System.out.println("Data left: " + element);
            }
            fail("Data is left in the database: " + items.toString());
        }
    } finally {
        try {
            tmpSession.close();
        } catch (Throwable t) {
            // intentionally empty
        }
    }
}