Find by subclass on an inner join query with JpaRepository

Viewed 20

I have an entity called Event:

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "events")
@EntityListeners(AuditingEntityListener::class)
abstract class Event(
    open var name: String,

    open var startDate: Date,
    ...
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    open var id: Long = 0
}

and the following subclass:

@Entity
@Table(name = "event_activities")
@EntityListeners(AuditingEntityListener::class)
class EventActivity(
    name:String,

    startDate: Date,

...

    ): Event(name, startDate...) {}

that have a relation with this entity:

@Entity
@Table(name = "event_participants")
@EntityListeners(AuditingEntityListener::class)
class EventParticipant (
    @OneToOne(fetch = FetchType.LAZY)
    var event: Event? = null,

    @OneToOne(fetch = FetchType.LAZY)
    var user: User? = null
)
...

I want to obtain all EventParticipant from a specific user that have events of type EventActivity, so I implemented this query:

@Query("SELECT e " +
            "FROM EventParticipant e INNER JOIN EventActivity e2 " +
            "ON e.user.id = :user AND e.event.id = e2.id")
    fun findPendingActivityInvitations(@Param("user")id: Long): List<EventParticipant>?

The problem is that I am obtaining all types of Event and not just EventActivity that is what I want to obtain.

How can I solve this problem? Of course, I can't modify any entity

0 Answers
Related