How to select object's DiscriminatorColumn with Eclipse Link

I have an abstract Entity User and some inherited classes, e.g. Patient that use DiscriminatorColumn as an inheritance method.

@Entity
@DiscriminatorColumn(name = "USER_TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class User {
    ...
}

@Entity
@DiscriminatorValue("patient")
public class Patient extends User {
    ...
}

I need to know the object's type. I found a solution for Hibernate, but I use Eclipse Link and I want to write a script that retrieves the DiscriminatorColumn from database. I tried

em.createQuery("SELECT u.user_type FROM User u WHERE u.username = :username", String.class)
            .setParameter("username", username)
            .getSingleResult();

and

em.createQuery("SELECT u.class FROM User u WHERE u.username = :username", String.class)
            .setParameter("username", username)
            .getSingleResult();

but none is working for me and I'm getting IllegalArgumentException

The state field path 'u.class' cannot be resolved to a valid type.

Is it possible in Eclipe Link?

Upvotes: 0

Views: 534

Answers (1)

Thanks to Neil Stockton I found the solution by getting the class name, not the DiscriminatorValue, but it works good enough. Using TYPE(u) instead of u.class works fine.

em.createQuery("SELECT TYPE(u) FROM User u WHERE u.username = :username", Class.class)
            .setParameter("username", username)
            .getSingleResult().getSimpleName();

Upvotes: 1

Related Questions