Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 315422

Summary: Enhanced EntityManagerImpl.unwrap to include additional session types
Product: z_Archived Reporter: Doug Clarke <douglas.clarke>
Component: EclipselinkAssignee: Doug Clarke <douglas.clarke>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: andrei.ilitchev, gordon.yorke, peter.krogh
Version: unspecifiedFlags: gordon.yorke: review+
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on:    
Bug Blocks: 244124    

Description Doug Clarke CLA 2010-06-02 12:41:06 EDT
The unwrap method should be enhanced to support some additional internal session types to expand the usability of this method in combination with native API calls.

these sessions include:

- UnitOfWorkImpl

- ServerSession
- Server (interface)
- AbstractSession

The proposed method would look like:

    public <T> T unwrap(Class<T> cls) {
        try {
            if (cls.equals(UnitOfWork.class) || cls.equals(UnitOfWorkImpl.class)) {
                return (T) this.getUnitOfWork();
            } else if (cls.equals(JpaEntityManager.class)) {
                return (T) this;
            } else if (cls.equals(Session.class) || cls.equals(Server.class) || cls.equals(ServerSession.class) || cls.equals(AbstractSession.class)) {
                return (T) this.getServerSession();
            } else if (cls.equals(java.sql.Connection.class)) {
                UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) this.getUnitOfWork();
                if (!unitOfWork.isInTransaction()) {
                    unitOfWork.beginEarlyTransaction();
                    return (T) unitOfWork.getAccessor().getConnection();
                }
            }
            throw new PersistenceException(ExceptionLocalization.buildMessage("Provider-does-not-support-the-call", null));

        } catch (RuntimeException e) {
            throw e;
        }
    }
Comment 1 Doug Clarke CLA 2010-06-02 12:43:48 EDT
I am making this a blocker for 244124 (nested fetchgroup & LoadGroup) work since the suggested JpaHelper.load method will use this unwrap method to access the requires AbstractSession.
Comment 2 Andrei Ilitchev CLA 2010-06-03 11:00:36 EDT
    public <T> T unwrap(Class<T> cls) {
        try {
            if (cls.equals(UnitOfWork.class) || cls.equals(UnitOfWorkImpl.class) || cls.equals(RepeatableWriteUnitOfWork.class)) {
                return (T) this.getUnitOfWork();
            } else if (cls.equals(JpaEntityManager.class)) {
                return (T) this;
            } else if (cls.equals(Session.class) || cls.equals(Server.class) || cls.equals(ServerSession.class) || cls.equals(AbstractSession.class)) {            
                return (T) this.getServerSession();
            } else if (cls.equals(java.sql.Connection.class)) {
                UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) this.getUnitOfWork();
                if (!unitOfWork.isInTransaction()) {
                    unitOfWork.beginEarlyTransaction();
                    return (T) unitOfWork.getAccessor().getConnection();
                }
            }
            throw new PersistenceException(ExceptionLocalization.buildMessage("Provider-does-not-support-the-call", null));

        } catch (RuntimeException e) {
            throw e;
        }
    }
Comment 3 Andrei Ilitchev CLA 2010-06-03 11:01:26 EDT
Cheched into trunk.
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:05:24 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink