Search in sources :

Example 1 with SessionImplementor

use of org.hibernate.engine.SessionImplementor in project jbosstools-hibernate by jbosstools.

the class ClassMetadataFacadeTest method testGetIdentifier.

@Test
public void testGetIdentifier() {
    ClassLoader cl = FACADE_FACTORY.getClassLoader();
    final SessionImplementor sessionTarget = (SessionImplementor) Proxy.newProxyInstance(cl, new Class[] { SessionImplementor.class }, new TestInvocationHandler());
    ISession session = (ISession) Proxy.newProxyInstance(cl, new Class[] { ISession.class, IFacade.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return sessionTarget;
        }
    });
    Object object = Integer.MAX_VALUE;
    Assert.assertSame(object, classMetadata.getIdentifier(object, session));
    Assert.assertEquals("getIdentifier", methodName);
    Assert.assertArrayEquals(new Object[] { object, sessionTarget }, arguments);
}
Also used : ISession(org.jboss.tools.hibernate.runtime.spi.ISession) SessionImplementor(org.hibernate.engine.SessionImplementor) RootClass(org.hibernate.mapping.RootClass) PersistentClass(org.hibernate.mapping.PersistentClass) Method(java.lang.reflect.Method) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 2 with SessionImplementor

use of org.hibernate.engine.SessionImplementor in project Gemma by PavlidisLab.

the class AbstractPersister method isTransient.

@Override
@Transactional
public boolean isTransient(Object entity) {
    if (entity == null)
        return true;
    Long id = EntityUtils.getId(entity);
    if (id == null)
        // assume.
        return true;
    if (EntityUtils.isProxy(entity)) {
        if (AbstractPersister.log.isDebugEnabled())
            AbstractPersister.log.debug("Object is a proxy: " + entity.getClass().getSimpleName() + ":" + id);
        return false;
    }
    org.hibernate.Session session = this.getSessionFactory().getCurrentSession();
    if (session.contains(entity)) {
        if (AbstractPersister.log.isDebugEnabled())
            AbstractPersister.log.debug("Found object in session: " + entity.getClass().getSimpleName() + ":" + id);
        return false;
    }
    // noinspection SynchronizationOnLocalVariableOrMethodParameter // Getting desperate ...
    synchronized (entity) {
        Session sess = this.getSessionFactory().openSession();
        sess.setFlushMode(FlushMode.MANUAL);
        Object pe = sess.get(entity.getClass(), id);
        sess.close();
        if (pe != null) {
            // Common case.
            if (AbstractPersister.log.isDebugEnabled())
                AbstractPersister.log.debug("Found object in store: " + entity.getClass().getSimpleName() + ":" + id);
            return false;
        }
    }
    /*
         * Hibernate has a method that, pretty much, does what we've done so far ... but probably does it better.
         */
    String bestGuessEntityName = ((SessionImplementor) session).bestGuessEntityName(entity);
    if (ForeignKeys.isNotTransient(bestGuessEntityName, entity, null, (SessionImplementor) session)) {
        AbstractPersister.log.info("Hibernate says object is not transient: " + bestGuessEntityName + ":" + id);
        return false;
    }
    /*
         * The ID is filled in, but it probably is a survivor of a rolled-back transaction. It doesn't matter what we
         * return, it's not guaranteed to be right.
         */
    AbstractPersister.log.info("Object has ID but we can't tell if it is persistent: " + entity.getClass().getSimpleName() + ":" + id);
    return true;
}
Also used : SessionImplementor(org.hibernate.engine.SessionImplementor) Session(org.hibernate.classic.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with SessionImplementor

use of org.hibernate.engine.SessionImplementor in project ma-core-public by infiniteautomation.

the class H3PropertyDescriptorProperty method getValue.

/* (non-Javadoc)
     * @see org.directwebremoting.impl.PropertyDescriptorProperty#getValue(java.lang.Object)
     */
public Object getValue(Object bean) throws MarshallException {
    if (!(bean instanceof HibernateProxy)) {
        // This is not a hibernate dynamic proxy, just use it
        return super.getValue(bean);
    } else {
        // If the property is already initialized, use it
        boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
        if (initialized) {
            // This might be a lazy-collection so we need to double check
            Object reply = super.getValue(bean);
            initialized = Hibernate.isInitialized(reply);
        }
        if (initialized) {
            return super.getValue(bean);
        } else {
            // If the session bound to the property is live, use it
            HibernateProxy proxy = (HibernateProxy) bean;
            LazyInitializer initializer = proxy.getHibernateLazyInitializer();
            SessionImplementor implementor = initializer.getSession();
            if (implementor.isOpen()) {
                return super.getValue(bean);
            }
            // So the property needs database access, and the session is closed
            // We'll need to try get another session
            ServletContext context = WebContextFactory.get().getServletContext();
            Session session = H3SessionAjaxFilter.getCurrentSession(context);
            if (session != null) {
                session.update(bean);
                return super.getValue(bean);
            }
            return null;
        }
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) SessionImplementor(org.hibernate.engine.SessionImplementor) ServletContext(javax.servlet.ServletContext) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session)

Example 4 with SessionImplementor

use of org.hibernate.engine.SessionImplementor in project jbosstools-hibernate by jbosstools.

the class ClassMetadataFacadeTest method testGetIdentifier.

@Test
public void testGetIdentifier() {
    ClassLoader cl = FACADE_FACTORY.getClassLoader();
    final SessionImplementor sessionTarget = (SessionImplementor) Proxy.newProxyInstance(cl, new Class[] { SessionImplementor.class }, new TestInvocationHandler());
    ISession session = (ISession) Proxy.newProxyInstance(cl, new Class[] { ISession.class, IFacade.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return sessionTarget;
        }
    });
    Object object = Integer.MAX_VALUE;
    Assert.assertSame(object, classMetadata.getIdentifier(object, session));
    Assert.assertEquals("getIdentifier", methodName);
    Assert.assertArrayEquals(new Object[] { object, sessionTarget }, arguments);
}
Also used : ISession(org.jboss.tools.hibernate.runtime.spi.ISession) SessionImplementor(org.hibernate.engine.SessionImplementor) RootClass(org.hibernate.mapping.RootClass) PersistentClass(org.hibernate.mapping.PersistentClass) Method(java.lang.reflect.Method) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 5 with SessionImplementor

