-->
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.  [ 7 posts ] 
Author Message
 Post subject: hashCode NullPointerException on merge
PostPosted: Fri Jul 25, 2008 5:50 am 
Newbie

Joined: Fri Jul 25, 2008 2:53 am
Posts: 2
Hibernate version: 3.3.2.GA

Code between sessionFactory.openSession() and session.close():
dao.merge(sub);


Full stack trace of any exception that occurs:
java.lang.NullPointerException
at com.example.adplatform.model.Campaign.hashCode(Campaign.java:141)
at java.util.HashMap.put(HashMap.java:372)
at java.util.HashSet.add(HashSet.java:200)
at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:329)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:240)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:222)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:195)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:877)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:865)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3049)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:399)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
at org.hibernate.type.EntityType.resolve(EntityType.java:379)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:120)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3049)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:399)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:229)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:120)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:235)
at com.example.common.dao.jpa.DaoImpl.merge(DaoImpl.java:81)
at com.example.adplatform.service.impl.AdPlatformServiceImpl.updateSub(AdPlatformServiceImpl.java:859)
at com.example.adplatform.service.impl.AdPlatformServiceTest.testUpdateSubDirectLinkAd(AdPlatformServiceTest.java:2771)
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:597)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127) at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
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:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)



Name and version of the database you are using: MySQL 5.0.51b

I'm seeing this NullPointerException when merging an object. I know this bug is related to hibernate.max_fetch_depth setting. If I change it I can eliminate the problem but it becomes unreasonably large. My overriden version of hashcode basically takes a unqiue key and hashcodes the values that make up that key. In this case:
Code:
public class Campaign{
    private TrafficSourceAccount trafficSourceAccount;
    private String name;
.
.
.
    public int hashCode() {
        int result = 17;
       
        Long trafficSourceAccountId;
        if(getTrafficSourceAccount() != null){
            trafficSourceAccountId = getTrafficSourceAccount().getId() == null ? new Long(0L) : getTrafficSourceAccount().getId();
        }else{
            trafficSourceAccountId = new Long(0L);
        }
       
        result = 37 * result + trafficSourceAccountId.hashCode();
        result = 37 * result + getName().hashCode(); //This is the line the NullPointer is thrown
       
        return result;
    }
}

The problem I'm having is this NullPointer is being thrown on an operation of some sort internal to Hibernate. If I execute hashCode() before the merge in my test case the NullPointerException is still thrown during the merge and not when I call it before the merge. I'm assuming that means the Campaign that is in the object I'm merging isn't the issue. It's some other Campaign that is in a collection that is being hydrated as part of this object graph. Does anyone know what the problem is? Is there a better way to override hashCode to avoid this sort of problem in large object graphs?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 9:17 pm 
Newbie

Joined: Fri Jul 25, 2008 2:53 am
Posts: 2
OK so I made this much more complicated than it needs to be. I'm seeing the same problem in a much simpler relationship. Let's say just Parent and Child.
Code:
public Parent{
  @Id @GeneratedValue
  private Long id;
  private String name;

  @OneToMany(cascade = CascadeType.ALL, mappedBy="parent")
  private Set<Child> children;
.
.
.
  public Child addChild(Child child){
    child.setParent(this);
    getChildren().add(child);

    return child;
  }
}
---
public Child{
  @Id @GeneratedValue
  private Long id;
  private String name;

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn("parent_id")
  private Parent parent;

  public in hashCode(){
    return getName().hashCode(); //On merge this throws a NullPointer
  }
}
---
---
public void test(){
  beginTransaction();

  Parent parent = new Parent();
  parent.setName("Something");

  Child child = new Child();
  child.setName("SomeChildName");

  parent.addChild(child)

  parentDao.persist(parent);

  closeTransaction();
  beginTransaction();
 
  Child mergedChild = childDao.merge(child);

  closeTransaction();
}


So at the childDao.merge() I can see the child not found in the Session, I can see it retreived, I can then see the Parent being retreived and in it's collection it finds the child that was already retreived but when it is being added to the Set the NPE is thrown. So anyone know what I'm doing wrong in this simplified version of my problem?


Top
 Profile  
 
 Post subject: Re: hashCode NullPointerException on merge
PostPosted: Wed Feb 03, 2010 6:32 am 
Newbie

Joined: Fri Aug 22, 2008 6:12 am
Posts: 13
I know this is old, but I have the same problem...

Any ideas what this is about?

_________________
believing in the impossible makes it possible


Top
 Profile  
 
 Post subject: Re: hashCode NullPointerException on merge
PostPosted: Wed Feb 03, 2010 7:34 am 
Newbie

Joined: Fri Aug 22, 2008 6:12 am
Posts: 13
ok, this seems to be a know issue for more than a year:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3441?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel

_________________
believing in the impossible makes it possible


Top
 Profile  
 
 Post subject: Re: hashCode NullPointerException on merge
PostPosted: Wed Feb 03, 2010 6:33 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
no that's just wrong, you have to write the hashcode in a safer way without assuming all fields to have a value.
The reported issue is using a wrong implementation:
Code:
this.event.getId().hashCode() ^ this.id.hashCode();

is just dangerous, because it assumes the id!=null

same story in this thread:
Quote:
result = 37 * result + getName().hashCode(); //This is the line the NullPointer is thrown

getName() could be null, this is just wrong code in the user class.

Any IDE is able to assist in writing a proper hashCode() implementation

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: hashCode NullPointerException on merge
PostPosted: Wed May 19, 2010 10:04 am 
Newbie

Joined: Tue May 04, 2010 8:53 am
Posts: 2
if getName() can be null at one point and non null at a later point, then it can't be used in hashCode() because the hash would change. That is the same reason that database identifiers can't be used in hashCode(). However, getName() shouldn't ever return null if we don't allow it to be set to null, right? Why is Hibernate calling hashCode() on an object before its state is loaded from the database? I am having this issue, and I believe getName() should be usable in hashCode() without checking for null if we don't allow the users of the class to set name to null. So what is going on when Hibernate calls hashCode() before the object's state is loaded?


Top
 Profile  
 
 Post subject: Re: hashCode NullPointerException on merge
PostPosted: Wed May 19, 2010 12:58 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
hibernate didn't call hashCode: you have added it to a Set, likely a HashSet, which will call the hashCode to position the entry internally.
this is plain java.util.collections usage

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.