-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hierarchical Entity mapping issue
PostPosted: Fri Feb 24, 2017 2:28 pm 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
I have a hierarchical mapping problem. Conceptually 3 elements

1. Product Definition -- high level definition about a product . A product can be STANDARD or COMPOSITE . A standard product has a list of specifications. Where as, a composite product consists of list of 2 or more intermediate products . One product can be part of one or more COMPOSITE . Hence Product Definition may be optionally hierarchical with many-to-many association to itself. One Product (STANDARD) can be part of multiple COMPOSITE products and COMPOSITE products may have 1 or more child products.

[Constraints : I have to keep both type of products as same Entity class. Ultimately going to be published as web services. So a single definitions / schema is preferred.]

2. Product Specification -- Detailed specification about product


3. Product Composition -- if a product is constituted by 1 or more intermediate products, this maintains the relationship. Composition has some other attributes too.

I have tried to following:

Code:
class ProductDefinition {

   @ID
   private Long id;
   
   @Column(name="product_code")
   private String productCode;
   
   @Column(name="product_type")
   private String productType; // STANDARD, COMPOSITE

   @OneToMany(mappedBy="productDefinitionRef", cascade={CascadeType.ALL})
   private Set<ProductSpec> productSpecs;
   
   @OneToMany(mappedBy="childProductRef", cascade={CascadeType.ALL})
   private Set<ProductComposition> compositionRefs; // For a standard product, lists the composites where the product belongs to
   
   @OneToMany(mappedBy="parentProductRef", cascade={CascadeType.ALL})
   private Set<ProductComposition> childProducts;  // For a composite product, lists the proucts which are part of the composite
   
   
   
   // ... other attributes
}


class ProductComposition {

   @ID
   private Long id;
   
   private ProductDefinition
   
   @ManyToOne
   @JoinColumn(name="parent_prod_ref")
   private ProductDefinition parentProductRef;
   
   @ManyToOne
   @JoinColumn(name="child_prod_ref")
   private ProductDefinition childProductRef;
   
   // ... Other attributes
}

class ProductSpec {

   @ID
   private Long id;
   
   
   @ManyToOne
   @JoinColumn(name="prod_def_ref")
   private ProductDefinition productDefinitionRef;

   // .. other attributes
   
}


Some sample data which I am expecting from the above model :

Product Definition
------------------

ID | Code | Type | productSpecs | compositionRefs | childProducts


1001 | P1 | STANDARD | [20001] | Empty | Empty
1002 | P2 | STANDARD | [20002] | [2001] | Empty
1003 | P3 | STANDARD | [20003] | [2001, 2002] | Empty
1004 | P4 | STANDARD | [20004] | Empty | Empty


2001 | B1 | COMPOSITE | Empty | Empty | [1002]

2002 | B2 | COMPOSITE | Empty | Empty | [1002, 1003]


Product Composition
-------------------

ID | parentProductRef | childProductRef

30005 | 2001 | 1002
30006 | 2002 | 1002
30007 | 2002 | 1003

Questions:
-----------
#1. Is this is an optimal way of mapping ? Please suggest if we can achieve anything better .
#2. Is this way of referencing the same composition entity twice within the product entity supported ? I am hitting some inconsistency when I see the data in the DB but before I discuss that, I want to make sure that atleast in theory this is feasible.


Top
 Profile  
 
 Post subject: Re: Hierarchical Entity mapping issue
PostPosted: Mon Feb 27, 2017 11:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I think the ProductComposition should define what a product is made of, while in your case a ProductDefinition can have multiple Compositions.

So, I'll change it so that Product only contains info about product details. You can have an AbstractProduct with two child entities: StandardProduct and CompositeProduct. StandardProduct only defines one product specification while CompositeProduct can take a List of Product definitions.


Top
 Profile  
 
 Post subject: Re: Hierarchical Entity mapping issue
PostPosted: Mon Feb 27, 2017 4:02 pm 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Thanks Vlad for responding. I shall try to implement your suggestion. Before I do that, let me describe the issue I am facing while trying to persist some data using the mapping I described.

I have tried to persist some sample data into mongo. I tried to add 2 STANDARD products and one COMPOSITE product ( id 7980 ), which comprises of only one STANDARD product (id 7975).

Code:
> db.ut_product_definitions.find()
{ "_id" : NumberLong(7971), "product_type" : "STANDRD", "product_code" : "PROD_3fe6530f4fe3e223", "productSpecs" : [ NumberLong(7973), NumberLong(7972) ] }
{ "_id" : NumberLong(7975), "product_type" : "STANDRD", "product_code" : "PROD_3fb59a336d2663b8", "productSpecs" : [ NumberLong(7977), NumberLong(7976) ], "childProducts" : [ NumberLong(7980) ] }
{ "_id" : NumberLong(7979), "product_type" : "COMPOSITE", "product_code" : "PROD_3fee5ee0bc59092b", "childProducts" : [ NumberLong(7980) ] }
>

> db.ut_product_compositions.find()
{ "_id" : NumberLong(7980), "parent_prod_ref" : NumberLong(7979), "child_prod_ref" : NumberLong(7975) }
>


Notice that in case of STANDARD product (product id 7975) the "childProducts" should be empty and "compositionRefs" should have a value pointing to composition with id 7980. I cross checked multiple times that I have set the compositionRefs for the standard product with id 7975. However when data is persisted, for some reasons, it only affects childProducts and not compositionRefs.

I feel even after implementing your suggestions I might run into this issue, while I try to keep track of the list of composite products where a standard product belongs to.


Top
 Profile  
 
 Post subject: Re: Hierarchical Entity mapping issue
PostPosted: Tue Feb 28, 2017 3:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Maybe we should move this thread to the OGM forum. I assumed you were using a RDBMS.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.