Im not sure it has been reported (i sure couldnt find it), but weve found what we believe is an hibernate bug.
Code:
public class TestHibernate extends TestCase {
@Entity
public static class Label {
@Id
@GeneratedValue
long id;
String label = "";
@Override
public int hashCode() {
return this.label == null ? 0 : this.label.hashCode();
}
@Override
public boolean equals(Object obj) {
final Label other = (Label) obj;
return this.label == null ? other.label == null : this.label.equals(other.label);
}
}
@Entity
public static class Assoc {
@Id
@GeneratedValue
long id;
@ManyToOne(cascade = CascadeType.ALL)
Label label;
}
@Entity
public static class Container {
@Id
@GeneratedValue
long id;
@OneToMany(cascade = CascadeType.ALL)
@MapKey(name = "label")
Map<Label, Assoc> map = new HashMap<Label, Assoc>();
}
public void testSimple() throws Exception {
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.setProperty(Environment.DIALECT, HSQLDialect.class.getName());
String driver = jdbcDriver.class.getName();
cfg.setProperty(Environment.DRIVER, driver);
String url = "jdbc:hsqldb:target/test_db.model;shutdown=true;sql.enforce_strict_size=true";
cfg.setProperty(Environment.URL, url);
cfg.setProperty(Environment.USER, "sa");
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
cfg.addAnnotatedClass(Container.class);
cfg.addAnnotatedClass(Label.class);
cfg.addAnnotatedClass(Assoc.class);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Container c = new Container();
Label l = new Label();
l.label = "someLabel";
Assoc s = new Assoc();
s.label = l;
c.map.put(l, s);
session.save(c);
session.flush();
session.clear();
Container c1 = (Container) session.get(Container.class, c.id);
assertNotNull(c1);
assertNotNull(c1.map);
Assoc next = c1.map.values().iterator().next();
assertNotNull(next);
assertNotNull(next.label);
assertNotNull(next.label.label);
assertEquals(l.label, next.label.label);
assertNotNull(c1.map.get(next.label));
}
basically, i have an entity (Label) that is used to be a key on a map (in the container), but when the map is created (during retrieval of the owner of the map), the Label is not initialized so its hashcode gets wrongly computed (and its put on the wrong slot on the map's internal table) and it can never be found.
Anyone knows any workaround? should i JIRA it?
thanks in advance