Tuesday, March 4, 2014

Hibernate Lazy Load exception and how to load an relationship



Hibernate and lazy loading sucks. Especially in Web Dynpro where we cannot use the Session in View pattern. Therefore there is handy method can load a collection to make sure that a collection is loaded and load it on demand from the service layer

public abstract class GenericDaoBean implements GenericDao { 

// ....

public void initializeRelationship(T entity, String property) {
        org.hibernate.Session session = getHibernateSession();
        // the lock method adds the entity back into the Entitymanager makes it managed.
        try {
            session.lock(entity, LockMode.NONE);
            Object collectionProxy = getAttributeValue(entity, property);

            if(!Hibernate.isInitialized(collectionProxy))
            {
                Hibernate.initialize(collectionProxy);
            }
        } catch (TransientObjectException e) {
            //ignore object that are not saved yet
        }
    }

    public org.hibernate.Session getHibernateSession() {
        Object delegate = getEntityManager().getDelegate();
        org.hibernate.Session session = null;
        //This is in SAP J2EE
        if(delegate instanceof org.hibernate.Session){
            session = (org.hibernate.Session) delegate;
        }
        // this is in OpenEJB container
        else if(delegate instanceof HibernateEntityManager){
            HibernateEntityManager hbEm = (HibernateEntityManager) delegate;
            session = hbEm.getSession();
        }
        return session;
    }
}

public static Object getAttributeValue(Object bean, String attributeName){
        Map props = getPropertyDescriptors(bean.getClass(), new String[]{"class"});
        PropertyDescriptor desc = props.get(attributeName);
        if(desc == null) throw new IllegalArgumentException("attribute:" +attributeName + " does not exist in bean "+ bean);
       
        try {
            Object result = desc.getReadMethod().invoke(bean, null);
            return result;
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

Wednesday, February 19, 2014

Oracle: How to escape an &


Insert into BRMF_VALUE_CATALOG (ID,TITLE,TYPE,CAN_HAVE_CHILDREN,PARENT_ID) values (322,'&MORE','Generic_Suffix_Basket',0,3);
DOES not work
 

escape the & with || chr(38) ||

Insert into BRMF_VALUE_CATALOG (ID,TITLE,TYPE,CAN_HAVE_CHILDREN,PARENT_ID) values (322,chr(38) || 'MORE','Generic_Suffix_Basket',0,3);




See also
http://stackoverflow.com/questions/1137354/oracle-pl-sql-escape-character-for-a
 

SAP Netweaver: 7.31 ""Log files are not loaded yet. Press Refresh after short time"

Problem:
You want to view the logs on a SAP 7.31 J2EE Engine but you get the message
 "Log files are not loaded yet. Press Refresh after short time"

I found a SAP Note that gives a hint with the security settings.

http://www.stechno.net/sap-notes.html?view=sapnote&id=1631616

Solution: Login to the logview via https then it works

Sunday, January 9, 2011

Implement a simple cache with java

The class LinkedHashMap has the method removeEldestEntry() that is called before
put() method so here you can remove the eldest element from the cache.


Map mapCache = new LinkedHashMap()
{
@Override
protected boolean removeEldestEntry(Entry eldest) {
//remove after 2 hours
Calendar cal = Calendar.getInstance();
long howLongAgoInMs = System.currentTimeMillis()-eldest.getValue().lastTimeTouched;
long twoHoursInMs = 2 * 60 * 60 * 1000;
if(howLongAgoInMs > twoHoursInMs){
return true;
}
return false;
}
};

Saturday, January 8, 2011

SAP NWDS, Web Dynpro: Add Destination/R3 fails

Problem:
In the SAP Netweaver Developer Studio you want to add a Destination
Window->Preferences->Destinations->R3 Destinations.

Applys to: NWDS 7, Windows 7

Cause:

Solution:
-vm
C:\DEV\Java\jdk1.5.0_22\bin\javaw.exe
-showsplash
com.sap.netweaver.developerstudio
--launcher.XXMaxPermSize
256m
-vmargs
-Xmx512m
-Xms128m
-XX:PermSize=32m
-XX:MaxPermSize=256m
-Dfile.encoding=UTF-8
-Dosgi.requiredJavaVersion=1.5
-Dos.name='Windows XP'

SAP NWDI: concurrent-lock-denied check failed

Problem:
When trying to revert an activity you get the error:
concurrent-lock-denied check failed
See also:
http://forums.sdn.sap.com/thread.jspa?threadID=1805846&tstart=15

Solution:
Restart your J2EE Server where the NWDI is running.