use of org.hibernate.engine.SessionImplementor in project ma-core-public by infiniteautomation.

the class H3BeanConverter method getClass.

/**
 * Hibernate makes {@link Class#getClass()} diffficult ...
 *
 * @param example
 *            The class that we want to call {@link Class#getClass()} on
 * @return The type of the given object
 */
public Class getClass(Object example) {
    if (example instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) example;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        SessionImplementor implementor = initializer.getSession();
        if (initializer.isUninitialized()) {
            try {
                // getImplementation is going to want to talk to a session
                if (implementor.isClosed()) {
                    // Give up and return example.getClass();
                    return example.getClass();
                }
            } catch (NoSuchMethodError ex) {
            // We must be using Hibernate 3.0/3.1 which doesn't have
            // this method
            }
        }
        return initializer.getImplementation().getClass();
    } else {
        return example.getClass();
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) SessionImplementor(org.hibernate.engine.SessionImplementor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Aggregations

SessionImplementor (org.hibernate.engine.SessionImplementor)5 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 RootClass (org.hibernate.mapping.RootClass)2 HibernateProxy (org.hibernate.proxy.HibernateProxy)2 LazyInitializer (org.hibernate.proxy.LazyInitializer)2 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)2 ISession (org.jboss.tools.hibernate.runtime.spi.ISession)2 Test (org.junit.Test)2 ServletContext (javax.servlet.ServletContext)1 Session (org.hibernate.Session)1 Session (org.hibernate.classic.Session)1 Transactional (org.springframework.transaction.annotation.Transactional)1