-->
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.  [ 2 posts ] 
Author Message
 Post subject: Error on maps with entities as keys
PostPosted: Thu Jul 17, 2008 2:43 pm 
Newbie

Joined: Fri Nov 10, 2006 2:47 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 18, 2008 2:26 pm 
Newbie

Joined: Fri Nov 10, 2006 2:47 pm
Posts: 2
anyone? please? a confirmation on the bug would be enougn for me - id gladly JIRA it...


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