Link to home
Start Free TrialLog in
Avatar of onaled777
onaled777Flag for United States of America

asked on

Preventing Cast Exception

The code below throws an error at the line:

owner = (Owner) session.load(Owner.class, new Long(id));

whenever an int value is passed to the getOwner method.

How do I modify this method to accept both int and long primitives in the method.
public static Owner getOwner(long id)
    {
    	Owner owner = null;
        if (id > 0)
        {
            PersistorSession session = null;
            try
            {
                session = PersistorSessionFactory.getInstance().getPersistorSession();
                owner = (Owner) session.load(Owner.class, new Long(id));
            }
            catch (PersistenceException pe)
            {
                logger.error("Persistence error loading the following Owner object id from memory: " + id, pe);
                pe.printStackTrace();
            }
            catch (Exception e)
            {
                logger.error("Error loading the following Owner object id from memory: " + id, e);
                e.printStackTrace();
            }
            
            finally
            {
                if (session != null)
                {
                    try
                    {
                        session.close();
                        session = null;
                    }
                    catch (PersistenceException pe)
                    {
                        logger.error("Error occured while closing persistor session", pe);
                        pe.printStackTrace();
                    }
                }
            }
        }
        return owner;
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sweetfa2
sweetfa2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of onaled777

ASKER

Thank you. the issue was found by stepping into the session.load.  The cast that was causing the error was found in there.