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: How should I map an inverted ElementCollection
PostPosted: Fri Jun 17, 2016 7:34 am 

Joined: Fri Jun 17, 2016 7:13 am
Posts: 1

Can anyone help me with a question regarding element collections? I am a bit of a noob having only used Hibernate for a few months, so apologies if I haven't understood this problem properly.

This is a product inventory system. Some products are considered to be 'consumables' for other products.
It returns data in JSON format. As there are many products and relations, I want to keep the payload of data quite low.
So when a product is a consumable, I just want to return a list of product ids it is a consumable for, not the full related product.

On this Product entity, there is a set of 'RelatedProduct'. This embeddable has a single string property called 'relatedId'.

So this element collection of productIds this product is a consumable for works fine:

   @CollectionTable(name="tbl_product_consumables", joinColumns={@JoinColumn(name="businessId"),@JoinColumn(name="id")})
        @AttributeOverride(name="businessId", column=@Column(name="BusinessId", table="tbl_product_consumables")),
        @AttributeOverride(name="id", column=@Column(name="Id", table="tbl_product_consumables")),
        @AttributeOverride(name="relatedId", column=@Column(name="RelatedId", table="tbl_product_consumables", nullable = false, length = "5"))
public Set<RelatedProduct> getIsConsumableFor() {
     return isConsumableFor;

So it works fine for one direction. But now I want to also get the product ids in the opposite direction too, so that I can find out product Ids that are consumables for this product.
My first thought was to add a second elementcollection, but swapping the columns for the Id and Related Id:

   @CollectionTable(name="tbl_product_consumables", joinColumns={@JoinColumn(name="businessId"),@JoinColumn(name="relatedId")})
        @AttributeOverride(name="businessId", column=@Column(name="BusinessId", table="tbl_product_consumables")),
        @AttributeOverride(name="id", column=@Column(name="RelatedId", table="tbl_product_consumables", nullable = false, length = "5")),
        @AttributeOverride(name="relatedId", column=@Column(name="Id", table="tbl_product_consumables"))
public Set<RelatedProduct> getConsumables() {
     return consumables;

This does actually work fine for retrieving data.
But I noticed a problem with the primary key.

If I include either one of these element collections on its own, a single primary key is generated containing businessId,Id and relatedId.
But when they are both included, a secondary key is also generated for businessId,Id

This means that I can only insert one record for each product.
I assumed they would attempt to generate and use the same primary key, as it is the same fields?

So can I stop it generating the second key? Or should this be done another way?


 Post subject: Re: ElementCollection inverted
PostPosted: Sat Jun 18, 2016 2:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
I would not use @ElementCollection here because if you remove a related Product from that Set the Hibernate is going to physically delete that record. Also, unidirectional associations such as element collections don't render the best SQL queries possible.

I suggest you use two @ManyToOne associations so that you link which product is related to other product, or use an additional info for retrieving the related products.

Relationships are better suited for materializing the FK info, not for querying. Actually, you should question if you really need this association anyway. Maybe a query is what you need in the first place. You should not use relationships as a substitute for queries.

If you define a Tag entity so that a Product can take multiple Tag(s), you can just create a query so that you retrieve all Products that have a certain Tag attached. This is much more flexible than a relationship.

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.