-->
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: Hibernate 3 & annotations: How to map multilevel inherit
PostPosted: Wed Jan 14, 2009 9:33 am 
Newbie

Joined: Wed Jan 14, 2009 8:21 am
Posts: 4
Hello,
I have been reading as many posts I could on the subject but I can't find the solution to this (apparently) simple problem.
I'd also like to add before going for the explanation that the information given on the reference manuals on the subject is always very limited since it is always said that using different startegies on different levels is not a problem.

So, for the example.

[Level 1] I have a ManufacturedProduct class, containing some generic properties that children will use and linked to other entities (like Manufacturer). Class is abstract
[Level 2] ManufacturedProduct is subclassed by 2 classes: Part and Assembly. Both of these classes are abstract as well.
[Level 3] Assembly is subclassed by DefaultAssembly, containing some properties.
[Level 3] Part is subclassed by multiple classes each defining a type of part ("Armor" and "Coupling" classes in this example)

At level 2, Assembly will use the "JOINED" inheritance strategy since the subclasses can potentially contain multiple properties.
At level 2 still, Part defines a "SINGLE_TABLE" inheritance strategy since I am mostly interested in the type of the part and each type contains a really small amount of properties (if any).


With the code now, here is how I defined everything:
Code:
@Entity
  @Table(name="ManufacturedProducts")
  @Inheritance(strategy=InheritanceType.JOINED)
  public abstract class ManufacturedProduct {
  ...

    private Long id;
   
    @GeneratedValue
    public Long getId() {
      return id;
    }
  ...
  }


Code:
@Entity
  @Inheritance(strategy = InheritanceType.JOINED)
  public abstract class Assembly extends ManufacturedProduct{

  }



Code:
@Entity
  public class DefaultAssembly extends Assembly {

  @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
  @DiscriminatorColumn(name="PART_TYPE", discriminatorType=DiscriminatorType.STRING)
  public abstract class Part extends ManufacturedProduct{
   
  }



Code:
@Entity
  @DiscriminatorValue("Armor")
  public class Armor extends Part {

  }



Code:
@Entity
  @DiscriminatorValue("Coupling")
  public class Coupling extends Part {

  }



Ok, so as you can see, at one given level in the hierarchy, each subclass uses the same strategy but, on the big picture, 2 different strategies are used.
The problem is that the "Part" part of the model (the one with the SINGLE_TABLE strategy) is not mapped correctly. A table is still made for each and every level 3 subclass here, as if the inheritance-related annotations were simply forgotten. I do not receive any error when generating the schema but what gets created is just not what should be.

As I said, I have checked multiple posts on the subject and never found a solution.
Note that somebody said something about just not adding the Level 2 classes in the hibernate.cfg.xml file since they are abestract or somehting but it doesn't change anything to me (plus the examples in the reference and books I read on the subject do not say anything about it).

I also tried to remove the @Entity annotation on the Level2 classes, forcing the table names and so on but nothing works fine or seems like a hack that will never work once an actual persist of fetch will be performed.



Could you then explain me how to do that? Don't hesitate to go into the details and explain why mappings have to be done in the way you say it so that we can really understand and not make the mistake anymore.

Thank you VERY MUCH in advance,

Jérôme


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 11:28 am 
Newbie

Joined: Wed Jan 14, 2009 8:21 am
Posts: 4
Note that we broke the inheritance between assembly & part to their common ancestor (ManufacturedProduct) and added a composition instead.

This does the trink but does not answer the original question.
We somehow can't accept the fact that it's not possible.

Don't hesitate posting even just to give us pointers as to where to search for more information about this specific subject if you have some.

Jer


Top
 Profile  
 
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.