• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ManyToOne on Child Entities Only

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,

Hoping somebody can review my approach and point me in the right/better direction.

I have two entities, Category and Subcategory. I want my UI to have nested options for each Category, where there may or may not be additional Subcategories.

This seemed like the simplest design initially that would do the job (additional fields and methods left out for brevity):


Problem is, I want to grab the JSON data from the Category down, not the other way around. In this scenario, the JSON data has a Category under each Subcategory (backwards), and so if there's a Category without a Subcategory you wouldn't see it.

Should I just add a Subcategory list to the Category? That seems like the most straightforward way to get it done, I tested and it works as expected when getting the JSON.


But say later, there are 2 or 3 different objects that need a foreign key reference to Category, I wouldn't want to keep having to modify the Category class.
With SQL the query would be something like:
SELECT * FROM X WHERE category_id=? ;

What's the best way to do this with Hibernate?

Thanks!

 
Saloon Keeper
Posts: 15561
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why not just add a @ManyToOne relationship in the new entities, instead of modifying category? Or do those entities need to be serialized as well when you write a category to JSON?

What's your reason for not wanting to modify the Category class, assuming you want to extract new information from it?
 
Saloon Keeper
Posts: 27819
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clean approach (recommended) is that Category contain a List property of Subcategory with a relationship of @OneToMany and that SubCategory contain a Category property pointing to the parent with relationship @ManyToOne. It's also important to note when doing this that the in-memory instances of Category and SubCategory be kept totally in sync with what you want to end up with in the database, as a partial or missing relationship can result in deletion of the target object(s).

I did a project once that allowed multiple levels of (sub)category. For that sort of organization a single Category class was sufficient. The root Category's parent was null.
 
Wayne Woodrow
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both for your input. Adding the @ManyToOne on the parent (Category) does work to retrieve a hierarchy of Categories as JSON. This is definitely what I want, see sample:

Both entities referencing each other:



As expcted:


I just thought it would be possible to get the same result, while leaving the parent Category like this:


Similar to how on the database, the parent doesn't need a column for the children, nor does it need to know how many other tables reference it.

As an experiment, I did try leaving the Category as you see above (without the reference to the CategorySub1), and instead did the work in the rest controller to organize data before returning it. It kind of worked, except that the Category wasn't (deserialized?) Would it be completely stupid to do it this way?


Also organized correctly, but isn't showing the Category details. I think this would be a simple fix but not sure how yet.

 
Tim Holloway
Saloon Keeper
Posts: 27819
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The neat thing about entity object classes is that the same class definition can represent JPA database table rows, JSON serial object and XML serial object (and likely other stuff I can't remember). You do need a few extra annotations to make the same definitions work on all platforms. I think you need some sort of root annotation for XML and JSON, for example. JPA would ignore it, but they need it.
reply
    Bookmark Topic Watch Topic
  • New Topic