I’m using Hibernate with @OneToOne on a polymorphic base class and unfortunately it generates random left outer joins with other sub tables of SuperClass apart from the actual sub class table. I’m only querying CalledClass, which itself is connected via bi-directional OneToMany to another class. SuperClass has a lot of tables which inherit from it, they only share the id field and nothing else. How can I prevent hibernate from generating left outer joins over the Child Tables of SuperClass when querying CalledClass? Is it possible to deduce the right class type of SuperClass when querying CalledClass and only joining this class table?
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "BlockType")
public abstract class SuperClass<E, T extends SomeClass> {
@Id
@GeneratedValue()
long id;
}
Not sure which Hibernate version you use, but you could try mapping some attributes as @ManyToOne. You have to be aware that Hibernate has to fetch inverse one-to-ones as it’s impossible to determine if it needs to set null or a proxy for an association otherwise. By using a many-to-one mapping it will always set a proxy, but when accessing that, it could happen that you get an exception if the object does not exist
Hi thank you for the reply,
So you mean the left joins over the super classes can be avoided by adding @ManyToOne relationships to SuperClass?
What I’m trying to do basically is loading CalledClass together with a subclass ofSuperClass where I’m only selecting from the table of the subclass and not joining on all subclasses of SuperClass.
The problem is basically to determine the type of the subclass instance that is saved and selecting only from the associated table and not the other ones. Unfortunately the discriminator column seems to be ignored in this case.
My last resort would be somehow to stitch it together manually by removing the @OneToOne on SuperClass and saving the actual type of the subclass of SuperClass into the CalledClass and querying manually with knowledge of the type the correct table.
In my view the correct behaviour of Hibernate should be to have an option such that it is possible to have a lazy @OneToOne on a superclass to query the type from the discriminator column, to make the call to the correct table and not have all the joins? In our case we will probably have an inheritance with more than 100 tables in the future, which would be incredibly inefficient.
P.S. I’m using Hibernate 5.4.23.Final within Spring Boot JPA Starter 2.4.0
Omitting the super class table joins is a tough topic and I think Hibernate should be able to omit it in your case, but you need to write a custom query that touches none of the superclass attributes. Something like the following select c.id, cont.subclassAttribute from CalledClass c join c.content cont should avoid the superclass table join. If you join fetch the association or touch any of the attributes of the super class table Hibernate needs to create the joins.