simon_t wrote:
Quote:
After more checking I found the problem of duplicate objects is happening everywhere in my application, not just in the aforementioned objects. This happens despite having implemented correct equals() and hashCode() methods everywhere.
Implementing equals() and hashCode() will not rid your application of duplicate objects. All implementing equals() and hashCode() will do is enable you to work with objects loaded from different hibernate sessions.
Allright, that's clear. Thank you for clearing it up.
simon_t wrote:
Quote:
This is what I do not really understand. How / where did I create a new sessions?
It is difficult to make my point clear without knowing how you create the hibernate sessions.
Here is my code which loads objects:
Code:
public List<NewsItem> getNewsItems() throws DaoException
{
List<NewsItem> result = null;
Session session = getSessionFactory().getCurrentSession();
try
{
session.getTransaction().begin();
Query query = session.createQuery("from DefaultNewsItem as newsitem order by newsitem.published");
query.setTimeout(5);
result = query.list();
}
catch (HibernateException he)
{
session.getTransaction().rollback();
throw new DaoException("Unable to load newsitems.", he);
}
finally
{
session.getTransaction().commit();
}
return result;
}
Both the events and the newsitems are loaded this way.
simon_t wrote:
Instead please consider the following code:
Code:
Session firstSession = createSession();
firstSession.beginTransaction();
Event eventFromFirstSession = (Event) firstSession.load(Event.class, 1);
firstSession.getTransaction().commit();
Session secondSession = createSession();
secondSession.beginTransaction();
Event eventFromSecondSession = (Event) secondSession.load(Event.class, 1);
secondSession.getTransaction()..commit();
if (eventFromFirstSession == eventFromSecondSession) {
System.out.println("duplicate java objects created");
}
The above code will print out "duplicate java objects created". Of course the example needs some other supporting code but I hope it serves to help explain an important point. domain objects loaded in different sessions in this way will result in duplicate java objects.
Allright, now I am a bid confused, because if "eventFromFirstSession == eventFromSecondSession" is true, doesn't this mean that the two objects are exactly the same (the same in memory), so this means that they are not duplicate...
simon_t wrote:
You also said on Wed Jul 25, 2007 at 12:41 pm:
Quote:
I understand the example, it exactly demonstrates what I do not want. I want to have the same Java object for the same database row.
I think there are only two ways of avoiding duplicate objects.
1. Make sure your application never uses objects loaded from different sessions together
2. To re-attach the objects you want to use to the new session before loading other objects from the session by using Session.update() or Session.lock().
Ad 1: in my previous code example: how did I close the session? E.g., how can I load everything in one session, doesn't getCurrentSession() return the same sessions each time?
Ad 2: If I chose this approach I should either use session.update() or session.lock() to add my already loaded objects to the new session?
Thank you very much for all you help, I think we are close to finding a solution.
--
Best regards,
Jethro