-->
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.  [ 8 posts ] 
Author Message
 Post subject: Mapping complex components (value objects)
PostPosted: Tue Mar 07, 2017 10:50 pm 
Newbie

Joined: Sat Aug 27, 2016 11:17 am
Posts: 12
We have the following simplified domain model:

Image

How shall we map the title and description properties of the Question entity given the referenced TranslatedText values should have composite semantics?

Thanks!


Top
 Profile  
 
 Post subject: Re: Mapping complex components (value objects)
PostPosted: Wed Mar 08, 2017 5:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
What have you tried so far?


Top
 Profile  
 
 Post subject: Re: Mapping complex components (value objects)
PostPosted: Fri Mar 10, 2017 12:17 am 
Newbie

Joined: Sat Aug 27, 2016 11:17 am
Posts: 12
vlad wrote:
What have you tried so far?


Well, I tried a few things:

- Map TranslatedText as a component within Question and then map Translation with a one-to-many within the component.

Code:
<component name="title" class="TranslatedText">
    <bag name="translations" table="Translation" cascade="all-delete-orphan">
        <key>
       <column name="entity_id"/>
   </key>

   <one-to-many class="Translation"/>
    </bag>
</component>


The problem with this approach is that I did not found a way to add an additional property_name column (which value would hard-code to 'title' in this case) to form a composite foreign key. The foreign key can't only be entity_id because Question will have several TranslatedText properties. I tried to add an additional <column name="property_name" default="'title'"/> within <key>, but it failed with this error so I guess that Hibernate can only map the association with the parent entity ID:

Foreign key (FKF5B39A91131961AF:Translation [entity_id,property_name])) Translation [entity_id,property_name])) must have same number of columns as the referenced primary key (Question [id])

- I also tried the "typed" one-to-one association discussed in here https://github.com/hibernate/hibernate-orm/tree/master/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula, but from what I understood it requires the value object to have a reference back to it's parent which makes no sense at all: it's like asking to put a parent object reference into a Date or an Integer.

I'm pretty sure I overlooked something, but I just cannot find a way to map the current model. Hopefully there is one way to do it and if there is is one we also need to make sure that the Question entity owns the relationship because I want value semantics so that the Question's concurrency version gets incremented if we change it's title or description.


Top
 Profile  
 
 Post subject: Re: Mapping complex components (value objects)
PostPosted: Fri Mar 10, 2017 12:07 pm 
Newbie

Joined: Sat Aug 27, 2016 11:17 am
Posts: 12
I managed to get something close to what I want by mapping TranslatedText using a many-to-one within Question. It works well, but I'll have to implement a custom solution to deal with orphan removal.

Ideally I'd map everything as dependend objects (components). The only thing that prevents me from doing it is that I could not find a way to properly map the translations because we need some kind of discriminator in there that is not part of the model and must be used to form a composite foreign key.
Code:
<component name="title" class="TranslatedText">
         
         
         <bag name="translations" table="Translation">
            <key>
               <column="question_id" not-null="true">
               <!--
                                         We are missing a discriminator that would be the 'title' hard-coded string in
                                         here and that value cannot come from the model.
                                         -->
            </key>
            
            <composite-element class="Translation">
               <property name="languageCode" type="string" column="language_code"/>
               <property name="text" type="string"/>
            </composite-element>
         </bag>
         
      </component>


Adding a type property to TranslatedText would probably work, but it would make the model quite ugly, having to do:

Code:
title = new TranslatedText('title', ...);
description = new TranslatedText('description', ...);


We could abstract that by creating concrete `Title` and `Description` classes, but that may potentially lead to an explosion of classes.

EDIT: I couldn't find a way to map everything as dependent objects, even with concrete distinct Title and Description classes.


Top
 Profile  
 
 Post subject: Re: Mapping complex components (value objects)
PostPosted: Sun Mar 12, 2017 2:59 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Why don't you use annotations? They are better supported than legacy HBM mappings.


Top
 Profile  
 
 Post subject: Re: Mapping complex components (value objects)
PostPosted: Sun Mar 12, 2017 3:38 pm 
Newbie

Joined: Sat Aug 27, 2016 11:17 am
Posts: 12
I thought XML mapping files were equally powerful to annotations? How would you solve this problem with annotations? TBH I really dislike annotations as they pollute the model with persistence concerns, mixing up levels of abstraction.


Top
 Profile  
 
 Post subject: Re: Mapping complex components (value objects)
PostPosted: Mon Mar 13, 2017 2:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The legacy HBM mappings are less powerful than Annotations, and there might be features that are not very well supported by the legacy mappings. In Hibernate 6, we plan on adding custom extensions to the JPA mappings, so you can also use the XML mappings if you like, but also benefit from Hibernate-specific features.


Top
 Profile  
 
 Post subject: Re: Mapping complex components (value objects)
PostPosted: Mon Mar 13, 2017 8:15 am 
Newbie

Joined: Sat Aug 27, 2016 11:17 am
Posts: 12
That's great news, but that still doesn't answer my question. Perhaps the question is better explained on StackOverflow.

http://stackoverflow.com/questions/4273 ... -hibernate


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