2013-10-02 33 views
1

下面是我的POJO的ERROR util.JDBCExceptionReporter - 否參數1指定的值使用Hibernate ORM

class User extends AbstractDomainObject{ 
    private String username; 
    private String password; 

    //setter and getter 
} 

class Notification extends AbstractDomainObject{ 
    private String message; 
    private Set<User> notificationFor; 

    //setter and getter 
} 

class AbstractDomainObject{ 
    private long id; 
    // setter and getter 
} 

下面是映射的上面POJO

User.hbm.xml

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="User" table="user"> 
     <id name="id" type="long" column="id"> 
      <generator class="native" /> 
     </id> 

      <property name="username" type="string"> 
      <column name="username" /> 
     </property> 

     <property name="password" type="string"> 
      <column name="password" /> 
     </property> 
    </class> 
</hibernate-mapping> 

Notification.hbm.xml

<?xml version="1.0"?> 
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
    <hibernate-mapping> 

     <class name="Notification" table="notification"> 
      <id name="id" type="long" column="id"> 
       <generator class="native" /> 
      </id> 

       <set name="notificationFor" table="user_notification" lazy="false" cascade="all"> 
      <key column="notification_id"/> 
      <many-to-many unique="true" class="User" column="user_id"/> 
      </set> 

       <property name="message" type="string"> 
      <column name="message" /> 
      </property> 
     </class> 
    </hibernate-mapping> 

我希望給定用戶的通知列表。下面是我的daoimpl。

public List<Notification> getNotification(User user) { 

     Session session = null; 
     List<Notification> notifications = null; 
     try { 

      session = getSessionFactory().openSession(); 
      String queryString = "from Notification notification where notification.notificationFor IN (:user)"; 
      Query query = session.createQuery(queryString); 
      query.setParameter("user", user); 
      notifications = query.list();   

     } catch (Exception e) { 

      e.printStackTrace(); 

     } 

     return notifications; 
    } 

上面的代碼片段在query.list()線給錯誤。 錯誤是 錯誤util.JDBCExceptionReporter - 沒有指定參數1的值 任何幫助將是可觀的。我不知道我錯在哪裏。

回答

0

用戶是一個用戶定義的類,休眠不知道它。你應該提供一個Hibernate已知的類型。它可能是String,Long,int

我假設用戶有一個名稱字段,查詢將相應執行。

query.setParameter("user", user.getName()); 

此外,你應該給實體類Query類。可以使用Query#setResultTransformer

Query query = session.createQuery(queryString).setResultTransformer(Transformers.aliasToBean(Notification.class)); 
相關問題