hibernate-dev  

Re: [hibernate-dev] Immutable entities

Hardy Ferentschik
Mon, 23 Jun 2008 03:16:50 -0700

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@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/hibernate-dev