java - Hibernate/JPA - NullPointerException when accessing SingularAttribute parameter -


i'm trying use jpa2 type-safe criteria queries hibernate 5.0.7.final.

... criteria.where( builder.equal( root.get(singularattribute.attr), value )); //where parameters //criteria.where( builder.equal( root.get(person_.name), "can" )); ... 

the root.get throw nullpointerexception. metamodel class person_ person generated org.hibernate.jpamodelgen.jpametamodelentityprocessor.

a similar problem asked in jpa/hibernate static metamodel attributes not populated -- nullpointerexception time both classes in same package.

stack trace:

java.lang.nullpointerexception @ org.hibernate.jpa.criteria.path.abstractpathimpl.get(abstractpathimpl.java:123) 

my code:

interface use make sure have getid();.

package it.unibz.db.hibernate.model;  public interface modelinterface<pk extends serializable> extends serializable {     pk getid(); } 

model class

package it.unibz.db.hibernate.model;  @entity @table(name ="person") public class person implements modelinterface<integer> {     @id     private integer id;     private string name;      public integer getid() {         return id;     }     //other getter , setters } 

generated metamodel person_ class

package it.unibz.db.hibernate.model;  @generated(value = "org.hibernate.jpamodelgen.jpametamodelentityprocessor") @staticmetamodel(person.class) public abstract class person_ {      public static volatile singularattribute<person, string> name;     public static volatile singularattribute<person, integer> id;  } 

generic dao class inherit persondao

public class genericdao<e extends modelinterface<pk>, pk extends serializable> implements daointerface<e, pk> { private class<e> type;  public genericdao(class<e> type) {     this.type = type;     //called super(classname.class); eg. super(person.class); }  public list<e> readby(singularattribute column, string value) throws exception {     entitymanager em = hibernateutil.getentitymanager();     criteriabuilder builder = em.getcriteriabuilder();     criteriaquery<e> criteria = builder.createquery(type);     root<e> root = criteria.from(type);      criteria.select(root);     criteria.where( builder.equal( root.get(column), value ));     list<e> entitylist = em.createquery(criteria).getresultlist();     em.close();     return entitylist;     } } 

some of dependencies

  • hibernate-c3p0 5.0.7.final

  • hibernate-entitymanager 5.0.7.final

  • postgresql 9.4.1207.jre7

  • hibernate-jpamodelgen 5.0.7.final

edit:

running code in main method works

entitymanager em = hibernateutil.getentitymanager(); criteriabuilder builder = em.getcriteriabuilder(); criteriaquery<person> criteria = builder.createquery(person.class); root<person> root = criteria.from(person.class); criteria.select(root); criteria.where( builder.equal( root.get(person_.name), "can" )); list<person> entitylist = em.createquery(criteria).getresultlist(); //do stuff entitylist 

but same code covered in method throws nullpointerexception.

public list<person> readby(singularattribute column, string value) throws exception {     log.debug("reading entity by");      entitymanager em = hibernateutil.getentitymanager();     criteriabuilder builder = em.getcriteriabuilder();     criteriaquery<person> criteria = builder.createquery(person.class);     root<person> root = criteria.from(person.class);      criteria.select(root);     criteria.where( builder.equal( root.get(column), value ));     list<person> entitylist = em.createquery(criteria).getresultlist();     em.close();     return entitylist; } 

so seems problem passing singularattribute parameter method readby(singularattribute column, string value).

i tested code in main , prints false

entitymanager em = hibernateutil.getentitymanager(); criteriabuilder builder = em.getcriteriabuilder(); criteriaquery<person> criteria = builder.createquery(person.class); root<person> root = criteria.from(person.class); system.out.println(root.get(person_.name) == null); //false 

meanwhile throws invocationtargetexception caused nullpointerexception @ root.get(column).

//invoked persondao.test(person_.name) main public void test(singularattribute column) {     entitymanager em = hibernateutil.getentitymanager();     criteriabuilder builder = em.getcriteriabuilder();     criteriaquery<person> criteria = builder.createquery(person.class);     root<person> root = criteria.from(person.class);     system.out.println(root.get(column) == null); //invocationtargetexception } 

is it's supposed do? how can pass singularattribute object method parameter?

edit2:

if call hibernateutil.getentitymanager() in main before method call, somehow works. it works when call literally empty block of method init(). related initialization of classes?

public class hibernateutil {     private static entitymanagerfactory emfactory;     private static entitymanager em;     private static final logger log = loggerfactory.getlogger(hibernateutil.class);     private static final string persistence_unit = "pt";     static{         log.info("creating entity manager factory");         emfactory = persistence.createentitymanagerfactory(persistence_unit);     }     //calling main before persondao's method calls somehow works...     public void init(){} //does nothing     public static entitymanager getentitymanager(){         if(em != null && em.isopen()){             closeentitymanager();         }         log.debug("creating new entity manager");         em = emfactory.createentitymanager();         return em;     }     public static void close() throws exception {         if(em != null && em.isopen()){             closeentitymanager();         }         log.info("closing entity manager factory");         emfactory.close();     }     private static void closeentitymanager(){         log.info("closing last entity manager");         em.close();     } } 


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -