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

onaled777Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sweetfa2Commented:
What is the full text of the exception you are getting.  Are you sure the exception is on the int or the return value from session.load?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stachenovCommented:
Int value is perfectly convertible to long. Moreover, it is converted as soon as it's passed to the getOwner() method, so once inside, it is a full-fledged long already. sweetfa2 is right, the only place in that line where a cast exception could occur is the cast to Owner. If you think it has anything to do with ints, perhaps session.load() returns something incompatible with Owner when passed a small enough value?
0
onaled777Author Commented:
Thank you. the issue was found by stepping into the session.load.  The cast that was causing the error was found in there.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.