net.sf.hibernate.AssertionFailure: cannot cache a reference to an object with a null id

Hibernate JIRA | tekno | 1 decade ago
  1. 0

    I am getting an AssertionFailure - cannot cache a reference to an object with a null id when I try to do an update. Here is the sample code - NB: This has been working fine for me in hibernate versions 2.0 - 2.03 I have the following two mappings <class name="Customer" table="customers" polymorphism="explicit"> <cache usage="read-write"/> <id name="id" unsaved-value="0"> <generator class="sequence"> <param name="sequence">customers_id_seq</param> </generator> </id> <property name="postcode"/> <many-to-one name="csOwner" class="Employee" column="cs_owner" /> </class> and <class name="Employee" table="staff"> <id name="id"> <generator class="sequence"> <param name="sequence">staff_id_seq</param> </generator> </id> <property name="name"/> </class> the following main() throws the exception Session session = null; Customer customer = null; SessionFactory factory = null; try { // build factory factory = new Configuration().configure().buildSessionFactory(); System.out.println("Starting first session"); // first session gets customer instance session = factory.openSession(); customer = (Customer) session.load(Customer.class, new Long(1275)); System.out.println("Loaded customer " + customer.getId() + " has owner " + customer.getCsOwner().getId()); session.flush(); session.connection().commit(); session.close(); System.out.println("Updating address field"); // update a field customer.setPostcode("TESTING"); System.out.println("Starting second session"); // second session persists update session = factory.openSession(); session.update(customer); session.flush(); session.connection().commit(); session.close(); System.out.println("Finished"); factory.close(); } catch (Exception ex){ ex.printStackTrace(); } finally { if(session!=null)session.close(); if(factory!=null)factory.close(); System.exit(0); } If i remove the cache entry in the customer mapping everything is OK, but other wise i get net.sf.hibernate.AssertionFailure: cannot cache a reference to an object with a null id at net.sf.hibernate.type.EntityType.disassemble(EntityType.java:89) at net.sf.hibernate.impl.CacheEntry.disassemble(CacheEntry.java:36) at net.sf.hibernate.impl.CacheEntry.<init>(CacheEntry.java:27) at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:42) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2278) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2235) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2178) at com.lab.model.HibBug.main(HibBug.java:46)

    Hibernate JIRA | 1 decade ago | tekno
    net.sf.hibernate.AssertionFailure: cannot cache a reference to an object with a null id
  2. 0

    I am getting an AssertionFailure - cannot cache a reference to an object with a null id when I try to do an update. Here is the sample code - NB: This has been working fine for me in hibernate versions 2.0 - 2.03 I have the following two mappings <class name="Customer" table="customers" polymorphism="explicit"> <cache usage="read-write"/> <id name="id" unsaved-value="0"> <generator class="sequence"> <param name="sequence">customers_id_seq</param> </generator> </id> <property name="postcode"/> <many-to-one name="csOwner" class="Employee" column="cs_owner" /> </class> and <class name="Employee" table="staff"> <id name="id"> <generator class="sequence"> <param name="sequence">staff_id_seq</param> </generator> </id> <property name="name"/> </class> the following main() throws the exception Session session = null; Customer customer = null; SessionFactory factory = null; try { // build factory factory = new Configuration().configure().buildSessionFactory(); System.out.println("Starting first session"); // first session gets customer instance session = factory.openSession(); customer = (Customer) session.load(Customer.class, new Long(1275)); System.out.println("Loaded customer " + customer.getId() + " has owner " + customer.getCsOwner().getId()); session.flush(); session.connection().commit(); session.close(); System.out.println("Updating address field"); // update a field customer.setPostcode("TESTING"); System.out.println("Starting second session"); // second session persists update session = factory.openSession(); session.update(customer); session.flush(); session.connection().commit(); session.close(); System.out.println("Finished"); factory.close(); } catch (Exception ex){ ex.printStackTrace(); } finally { if(session!=null)session.close(); if(factory!=null)factory.close(); System.exit(0); } If i remove the cache entry in the customer mapping everything is OK, but other wise i get net.sf.hibernate.AssertionFailure: cannot cache a reference to an object with a null id at net.sf.hibernate.type.EntityType.disassemble(EntityType.java:89) at net.sf.hibernate.impl.CacheEntry.disassemble(CacheEntry.java:36) at net.sf.hibernate.impl.CacheEntry.<init>(CacheEntry.java:27) at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:42) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2278) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2235) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2178) at com.lab.model.HibBug.main(HibBug.java:46)

    Hibernate JIRA | 1 decade ago | tekno
    net.sf.hibernate.AssertionFailure: cannot cache a reference to an object with a null id
  3. 0

    Hibernate Community • View topic - AssertionFailure: possible nonthreadsafe access to session

    hibernate.org | 3 months ago
    net.sf.hibernate.AssertionFailure: possible nonthreadsafe access to session
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    If somehow a flush happens while an object is being saved, following exception is thrown: net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs) at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2605) at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2429) at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2422) at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2224) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203) at com.alcatel.util.property.HibernatePropertyStore.getProperty(HibernatePropertyStore.java:34) at com.alcatel.util.property.ReflectionPropertyGroup.loadProperties(ReflectionPropertyGroup.java:74) at com.alcatel.util.property.PropertyManager.loadPropertyGroup(PropertyManager.java:79) at com.alcatel.util.property.PropertyManager.getPropertyGroup(PropertyManager.java:73) at com.alcatel.util.email.EmailAddress.setEmailAddress(EmailAddress.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at net.sf.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:38) at net.sf.hibernate.type.ComponentType.setPropertyValues(ComponentType.java:230) at net.sf.hibernate.type.ComponentType.deepCopy(ComponentType.java:270) at net.sf.hibernate.type.TypeFactory.deepCopy(TypeFactory.java:212) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:900) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:839) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:757) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720) In our case, a flush was executed during a save because the saved object contained a component (EmailAddress) that has some intelligence in it's setter method. The setter checks the email against a pattern that is stored in the DB. When retrieving the pattern (using the same (thread local) session), hibernate issues a flush, leading to the exception. This is just one example, but i guess there will be other ways of (unintentionally) flushing during a save. Currently, we worked around the problem by explicitly setting the session flushmode to NEVER, but thats not an ideal solution. Possible solutions: * Fix the ComponentType to not call the setter when saving. Does not make to much sense anyway. * SessionImpl#flushEntities should not do anything in case the status is SAVING. For completeness, the EmailAddress component: public class EmailAddress implements Serializable { private InternetAddress m_address; private String m_emailAddress; public EmailAddress() { } public EmailAddress(String emailAddress) throws MalformedEmailException { setEmailAddress(emailAddress); } public String toString() { return m_emailAddress; } /** * @hibernate.property * column="EMAIL_ADDRESS" */ public String getEmailAddress() { return m_emailAddress; } public Address getAddress() { return m_address; } public void setEmailAddress(String stringValue) throws MalformedEmailException { MailPropertyGroup mailPropertyGroup = (MailPropertyGroup) PropertyManager.getInstance().getPropertyGroup(MailPropertyGroup.GROUP_NAME); //This leads to the exception above... Pattern emailPattern = Pattern.compile(mailPropertyGroup.getEmailRegex()); Matcher matcher = emailPattern.matcher(stringValue); if (!matcher.matches()) { throw new MalformedEmailException(stringValue); } try { m_address = new InternetAddress(stringValue); //this check alone is not good enough } catch (AddressException ex) { throw new MalformedEmailException(stringValue, ex); } m_emailAddress = stringValue; }

    Hibernate JIRA | 1 decade ago | Koen Janssens
    net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. net.sf.hibernate.AssertionFailure

      cannot cache a reference to an object with a null id

      at net.sf.hibernate.type.EntityType.disassemble()
    2. net.sf.hibernate
      SessionImpl.flush
      1. net.sf.hibernate.type.EntityType.disassemble(EntityType.java:89)
      2. net.sf.hibernate.impl.CacheEntry.disassemble(CacheEntry.java:36)
      3. net.sf.hibernate.impl.CacheEntry.<init>(CacheEntry.java:27)
      4. net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:42)
      5. net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2278)
      6. net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2235)
      7. net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2178)
      7 frames
    3. com.lab.model
      HibBug.main
      1. com.lab.model.HibBug.main(HibBug.java:46)
      1 frame