-->
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.  [ 1 post ] 
Author Message
 Post subject: Persistence of Map not working with GeneratedValue for key?
PostPosted: Fri Mar 27, 2009 12:56 am 
Newbie

Joined: Fri Mar 27, 2009 12:41 am
Posts: 1
I have two classes, each containing a Map<String,String>, which I want to persist. One like this works:

Code:
@Entity(name = "foo")
public class Foo implements Serializable {

    @Id
    @Column(name = "id")
    private String id;

    @CollectionOfElements(fetch=FetchType.EAGER)
    @JoinTable( name = "foo_map", joinColumns = {@JoinColumn(name = "foo_id")})
    @Column( name = "value", nullable = false )
    @org.hibernate.annotations.MapKey( columns={ @Column(name="name" ) } )
    private Map<String, String> map = new HashMap<String, String>();

}


i.e. I can create an object, populate its map, and then call entityManager.persist() on the object, and the object and the contents of the map are correctly persisted to the tables "foo" and "foo_map".


Now, if I do exactly the same thing on this class:
Code:
@Entity(name = "bar")
public class Bar implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;


    @CollectionOfElements(fetch = FetchType.EAGER)
    @JoinTable( name = "bar_map", joinColumns = {@JoinColumn(name = "bar_id")})
    @Column( name = "value", nullable = false )
    @org.hibernate.annotations.MapKey( columns={ @Column(name="name" ) } )
    private Map<String, String> map = new HashMap<String, String>();


}


persistence of the map simply does not work. The object itself is persisted (to the "bar" table) but the contents of the map are never persisted.

If I compare the logs, in the working example I see an insert into the "foo" table followed by inserts into the "foo_map" table. In the nonworking example there's an insert for the "bar" table, but no inserts for the "bar_map" table.

The bar_map table is created correctly by Hibernate, and retrieving objects shows me queries on both the bar and bar_map tables.

The only obvious difference I can see is that the Bar class has an autogenerated int id where the Foo class has an assigned string one.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.