-->
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: NullPointerException from AbstractType
PostPosted: Fri Jul 21, 2006 3:04 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
Hibernate version:
Hibernate 3.2.0.CR2
Annotations 3.2.0.CR1
EntityManager 3.2.0.CR1

Mapping annotations: too many annotated classes involved to list here

Code between sessionFactory.openSession() and session.close():

Out of container (test case) inside a transaction.

Full stack trace of any exception that occurs:
Code:
Caused by: java.lang.NullPointerException
   at org.hibernate.type.AbstractType.getHashCode(AbstractType.java:112)
   at org.hibernate.type.AbstractType.getHashCode(AbstractType.java:120)
   at org.hibernate.type.EntityType.getHashCode(EntityType.java:402)
   at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:162)
   at org.hibernate.engine.EntityKey.generateHashCode(EntityKey.java:104)
   at org.hibernate.engine.EntityKey.<init>(EntityKey.java:48)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:75)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:871)
   at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:208)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:99)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:689)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:671)
   at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:156)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
   at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:332)
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:245)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:99)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:689)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:671)
   at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:156)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
   at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:332)
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:245)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:99)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:689)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:671)
   at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:156)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
   at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:332)
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:245)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:99)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:689)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:671)
   at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:156)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
   at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:332)
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:245)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:99)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:689)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:671)
   at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:156)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
   at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:332)
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:245)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:99)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:51)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:679)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:663)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:667)
   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:201)

Name and version of the database you are using:
Oracle 9

Any idea what would cause this? Is that error coming from one of the hash code implementations that I wrote and hibernate is extending? I looked over all of my hashCode implementations and I dont see any way they can throw that. (and besides wouldnt my class be in the stack trace?)

Is there something else I can do to help trace this one down?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 12:24 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
AbstractType.java is calling this when x is null...
Code:
public int getHashCode(Object x, EntityMode entityMode) {
      return x.hashCode();
   }


In this method in EntityType.java
Code:
   public int getHashCode(Object x, EntityMode entityMode, SessionFactoryImplementor factory) {
      EntityPersister persister = factory.getEntityPersister(associatedEntityName);
      if ( !persister.canExtractIdOutOfEntity() ) {
         return super.getHashCode(x, entityMode);
      }
      
      final Serializable id;
      if (x instanceof HibernateProxy) {
         id = ( (HibernateProxy) x ).getHibernateLazyInitializer().getIdentifier();
      }
      else {
         id = persister.getIdentifier(x, entityMode);
      }
      return persister.getIdentifierType().getHashCode(id, entityMode, factory);
   }



id is null for that last return call. It calls getIdentifier(x, entityMode) on the persister, which Looks up the POJO tupleizer, and calls abstractEntityTupilizer.getIdentifier(entity) which was then calling into my Entity to get it's id. In this case it was null (because it was newly created).

Now, Im not really sure why it is asking my class for an id. It is annotated as such:

Code:
@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "products")
@SequenceGenerator(name = "products_seq", sequenceName = "products_seq")
public class Product {


/* ...  */

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "products_seq")
  @Column(name="product_id")
  public Long getProductId() {
    return productId;
  }

  public void setProductId(Long productId) {
    this.productId = productId;
  }

/* ...  */

}


There is a ManyToOne for a productMaster which has two back references to it: the original product and and a list of the current products. I'll be looking into why it is asking that class for an id today... any advice would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 1:10 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
So... When I had this problem the first time, I managed to work around it by changing the way I was persisting objects. Essentially, I made some changes that switched it from a merge call to a persist call. Last week, I had some new code that was getting the same error. This time, it was with a class mapped the with composite primary keys like this:

http://forum.hibernate.org/viewtopic.php?t=958753

In this case however, it was a class with 3 columns making up the composite primary key. Again, Hibernate was getting a null pointer asking a newly created object for an id. In this case it was already a persist() call so what I did last time was already happening. The tables were set up like this:

Table 1, Table 2 and Table 3 all have normal primary keys. Table 4 has a primary key consisting of 1,2 and 3. I have a Java Object called Table4 and another called Table4Pk as a @Embedded object. There is a CascadeType.PERSIST on the @ManyToOne of Table4Pk going to Table3.

In the test case I find an existing Table1 and Table2 object but I create a new Table3 object. I pass these 3 objects to a newly created Table4 object and try to persist it. hibernate then tries to ask Table3 for it's id which thows the NullPointerException. Making Table3 return a new Long(-1L) for it's id prevents the null pointer but instead results in a TransientObjectException. If you call persist(table3) and then persist(table4) it works.

So in short, it appers that Hibernate is failing to recoginze the CascadeType.PERSIST on the embedded object. I'll try to make a test case for this when I get a chance.


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.