ChrisGeo
ChrisGeo

Reputation: 3907

Spring Data - Inheritance Search by Type

Lets assume we have the entities below:

@Entity
@DiscriminatorColumn(name="Type")
@Inheritance(strategy=InheritanceType.Joined)
public abstract class Employee extends Persistable<Long> {

    private String firstName;
    private String lastName;

    //getters, setters
}

@Entity
@DiscriminatorValue("S")
public class SalariedEmployee extends Employee {
            //various fields
}

@Entity
@DiscriminatorValue("C")
public class ContractEmployee extends Employee {
            //various fields
}

@Entity
@DiscriminatorValue("H")
public class HourlyEmployee extends Employee {
            //various fields
}

And a Repository

public interface EmployeeRepository implements PagingAndSortingRepository<Employee, Long> {
}

How can I search do a query lets say "Bring me all the Employees that are not HourlyEmployees". Obviously I would require these to be Paged/Sorted so I probably cant do findAll(); and just remove the entries from the Page collection if I'm not mistaken.

Upvotes: 1

Views: 976

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

See the JPQL Type Operator:

http://en.wikibooks.org/wiki/Java_Persistence/JPQL#JPQL_special_operators

The JPA 2.0 spec (section 4.6.17.4) gives the following examples of usage:

SELECT e
FROM Employee e
WHERE TYPE(e) IN (Exempt, Contractor)

SELECT e
FROM Employee e
WHERE TYPE(e) IN (:empType1, :empType2)

SELECT e
FROM Employee e
WHERE TYPE(e) IN :empTypes

SELECT TYPE(e)
FROM Employee e
WHERE TYPE(e) <> Exempt

Upvotes: 2

Related Questions