-->
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: PersistentMap.get returns null for key it has
PostPosted: Sun Oct 02, 2011 2:26 pm 
Newbie

Joined: Sun Sep 25, 2011 4:05 pm
Posts: 12
So this is driving me just crazy, I'm calling the get function on a map and it always returns null. I have verified that the map indeed contains my key, and that the entry indeed has a non-null value. I also verified that the hashCode and the equals functions are working properly between the entity I try to get as a key, and the entity that is the key.

Here is my test code:
Code:

      System.out.println("=============\nTesting Load:");
      reset();
      UserEntity user = makeUser();
      String loginName = user.getUserInfo().getLoginName();
      addMeasurementData(user);
      MeasurementTypeEntity type = user.getMeasurements().keySet().iterator().next();
      Assert.assertTrue("Type is null.",type!=null);
      Assert.assertTrue("Type is not a persistent/detached entity.",type.getId()>0);
      Date d = user.getMeasurements(type).getMeasurements().keySet().iterator().next();
      Assert.assertTrue(user.getId()==null || user.getId()<0);
      // type should already be persistent
      Assert.assertTrue(type.getId()!=null && type.getId()>=0);
      try {
         user.save(true);
      } catch (ConflictException e) {
         Assert.fail("Unexpected exception saving user.");
         e.printStackTrace();
      }
      String email = user.getUserInfo().getEmailAddress();
      Long id = user.getId();
      user = null;
      user = UserEntity.getById(id,true);
      Assert.assertTrue("user gotten by id is null: "+(user==null)+", or id does not match: "+
            id+"!="+(user==null ? "n/a":user.getId()),user != null && user.getId().equals(id));
      
      // make sure that child data was persisted too.
      Assert.assertTrue(user.getId()!=null && user.getId()>=0);
      
      // ===========================
      // THIS IS WHERE THE PROBLEM STARTS:
      // ===========================


      Map<MeasurementTypeEntity,MeasurementsEntity> mmss = user.getMeasurements();
      System.out.println("type hash code is "+type.hashCode());
      Iterator<MeasurementsEntity> iter = mmss.values().iterator();
      int i = 0;
      while (iter.hasNext()) {
         MeasurementTypeEntity t = iter.next().getType();
         System.out.println("\t\t-- value: "+t+", hc="+t.hashCode()+", eq="+t.equals(type)+", contains: "+mmss.containsKey(t));
         i++;
      }
      System.out.println("There were "+i+" values.");
      Iterator<MeasurementTypeEntity> iter2 = mmss.keySet().iterator();
      int j = 0;
      while (iter2.hasNext()) {
         MeasurementTypeEntity t = iter2.next();
         System.out.println("\t\t-- key: "+t+", hc="+t.hashCode()+", eq="+t.equals(type)+", contains: "+mmss.containsKey(t));
         j++;
      }
      System.out.println("There were "+j+" keys.");
      System.out.println(mmss.keySet().contains(type));
      
      MeasurementsEntity measurements = user.getMeasurements(type);
      Assert.assertTrue("Measurements returned null for type: "+type.getName(),measurements!=null);


Here is the console output:
Quote:
type hash code is 2017741059
-- value: weight (lb), hc=2017741059, eq=true, contains: false
There were 1 values.
-- key: weight (lb), hc=2017741059, eq=true, contains: false
There were 1 keys.
false


From the console output you can see that the key in my map has the same hashcode as the one I try using to get it, and that the equals function returns true between the key and my argument. You can also see that the map's contains function returns false even for keys taken straight from it! I did not write the PersistentMap class, it's hibernate's, so why on earth is this happening?

Here is the code for the user.getMeasurements(type) function:
Code:

   public MeasurementsEntity getMeasurements(MeasurementTypeEntity type) {
      if (this.measurements==null)
         return null;
      if (type == null)
         throw new NullPointerException("Must specify a type of measurements to get.");
      return measurements.get(type);
   }


Here is my junit exception from the last line of the code:
Quote:

junit.framework.AssertionFailedError: Measurements returned null for type: weight (lb)
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.assertTrue(Assert.java:20)
at com.knapptech.fitnesstracker.entities.UserEntityTest.testLoad(UserEntityTest.java:203)
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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
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.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at $Proxy0.invoke(Unknown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)


Why is this happening? How can I fix this?


Top
 Profile  
 
 Post subject: Re: PersistentMap.get returns null for key it has
PostPosted: Sun Oct 02, 2011 6:15 pm 
Newbie

Joined: Sun Sep 25, 2011 4:05 pm
Posts: 12
A workaround I found is this:

Code:
      user.setMeasurements(new Hashtable<MeasurementTypeEntity,MeasurementsEntity>(user.getMeasurements()));


basically I converted the hibernate map into a java map. This is not a great solution because it could induce performance lags, but at least it works now.


Top
 Profile  
 
 Post subject: Re: PersistentMap.get returns null for key it has
PostPosted: Mon Aug 27, 2012 5:44 pm 
Newbie

Joined: Fri Jul 22, 2011 5:04 pm
Posts: 8
Did you ever find out what was causing this? I'm having the same problem.


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.