-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: setParameter for where clause leads to NullpointerException
PostPosted: Thu Feb 16, 2017 3:14 am 
Newbie

Joined: Wed Feb 15, 2017 11:32 am
Posts: 2
Hi all!

I'm using hibernate-core 5.2.6.Final for querying Views from an Oracle-SQL Server (Oracle10gDialect).
I've generated both POJO and StaticMetaModel files.
In hibernate.cfg.xml I'm mapping to the POJO entity class.

I'm not allowed to post original company code, but the StaticMetaModel looks like:
Code:
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(MyEntity.class)
public abstract class MyEntity_ {

   public static volatile SingularAttribute<MyEntity, String> someId;
   public static volatile SingularAttribute<MyEntity, Long> someLong;                  // NUMBER(4)
}


I can query the view when adding a where-clause for someId.
But when I additionally add someLong to the where-clause i get a NullpointerException.

Code:
java.lang.NullPointerException
   at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:375)
   at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:57)
        .....


The query looks like this:

Code:
   public Collection<MyEntity> readData(String id, long longValue) {
      EntityManager em = this.getSession().getEntityManagerFactory().createEntityManager();
      CriteriaBuilder cb = em.getCriteriaBuilder();
      
      CriteriaQuery<MyEntity> q = cb.createQuery(MyEntity.class);
      Root<MyEntity> root = q.from(MyEntity.class);
   
      ParameterExpression<Long> pSomeLong = cb.parameter(Long.class);
      q.where(cb.equal(root.get(MyEntity_.someLong), pSomeLong ));
      ParameterExpression<String> pSomeId = cb.parameter(String.class);
      q.where(cb.equal(root.get(MyEntity_.someId), pSomeId ));
      
      TypedQuery<MyEntity> query = em.createQuery(q);
      query.setParameter(pSomeId , id);
      query.setParameter(pSomeLong , longValue);   // crash happens here

      return query.getResultList();
   }


The exception occurs when I call query.setParameter(pSomeLong , longValue).

Here is the POJO:
Code:
@Entity
@Table(name = "MY_ENTITY_VIEW", schema = "SOME_SCHEMA", catalog = "")
public class MyEntity implements java.io.Serializable {
   /**
    *
    */
   private static final long serialVersionUID = -7508480021949722883L;
   private Long someLong;
   private String someId;

   @Basic
   @Column(name = "LONG_FIELD")
   public Long getSomeLong() {
      return someLong;
   }

   public void setSomeLong(Long someLong) {
      this.someLong = someLong;
   }

   @Id
   @Basic
   @Column(name = "SOME_ID")
   public String getSomeId() {
      return someId;
   }

   public void setSomeId(String someId) {
      this.someId = someId;
   }
}


When debugging the Hibernate code i can see that resolveParameterInfo returns a NullReference (CriteriaQueryTypeQueryAdapter):

Code:
   @Override
   @SuppressWarnings({ "unchecked" })
   public <T> QueryImplementor<X> setParameter(Parameter<T> param, T t) {
      entityManager.checkOpen( false );
      final ExplicitParameterInfo parameterInfo = resolveParameterInfo( param );  // ---> returns null
      if ( parameterInfo.isNamed() ) {
         jpqlQuery.setParameter( parameterInfo.getName(), t );
      }
      else {
         jpqlQuery.setParameter( parameterInfo.getPosition(), t );
      }
      return this;
   }


Does anyone have an idea what's going wrong here?
Any hints welcome.

Thanks in advance.

Regards,
Jo


Top
 Profile  
 
 Post subject: Re: SetParameter for where-Clause leads to NullpointerException
PostPosted: Thu Feb 16, 2017 4:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You are overriding the where clause with your second q.where call so Hibernate will not never find the "pSomeLong" paremeter:

Code:
q.where(cb.equal(root.get(MyEntity_.someLong), pSomeLong ));
ParameterExpression<String> pSomeId = cb.parameter(String.class);
q.where(cb.equal(root.get(MyEntity_.someId), pSomeId ));


What you need to do is to pass both parameters to a single WHERE clause method call.

Code:
ParameterExpression<Long> pSomeLong = cb.parameter(Long.class);
ParameterExpression<String> pSomeId = cb.parameter(String.class);

q.where(
      cb.equal( root.get(MyEntity_.someLong), pSomeLong ),
      cb.equal( root.get(MyEntity_.someId    ), pSomeId     )
);


Top
 Profile  
 
 Post subject: Re: setParameter for where clause leads to NullpointerException
PostPosted: Thu Feb 16, 2017 4:40 am 
Newbie

Joined: Wed Feb 15, 2017 11:32 am
Posts: 2
Thanks a lot! What a mistake.


Top
 Profile  
 
 Post subject: Re: setParameter for where clause leads to NullpointerException
PostPosted: Thu Feb 16, 2017 4:46 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I'm glad it worked.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.