2016-12-03 10 views
0

で私のプロジェクトの設定をという名前のパラメータを見つけることができませんでしたPostgreSQLのDB上のSpring MVC、Hibernateは4.3.8.Final、であり、次のエラー取得:のUserInfo QueryParameterExceptionはなしエンティティ

org.hibernate.QueryParameterException: could not locate named parameter [username]

public UserInfo findUserInfo(String userName) { 
    String sql = "Select new " + UserInfo.class.getName() + "(u.username,u.password) "// 
      + " from " + User.class.getName() + " u where u.username=(:username)"; 

    Session session = sessionFactory.getCurrentSession(); 

    Query query = session.createQuery(sql).setParameter("username", userName); 

    return (UserInfo) query.uniqueResult(); 
} 

ですなしエンティティ:

public class UserInfo { 

    private String username; 
    private String password; 

    public UserInfo() { 
    } 

    public UserInfo(String username, String password) { 
     this.username = username; 
     this.password = password; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 
} 

答えて

0

あなたがUserと呼ばれるエンティティを持っていると仮定は、と"from " + User.class.getName()を置き換えますを削除し、パラメータ(:username)のまわりのかっこを削除します。 :usernameである必要があります。

String sql = "Select new " + UserInfo.class.getName() + "(u.username,u.password) " 
      + " from yourpackageHERE." + User.class.getName() + " u where u.username=:username"; 

をしかし、あなたのエンティティがUserInfoと呼ばれているならば、selectfromの両方でUserInfoを使用してください:

だからあなたの文字列は以下のようにする必要があります。ここで

String sql = "Select new " + UserInfo.class.getName() + "(u.username,u.password) " 
       + " from yourpackageHERE." + UserInfo.class.getName() + " u where u.username=:username"; 
+0

ありがとうございました。しかし、UserInfoは通常のクラス(エンティティではない)であり、Userはテーブルのユーザにマッピングしているエンティティです。 –

+0

@GiangPhanThanhGiangそれから 'String sql ="あなたのパッケージから新しい "+ UserInfo.class.getName()+"(u.username、u.password) " +"を選択してください。 + User.class.getName()+ "u u.username =:username"; ' – Mitchapp

+0

ありがとうございました。私は自分の問題を見つけました。私はHibernateで自分のエンティティを宣言していないからです。 –

0

JPA 2.0 Specificationからの抜粋です:

4.3.1 Naming

Entities are designated in query strings by their entity names. The entity name is defined by the name element of the Entity annotation (or the entity-name XML descriptor element), and defaults to the unqualified name of the entity class. Entity names are scoped within the persistence unit and must be unique within the persistence unit.

したがって、上記の仕様の抽出液によると、クエリ文字列は次のようになります。

あなたが定義したと仮定すると、
String sql = "select new " + UserInfo.class.getName() + "(u.username, u.password) from User u where u.username=:username"; 

エンティティUserの場合、User.class.getName()はクラスの修飾名(パッケージ名を含む)を返しますが、JPAはエンティティの非修飾名を必要とします。もう一つは、user @Mitchappが指摘するように、指定されたパラメータの周りには括弧は使用できません。

+0

名前の付いたパラメータのまわりで括弧を削除しましたが、それも機能しません –

+0

どうしますか?例外はありますか?私はあなたがものを混ぜていると思う。 JPAを使用している場合は、 'Session'の代わりに' EntityManager'を使用してください。上記のクエリはHQLではなくJP QLです。つまり、EntityManagerFactoryとEntityManagerを使用してクエリを定義することをお勧めします。 – ujulu

+0

ご協力ありがとうございます。問題が見つかりました。私のEntitiyは工場のBeanでスキャンされたパッケージの外にあります。 –

関連する問題