How to get class and attribute names from a Path?

I have a javax.persistence.criteria.Path<X> field (obtained by from.get<T>(fieldName) somewhere else in the codebase) and I want to find corresponding class name and field name as strings. Currently I’m doing this:

        val parentClassName = field.parentPath.type().javaType.simpleName
        val fieldName = field.attribute.name

For that to be possible I need to cast the field to PathImplementor which is Hibernate specific, though. Is there any standard JPA way how to achieve that?

Path has a getParentPath() method through which you can obtain the From. Through that, you can figure out the entity class name. Path#getModel should return you the javax.persistence.metamodel.Attribute, which then exposes an attribute name.

I tried that before but Path#getModel returns just a Bindable which doesn’t seem to be useful for obtaining the name.

The returned object type from Path#getModel, when the Path is not a From is usually Attribute, so you can simply cast that.

That’s unfortunately not true for plural attributes (getModel returns just null).

That would be a bug then IMO.

Well, I can create an issue but are you sure about that? The implementation in PluralAttributePath is pretty specific

public Bindable<X> getModel() {
        return null;
    }

This doesn’t seem like an unintended omission…

Looks like a bug to me :slight_smile:
Usually we add a comment there to document why we return null e.g. at least a // no-op or something like that.