3 Replies Latest reply on Apr 29, 2007 4:04 PM by fhh

    In relation to Entity hierarchy and EntityManager's behaviou

    pyctam

      I found a strange behaviour of EntityManager for the Entity Hierarchy using JOINED strategy (I have not tested yet other strategies).

      Let me assume that I have a hierarchy of joined entities derived from, for example, a Vehicle base class:

       Vehicle
       / | \
       / | \
       / | \
      Ground Air Water
       / | \
       / | \
       / | \
      Airbus Boeing Cessna
       / | \
       / | \
       / | \
       B737 B747 B777
      


      When I'm trying to remove a Vehicle entity, entity manager removes all related child records from db (Vehicle->Air->Boeing->B747). It's OK.

      When I'm trying to remove a B747 entity, entity manager also removes all ancestor records from db (including Boeing, Air and Vehicle), but I don't need it :-(. I just tried to remove only B747 record, why entity manager supposed that I need to remove all ancestors too? I think that this approach is not right. Because, when I want to remove a B747 record only from db (for example an ailiner with registration number XXXXXX), I didn't want to remove all related ancestors include Vehicle.

      When I want to remove just Boeing related records from my db, it means that I just wanted to remove only Boeing records including all its childs (B737, B747 and B777), but not its anchestors (Air and Vehicle).


      if you think that my thougts on this subject is not on right way or do not cover OOP concepts, please suggest me some resources and will try to study them to understand inheritance from yours point of view.


      --
      rustam bogubaev

        • 1. Re: In relation to Entity hierarchy and EntityManager's beha

          I think this is a misunderstanding but it is difficult to explain.

          The JOINED strategy does not directly relate to the class hierachy because the class hierachy is about classes and their relationships while the persitence api deals with instances of classes. Therefore entities cannot "share" the rows in the classes of tables they have inherited.

          Maybe an example makes it easier to understand: Imagine the (abstract) class Vehicle had a property license_number that is inherited up to the Boieng 747. It is not sufficient to have one license plate number for all Vehicles (it is not a static property), so you have to have one row in the vehicle table for every vehicle.

          If you want to have one entry in the parent table for all the children use ManyToOne on the "child" instead of inheritance.

          Regards

          Felix

          • 2. Re: In relation to Entity hierarchy and EntityManager's beha
            pyctam

            Thanks for your response Felix,

            You earned me a new point of view to Entity Hierarchy in EJB3. I studied today this subject and found that JOINED strategy is an alternative way for entities annotated with @SecondaryTable or used @OneToOne relationship. You just need to derive a class from its parent and you will have an acces to all attributes from secondary table or from table with one-to-one ralationship in single entity. But I still can say nothing about differences between removing entities implemented hierarchy and @SecondaryTable or @OneToOne annotations.

            --
            rustam bogubaev

            • 3. Re: In relation to Entity hierarchy and EntityManager's beha

              Yes, but you will still have a row per instance in the parent tables not per class. So same problem.

              Regards

              Felix