Note that there are some explanatory texts on larger screens.

plurals
  1. POWorking with Collections and Hibernate
    primarykey
    data
    text
    <p>I got a problem, where I cant get the value out of an HashMap but I am sure, the key i want to get the value out with, is in the map (but not the same instance). So here is, what i have: I got a HiberKey, and a HiberVal.</p> <pre><code>public class HiberKey { private int id; } public class HiberVal { private int id; } </code></pre> <p>And while the programm is running, it does a transaction now and then, and at some point i got a Hashmap AND a single HiberKey single_key. The problem here is, that i wanted to execute map.get(single_key) i allways get a null value, but im shure, there is the same value in the map (not same reference, but also of an transaction from the hibernate-session). The first thing i thought of was equals &amp; hashcode, but those methodes are already implemented in HiberKey.</p> <p>A solution would be to fill the map with key.id instead of hiberkey and call map.get(single_key.id), but thats not the idea of hibernate i think, and i got quite more complicated sctructures with Hashmaps in maps and so on, and if i fill all of them with integer, its not really handy and readable any more</p> <p>Does anyone have a suggestion, why this could occour? Thanks for any advice.</p> <p>Hibernateversion: 3.5.3</p> <pre><code>@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (!(obj instanceof BabelKey)) { return false; } return ((BabelKey) obj).id == id; } @Override public int hashCode() { return String.valueOf(id).hashCode(); } </code></pre> <p><strong>// UPDATE:</strong></p> <p>Ok, here is an example where i iterate the map with additional out.println debugging, and in the output you can see the key is in the map, but simply does not work. output:</p> <pre><code>-------------------------- Following keys in Map: Keys with no null value: 301 //Key is In Map Keys with no null value: 304 Keys with no null value: 303 Keys with no null value: 307 Searched key: 301 //same key Result: null //result is null Next Try: null </code></pre> <p><strong>// UPDATE2:</strong></p> <p>I compared the two key files, and equals returns true, and they also have the same hashcode. But somehow Hibernate uses Javaassist, so different instances wont work out with a HashMap. I think the only solution for that, is to save the id in the hashmap and compare the id of them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload