2017-02-08 4 views
0

hibernateクエリを使用してデータをフェッチするクラスを作成します。私はそれがスプリングデータhibernateクエリを使用したい

java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [java.lang.Long (n/a)] 

、のようなエラーを取得するEntityManager

public class BranchCustomRepositoryImpl implements BranchCustomRepository{ 

    @PersistenceContext 
     private EntityManager entityManager; 

    private SessionFactory sessionFactory; 



    public Branch findByOrgOrgIdAndBranchId(String orgId, String branchId) { 
    //Session session=null; 
    //sessionFactory=entityManager.unwrap(SessionFactory.class); 
    //session=(Session) sessionFactory.getCurrentSession(); 

     Session session = (Session) entityManager.getDelegate(); 
     System.out.println("BranchCustomRepositoryImpl"); 
     Long orgId2=Long.valueOf(orgId); 
     Long branchId2=Long.valueOf(branchId); 
     try{ 
     Query query= (Query)((EntityManager) session).createQuery("from Branch b where b.org.orgId=:orgId AND b.branchId=:branchId");  
     query.setParameter("orgId", orgId2); 
     query.setParameter("branchId", branchId2);  
     return (Branch) query.uniqueResult(); 
     }catch(Exception e){ 
      System.out.println("Exception"+e.toString()); 
     }finally{ 
      try { 
       if(session!=null){ 
       session.close(); 
       System.out.println("session closed"); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return null; 

    } 

} 

をセッションをキャスト尋ねる

Session session=null; 
    sessionFactory=entityManager.unwrap(SessionFactory.class); 
    session=(Session) sessionFactory.getCurrentSession(); 

または

Session session = (Session) entityManager.getDelegate(); 

とセッションaginを使用してのEntityManagerからのセッションを取得するときifどのように春データjpaでハイバネートクエリを使用する方法を知っています

+0

以下のように変更し、それを

query.setParameter("orgId", orgId2); query.setParameter("branchId", branchId2); 

以下のようにあなたの現在の行であなたはとても「APIを休止状態」を使用する必要がJPQLを使用していない完全に発現可能であるクエリを持って –

答えて

0

永続的な属性orgId2のタイプはLongなので、対応する タイプの引数は、ParameterExpressionを作成するときにもLongでなければなりません。したがって、ParameterExpressionの型はLong型であるため、パラメータの値の型もLong型にする必要があります。クエリパラメータを設定するときは、次のように変更します。

代わりに、このような

query.setParameter("orgId", Long.valueOf(orgId2)); 
    query.setParameter("branchId", Long.valueOf(branchId2)); 
+0

なしorgId2とbrancgId2の値はlo​​ngのみ –

関連する問題