0

I am using Hibernate inheritance with @Inheritance and would like to use @Subselect instead of @Table, like this:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Parent { ... }
@Entity
@Subselect("select a,b,c from entity_a")
public class EntityA extends Parent { ... }
@Entity
@Subselect("select a,b,c from entity_b")
public class EntityB extends Parent { ... }

This does not work and I get the error: org.postgresql.util.PSQLException: ERROR: subquery in FROM must have an alias

The error does not occur with the same @Subselect but without using inheritance.

I understand that the issue is that using inheritance leads to a query using union all, like this (simplified):

select *
from (select a, b, c
      from entity_a)
union all
select *
from (select a, b, c
      from entity_b)

I tried adding an alias in the @Subselect clause, but was only able to get syntax errors around the parentheses.

What I tried:

@Subselect("(select a,b,c from entity_a) as x")
@Subselect("(select a,b,c from entity_a) x")

I also read that Postgres 16 made subquery aliases optional, but unfortunately I have to use Postgres 13 for the time being.

Here a person made @Subselect and @Inheritance work, but I suppose they didn't use Postgres.

Is there a way to make this work anyway? My alternative would be to define the subselects as views and refer to them with @Table, but it would be nice if that wasn't necessary.

0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.