-->
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: Use Map<Entity, Integer> to map many-to-many relation
PostPosted: Sat Nov 05, 2016 2:38 pm 
Newbie

Joined: Sat Nov 05, 2016 2:22 pm
Posts: 2
Hi, all!

I've been struggling figuring out whether it is possible to map this relational model:
Image
https://drive.google.com/open?id=0B32rkhYCEyjuenpKWlFaTGczSlE

to an object model like this:
Image
https://drive.google.com/open?id=0B32rkhYCEyjuZ3VpYzRGcDl2Nk0

I wanna store in the Map<Dish, Integer> pairs of Dish/Quantity, that I will get from the joining table "dish_order".


I studied this user's guide..http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#collections-map but still haven't arrived at the solution.

I tried this, but keep getting errors of different kind:
Code:
        @ElementCollection
        @CollectionTable(name = "order_dish")
        @Column(name = "quantity")
        @MapKeyJoinColumn(name = "D_ID", referencedColumnName = "ORD_ID")
        @Fetch(FetchMode.JOIN)
        Map<Dish, Integer> dishes;


Top
 Profile  
 
 Post subject: Re: Use Map<Entity, Integer> to map many-to-many relation
PostPosted: Sun Nov 06, 2016 3:16 am 
Newbie

Joined: Sat Nov 05, 2016 2:22 pm
Posts: 2
I've found one solution:

Code:
        @ElementCollection(fetch = FetchType.EAGER)
        @CollectionTable(name = "order_dish",
                joinColumns = @JoinColumn(name = "ORD_ID"))
        @MapKeyJoinColumn(name = "D_ID")
        @Column(name = "quantity")
        Map<Dish, Integer> dishes;


But the question remains: is this approach accepted, elegant and contains no pitfalls?


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