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: mapping manytomany with a map
PostPosted: Mon Dec 07, 2015 5:24 pm 

Joined: Mon Dec 07, 2015 5:05 pm
Posts: 1
When mapping a ternary association with a Map, Map.Entry value cannot be null. Why is that, and can it be changed ?

Say we have a ternary association mapped with a java.util.Map as described in 'Java Persistence With Hibernate' (looking from a perspective of a Category):

   joinColumns = @JoinColumn(name = "ITEM_ID")
  name = "CATEGORY_ITEM",
  joinColumns = @JoinColumn(name = "CATEGORY_ID"),
  inverseJoinColumns = @JoinColumn(name = "USER_ID")
private Map<Item,User> itemsAndUser = new HashMap<Item,User>();

If we wanted to persist a map entry with a null User, that would not be possible.
I tracked down the source of this to this method in PersistentMap class:
   public boolean entryExists(Object entry, int i) {
      return ( (Map.Entry) entry ).getValue() != null;

which apparently checks if the map entry value is null, and if it is, Hibernate does not persist the entry.
Now I guess this can be circumvented implementing a custom UserCollectionType that would wrap the map in a PersistentMap subclass that would remove the checking.
However I am interested in the logic of this - why can't the PersistentMap let a null-value Map.Entry be persisted ?

Thanks a lot in advance



I think I found another way to circumvent this obstacle.

I created a helper @Embeddable class for holding the User value , let's call it UserEmbeddable:

public class UserEmbeddable

   private User User;

... setters & getters...

Now in Category the map looks like this:

   name="ITEM_ID", nullable=false
  name = "CATEGORY_ITEM",
  joinColumns = @JoinColumn(name = "CATEGORY_ID")
private Map<Item,UserEmbeddable> itemsAndUser = new HashMap<Item,UserEmbeddable>();

Hibernate generates a table like this:
create table CATEGORY_ITEM (
        CATEGORY_ID varchar(36) not null,
        USER_ID varchar(36),
        ITEM_ID varchar(36) not null,
        primary key (CATEGORY_ID, ITEM_ID)

and successfully inserts the row into this join table.

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