Simplest one-to-many Map case in Hibernate doesn't work in MySQL

Posted by Malvolio on Stack Overflow See other posts from Stack Overflow or by Malvolio
Published on 2010-03-24T00:00:31Z Indexed on 2010/03/24 0:03 UTC
Read the original article Hit count: 195

Filed under:
|

I think this is pretty much the simplest case for mapping a Map (that is, an associative array) of entities.

@Entity
@AccessType("field")
class Member {
    @Id
    protected long id;

    @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    @MapKey(name = "name")
    private Map<String, Preferences> preferences
             = new HashMap<String, Preferences>(); 
}

@Entity
@AccessType("field")
class Preferences {
    @ManyToOne Member member;
    @Column String name;
    @Column String value;
}

This looks like it should work, and it does, in HSQL. In MySQL, there are two problems: First, it insists that there be a table called Members_Preferences, as if this were a many-to-many relationship.

Second, it just doesn't work: since it never populates Members_Preferences, it never retrieves the Preferences.

[My theory is, since I only use HSQL in memory-mode, it automatically creates Members_Preferences and never really has to retrieve the preferences map. In any case, either Hibernate has a huge bug in it or I'm doing something wrong.]

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about mysql