-->
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: Calling em.refresh on detached entity should throw exception
PostPosted: Wed Jun 28, 2006 1:56 pm 
Beginner
Beginner

Joined: Mon Jan 09, 2006 3:05 pm
Posts: 24
Hibernate version:
* Hibernate Core 3.2.0.CR2
* Hibernate Annotation 3.2.0.CR1
* Hibernate Entity Manager 3.2.0.CR1

Environment
A Java SE extended persistence context.

Code between emf.createEntityManager() and em.close():
Code:
        em.getTransaction().begin();
        Initiative i = new Initiative("initiative1");
        em.persist(i);       
        em.getTransaction().commit();   
       
        em.clear();
       
        em.getTransaction().begin();
        em.refresh(i);
        em.getTransaction().commit();


Description of Problem
The spec says calling em.clear() causes all managed entities to be detached. It also says calling em.refresh() on an entity that is not managed should throw an exception. Thus, the above test should result in an exception, but it does not. (Nor does it seem to refresh the entity.)

Should I file a JIRA report?

I posted another problem with Java SE extended persistence contexts, http://forum.hibernate.org/viewtopic.php?t=960885, are they as well supported as the more common transaction scoped contexts?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 5:20 am 
Newbie

Joined: Wed Jun 21, 2006 5:46 am
Posts: 10
Location: Finland
I think you are right. This is a bug in Hibernate. The spec says that if you call em.refresh method to entity that is not managed (for example detached) IllegalArgumentException is thrown.

I tried this in Eclipse debug mode and noticed that em.clear() detaches the entity as it should, but em.refresh() does not throw Exception, it fetches the entity state from database. Log4j also confirms this by logging the executed SQL select statement.

I tried the same code with Bea Kodo 4.0 (Solarmetric) JPA implementation and it did throw an IllegalArgumentException, when em.refresh was called.

My test code:
Code:
public class ExtendedSEPersistence {
 
  private EntityManagerFactory emf;
  private EntityManager em;
  private String hibernate = "hibernate-local";
  //private String kodo = "kodo-local";
 
  public ExtendedSEPersistence() {
    emf = Persistence.createEntityManagerFactory(hibernate);
    //emf = Persistence.createEntityManagerFactory(kodo);
    em = emf.createEntityManager();
  }
 
  public void runTest() {
    try {
      em.getTransaction().begin();
      Type t = new Type(new TypeCode(-1004), new ClassCode(-1000) , "Test Type 4" );
      em.persist(t);
      em.getTransaction().commit();
 
      em.clear();

      em.getTransaction().begin();
      em.refresh(t); // this should throw Exception
      em.getTransaction().commit();

      System.out.println("Type = " + t);  // should not be called
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
  }
 
  public static void main(String[] args) {
    ExtendedSEPersistence p = new ExtendedSEPersistence();
    p.runTest();
  }

}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 7:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I've fixed the bug this week.

_________________
Emmanuel


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.