5

I have some JPA classes and generate metamodel through org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor. So, one of my classes is:

@Table(name = "USER")
@Entity
@NamedQueries({@NamedQuery(name = "User.byLogin", query = "select u from User u where u.login = :login and u.active = :active")})
public class User implements Serializable {
  @Column(name = "ID")
  @Id
  private Long id;
  @Column(name = "LOGIN")
  private String login;
  @Column(name = "ACTIVE")
  private Boolean active;
  // etc..
}

Metamodel processor generates this:

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(User.class)
public abstract class User_ {

    public static volatile SingularAttribute<User, Long> id;
    public static volatile SingularAttribute<User, Boolean> active;
    public static volatile SingularAttribute<User, String> login;

}

Then, there is the following code in my business logic classes:

Map<String, Object> params = new HashMap<String, Object>();
params.put(User_.login.getName(), username);
params.put(User_.active.getName(), Boolean.TRUE);
userDao.executeNamedQuery("User.byLogin", params);

This code crashes with NPE on at the second line. I noticed through debugger that User_ fields are all null. So, the question is: is there a way to initialize these fields? How can i do that?

P.S. This is a legacy code, it worked fine for long time, but now it seems to be broken somehow.

Everv0id
  • 1,862
  • 3
  • 25
  • 47
  • JPA metamodel is meant to be used only in Criteria queries as explained [here](https://docs.oracle.com/javaee/6/tutorial/doc/gjrij.html). I am not sure whether it can be used in the parameters construction of a named query at all. Also note that the usage is completely different from yours – perissf Apr 20 '16 at 13:50
  • @perissf i understand. The only thing i dont understand is that it worked somehow before. So there are big amounts of similar legacy code, and i wonder if there a way to fix it without rewriting all of that. – Everv0id Apr 20 '16 at 13:57
  • Does this answer your question? [Static Metamodel attributes null when unit testing](https://stackoverflow.com/questions/36225377/static-metamodel-attributes-null-when-unit-testing) – Vladislav Varslavans May 26 '23 at 09:04
  • @VladislavVarslavans Sorry, I cannot confirm - I don't work with JPA anymore. – Everv0id May 31 '23 at 13:49

1 Answers1

0

Parameter values of JPA metamodel are supposed to be null. From what I get it is impossible to get string containing name from User_.login.getName() because User_.login does not exist (is never initialized). User_ is an abstract class, and those values cannot be initialized. The only way to get variable names from metamodel is to get all of them. I'm using following program

public class EntityReader {
    public static<T> String[] getArguments(Class<T> classToRead){
        Field[] fields = classToRead.getFields();
        String[] result = new String[fields.length];
        for (int i = 0; i < fields.length; i++) result[i] = fields[i].getName();
        return result;
    }
}
Karol Pokomeda
  • 133
  • 2
  • 13