Home » Java » java – Spring Data: ignore join table on inherited entities-Exceptionshub

java – Spring Data: ignore join table on inherited entities-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am currently building an Spring Boot application using Hibernate, I this Entity inheritance like the following chart

        Account
          / \
     Admin   AdvancedAccount
                  / \
             Type1   Type2

Account class:

@Entity
@Table(name = "accounts")
@Inheritance(strategy = InheritanceType.JOINED)
public class Account {
    // attributes and methods        
}

Admin class:

@Entity
@Table(name = "admins")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "id")
public class Admin extends Account {
    // attributes and methods
}

AdvancedAccount class:

@Entity
@Table(name = "advanced_accounts")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "id")
public class AdvancedAccount extends Account {
    // attributes and methods
}

Type1 class:

@Entity
@Table(name = "type1s")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "id")
public class Type1 extends AdvancedAccount {
    // attributes and methods
}

Type2 class:

@Entity
@Table(name = "type2s")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "id")
public class Type2 extends AdvancedAccount {
    // attributes and methods
}

I am fully aware that once using InheritanceType.JOINED, Hibernate will execute join table queries on queries that involve any of these tables.

What I am trying to ask here is there any way that I can make Hibernate to not join every inherited tables into the query in some specific scenarios?

Thank you guys!

How to&Answers: