-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Entity not managed error with EntityManager.refresh (3.2.1)
PostPosted: Thu Mar 01, 2007 7:25 pm 
Newbie

Joined: Thu Mar 01, 2007 6:58 pm
Posts: 9
I am having a problem where calling refresh() throws an java.lang.IllegalArgumentException: Entity not managed, but replacing it with merge() in the same code works. I am not sure if this is a bug or if it has something to do with how the code is set up. I am calling refresh through a template style anonymous innner class that accepts the entity as a final class. Here is a code excerpt

Code:
    public <T> void refresh(final T entity) {
        log.tracef("Refreshing entity %s", entity);
        executeQuery(new IHibernateQuery<T>() {
            public T execute(EntityManager entityManager) {
                entityManager.refresh(entity);
                return null;
            }
        });
    }

    public <T> T merge(final T entity) {
        log.tracef("Merging entity %s", entity);
        return executeQuery(new IHibernateQuery<T>() {
            public T execute(EntityManager entityManager) {
                return entityManager.merge(entity);
            }
        });
    }


This code gets executed in this block

Code:
    public <T> T executeQuery(IHibernateQuery<T> query) {
        EntityManager em;
        T results;

        //get the entity manager
        em = getEntityManager();
        try {
            log.trace("Joining transaction");
            em.joinTransaction();
            log.trace("Executing query");
            results = query.execute(em);
            log.trace("Have results, committing");
            em.commitTransaction();
            return results;
        } catch (RuntimeException runtimeEx) {
            em.rollbackActiveTransaction();
            throw runtimeEx;
        } catch (Error error) {
            em.rollbackActiveTransaction();
            throw error;
        } finally {
            if (em.isOpen()) {
                log.trace("Closing entity manager");
                em.close();
            }
        }
    }


Here is the relevant stack trace

Code:
java.lang.IllegalArgumentException: Entity not managed
   at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:260)


The code throwing the exception is this

Code:
AbstractEntityManagerImpl...

public void refresh(Object entity) {
    checkTransactionNeeded();
    //adjustFlushMode();
    try {
259:        if ( ! getSession().contains( entity ) ) {
260:            throw new IllegalArgumentException( "Entity not managed" );
        }
        getSession().refresh( entity );
    }
    catch (MappingException e) {
        throw new IllegalArgumentException( e.getMessage(), e );
    }
    catch (HibernateException he) {
        throwPersistenceException( he );
    }
}



Based on this code, it doesn't look like refresh is hitting the database at all.

Log excerpt
Code:

[15:15:04.555] [TRACE] [AbstractDataAccessor] - Joining transaction
[15:15:04.555] [DEBUG] [SessionImpl] - opened session at timestamp: 4803751545057284
[15:15:04.555] [DEBUG] [JDBCTransaction] - begin
[15:15:04.555] [DEBUG] [ConnectionManager] - opening JDBC connection
[15:15:04.555] [DEBUG] [JDBCTransaction] - current autocommit status: true
[15:15:04.555] [DEBUG] [JDBCTransaction] - disabling autocommit
[15:15:04.555] [DEBUG] [JDBCContext] - after transaction begin
[15:15:04.555] [TRACE] [AbstractDataAccessor] - Executing business transaction
[15:15:04.555] [TRACE] [MemberManager] - Refreshing <MemberType id="124" name="Test Type" displayName="Test Type" description="The test member type"/>
[15:15:04.570] [TRACE] [AbstractDataAccessor] - Refreshing entity <MemberType id="124" name="Test Type" displayName="Test Type" description="The test member type"/>
[15:15:04.570] [TRACE] [AbstractDataAccessor] - Joining transaction
[15:15:04.570] [TRACE] [AbstractDataAccessor] - Executing query
[15:15:04.570] [DEBUG] [JDBCTransaction] - rollback
[15:15:04.570] [DEBUG] [JDBCTransaction] - re-enabling autocommit
[15:15:04.570] [DEBUG] [JDBCTransaction] - rolled back JDBC Connection
[15:15:04.586] [DEBUG] [JDBCContext] - after transaction completion
[15:15:04.586] [DEBUG] [ConnectionManager] - aggressively releasing JDBC connection
[15:15:04.586] [DEBUG] [ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
[15:15:04.586] [DEBUG] [SessionImpl] - after transaction completion
[15:15:04.586] [TRACE] [AbstractDataAccessor] - Closing entity manager
[15:15:04.586] [TRACE] [AbstractDataAccessor] - Closing entity manager
[15:15:04.586] [ERROR] [MemberManagerTest] - Error occurred inserting attribute
java.lang.IllegalArgumentException: Entity not managed
   at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:260)



Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 3:36 pm 
Newbie

Joined: Thu Mar 01, 2007 6:58 pm
Posts: 9
This works if I use ((Session)getDelegate()).refresh() without changing any of my code. If you look at the session refresh method, it just fires the event and doesn't check to see if the entity is managed (which is why I am refreshing because it is not). I am pretty sure this is a bug. Read the description of refresh.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.