Java Mailing List Archive

http://www.gg3721.com/

Home » Hibernate Dev List »

Re: [hibernate-dev] Immutable entities

Hardy Ferentschik

2008-06-23

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi,

It seems changing an immutable entity does not throw an exception. Instead
the update statements just gets ignored. However additions/removals to an
immutable collection throws a HibernateException. Is this correct?

I also changed my test code to do something like this:

 public void testImmutableEntity() throws Exception {
   Session s = openSession();
   Transaction tx = s.beginTransaction();
   Country country = new Country();
   country.setName("Germany");
   s.persist(country);
   tx.commit();
   s.close();

   // try changing the entity
   s = openSession();
   tx = s.beginTransaction();
   Country germany = (Country) s.get(Country.class, country.getId());
   assertNotNull(germany);
   germany.setName("France");
   assertEquals("Local name can be changed", "France", germany.getName());
   s.save(germany);
   tx.commit();
   s.close();
   
   // retrieving the country again - it should be unmodified
   s = openSession();
   tx = s.beginTransaction();
   germany = (Country) s.get(Country.class, country.getId());
   assertNotNull(germany);
   assertEquals("Name should not have changed", "Germany",
germany.getName());
   s.close();
   
//    // try deletion
//    s = openSession();
//    tx = s.beginTransaction();
//    s.delete(germany);
//    tx.commit();
//    s.close();
//    
//    s = openSession();
//    tx = s.beginTransaction();
//    germany = (Country) s.get(Country.class, country.getId());
//    assertNotNull(germany);
//    assertEquals("Name should not have changed", "Germany",
germany.getName());
//    s.close();
 }

Changes to the entity seem to be properly ignored, but if I comment out
the attempt to delete an entity the test fails. Personally I think it
should be possible to delete the entity, however the documentation says:
"may not be updated or deleted by the application".

So my questions really are:

1. Is it correct that an immutable entity behaves differently than an
immutable collection? For the former update statements just get ignored,
for the latter a HibernateException is thrown.

2. Is the documetnation of immutable wrong - an immutable entity can be
deleted?

--Hardy



_______________________________________________
hibernate-dev mailing list
hibernate-dev@(protected)
https://lists.jboss.org/mailman/listinfo/hibernate-dev
©2008 gg3721.com - Jax Systems, LLC, U.S.A.