12

I have a persistent object (Action) and auto generated data model (Action_). By having an object of Action class and an instance of SingularAttribute, is it possible to get the field corresponding to the given SingularAttribute?

I need a function like this:

public S getValue(T object,SingularAttribute<T,S> attribute);

My entity class (Action.java):

@Entity
@Table(name="ACTION")
public class Action implements Serializable {
    private long id;
    private String name;

    public Action() {
    }


    @Id
    @Column(unique=true, nullable=false, precision=6)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }


    @Column(length=50)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

My meta model class (Action_.java):

@StaticMetamodel(Action.class)
public class Action_ {
    public static volatile SingularAttribute<Action, Long> id;
    public static volatile SingularAttribute<Action, String> name;
}
1
  • JPA is not there to provide values of fields of an object, it is to persist/retrieve the object. As the reply says, use Java reflection. Aug 16, 2011 at 12:59

2 Answers 2

12

As JB Nizet suggested, you can use getJavaMember. I found that I didn't need to set private fields to be accessible, perhaps Hibernate has already done this.

In case this is helpful, here's some code which works for me:

/**
 * Fetches the value of the given SingularAttribute on the given
 * entity.
 *
 * @see http://stackoverflow.com/questions/7077464/how-to-get-singularattribute-mapped-value-of-a-persistent-object
 */
@SuppressWarnings("unchecked")
public static <EntityType,FieldType> FieldType getValue(EntityType entity, SingularAttribute<EntityType, FieldType> field) {
    try {
        Member member = field.getJavaMember();
        if (member instanceof Method) {
            // this should be a getter method:
            return (FieldType) ((Method)member).invoke(entity);
        } else if (member instanceof Field) {
            return (FieldType) ((Field)member).get(entity);
        } else {
            throw new IllegalArgumentException("Unexpected java member type. Expecting method or field, found: " + member);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
2
  • Nice solution. We have easily derived an EntityAttributeCollection so we are able to iterate over attributes of entities in an existing collection.
    – mazatwork
    Jun 4, 2012 at 17:53
  • This one is great! I can create easily a new column in JSF without adding to much code!
    – alexander
    Jun 19, 2016 at 19:49
6

You could use the getJavaMember() method to get the member, then test if this member is a Field or a Method, and access the field or call the method on the object using reflection.

You will probably have to make the field or method accessible before accessing/invoking it. And you will also have to handle primitive type conversion to objects.

The main question is: why do you need this?

If you need it only for this specific entity class, you could simply use a switch on the attribute name and return the appropriate value:

switch (attribute.getName()) {

    case "name":
        return action.getName();
    ...
}
2
  • Thanks for your quick response. I want to make a general class which should be able to work with any given persistent object(by using the meta model). For example a class for representing any table of database. Using the getJavaMember() function seems to be the only way but since all persistent objects should provide getter methods for their fields, I expect the API to provide the functionality to get all fields (Internally using the getter method if needed). Aug 16, 2011 at 12:28
  • It works. As I expected SingularAttribute return instances of Method or Field based on where the @Column is set. Thanks again! Aug 16, 2011 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.