-->
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.  [ 3 posts ] 
Author Message
 Post subject: java.lang.StackOverflowError
PostPosted: Sun Apr 16, 2006 11:32 am 
Newbie

Joined: Mon Apr 10, 2006 1:36 pm
Posts: 3
Hibernate version:3.1.1

Hey all

I am trying to test my first Hibernate application and have ran into some problems when deleting objects.

In my test class I have the following method

Code:
public void testDeletePerson() {
      Person person = personDao.getPersonByUsername("person1");
      personDao.delete(person);
   }


This will throw the application into an infinite loop, ending with an java.lang.StackOverflowError which is caused in the following piece of code

Code:
public class PersonDAO extends AbstractDAO {
    Log log = LogFactory.getLog(PersonDAO.class);

    public PersonDAO() {
        super();
    }

    public void delete(Person person) throws DAOException {
       delete(person);
    }
..


And here is the called method delete from the abstract class AbstractDAO

Code:
public abstract class AbstractDAO {
    private Session session;
    private Transaction tx;
..
protected void delete(Object obj) {
        try {
            startOperation();
            session.delete(obj);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }


My create and read methods work fine, it's the delete method which causes the test to crash. The debugger isn't giving me any ideas on what i'm doing wrong either as it calls the delete(Person person) in PersonDAO repeatedly until the java.lang.StackOverflowError occurs (the stacktrace is also just a bunch of calls to delete(Person person) and then StackOverFlowError). I also tried doing this in my test case:

Code:
Session session = HibernateFactory.openSession();
      Person person = personDao.getPersonByUsername("person1");
      session.delete(person);


But with no luck, the stackoverflowerror doesn's occur, but the row in the database fails to get deleted. Any ideas on why the test code is causing this infinite loop?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 12:11 pm 
Newbie

Joined: Mon Apr 10, 2006 1:36 pm
Posts: 3
Okay so I solved it... but can someone please tell me why this error occured in the first place??

Code:
public class PersonDAO extends AbstractDAO {
   Log log = LogFactory.getLog(PersonDAO.class);

    public PersonDAO() {
        super();
    }

    public void create(Person person) throws DAOException {
        saveOrUpdate(person);
    }

     public List findAll() throws DAOException{
        return findAll(Person.class);
    }

    public void delete(Person person) throws DAOException {
       super.delete(person); //modified line
    }
..


As you can see, I just added super. in my delete method. But how come this isn't needed in methods such as create and findAll?? This makes no sense at all..


Top
 Profile  
 
 Post subject: Re: java.lang.StackOverflowError
PostPosted: Wed Apr 19, 2006 3:51 pm 
Newbie

Joined: Tue Apr 18, 2006 3:05 pm
Posts: 9
The reason is b/c of common Java inheritance.

Your other 2 methods have either a different name or different signature (argument), so it knows to jump to the Abstract class's method. Since your delete method is the same, it is actually calling itself. If you change the name of the delete method in your implemented DAO class to "deletePerson(person)"...and remove the "super."...it'll work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.