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