2

I extended domain model of new entity Auditor, which extends from entity User. User is old entity (database table), which has existed already. Now, if is executed some query for super class User (only), Hibernate appends left outer join clause on auditor's table too. Does anyone have an idea how to solve it?

User class

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "users")
@DiscriminatorColumn(length = 10, name = "discriminator", 
    discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("user")
@SequenceGenerator(name = "users_id_seq", 
    sequenceName = "users_id_seq", 
    initialValue = 1, allocationSize =1)
public class User extends AbstractEntity implements UserDetails{

    private static final long serialVersionUID = 198524L;

    private Long id;
    // ...
}

Auditor class

@Entity
@Table(name = "auditor")
@DiscriminatorValue("auditor")
public class Auditor extends User {

    private Country countery;
    // ...
}

Example of HQL query for User list

Query query = sessionFactory.getCurrentSession()
    .createQuery("select type(u) from User u");

return Collections.checkedList(query.list(), User.class);

Hibernate generats e.g.:

 select
        case 
            when user0_1_.id is not null then 1 
            when user0_.id is not null then 0 
        end as col_0_0_ 
    from
        users user0_ 
    left outer join
            auditor user0_1_ 
            on user0_.id=user0_1_.id 
  group by
        user0_.id 
order by
carbontax
  • 2,164
  • 23
  • 37
Peter Jurkovic
  • 2,686
  • 6
  • 36
  • 55
  • This seems to be related to [my question](https://stackoverflow.com/questions/49804053/psqlexception-the-column-name-clazz-was-not-found-in-this-resultset). How did you resolve this? – Stefan Falk Apr 12 '18 at 20:25

1 Answers1

1

This is disadvantage of the JOINED inheritance type and you cann't disable this feature. There are two workarounds:
1) Use TABLE_PER_CLASS inheritance type

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class User extends AbstractEntity implements UserDetails {  

2) use @MappedSuperclass instead of @Inheritance

@MappedSuperclass
public class User extends AbstractEntity implements UserDetails {
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • I used to tried TABLE_PER_CLASS, but instead of left outer join there was union all on auditor table. I integrated @DiscriminatorColumn too, but generated SQL is the same. – Peter Jurkovic Jun 10 '14 at 18:17
  • If you do something like `select u from Users u` do you get both Auditors and Users in your list of results? – carbontax Jun 12 '14 at 10:52
  • Both no, only Users attributes. But problem is that, Hibernate always append left outer join clause. It is problem in complex queries, where is used group by clause. And on the other hand, it is unnecessary overhead, too. – Peter Jurkovic Jun 12 '14 at 12:01
  • @PeterJurkovič Did you try `@MappedSuperclass`? – Ilya Jun 12 '14 at 13:53