[英]Hibernate and DB2- gives out an ERROR: util.JDBCExceptionReporter while inserting into or deleting from the table
[英]ERROR util.JDBCExceptionReporter - No value specified for parameter 1 using Hibernate ORM
以下是我的POJO的
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()行給出了錯誤。 錯誤為ERROR util.JDBCExceptionReporter-未為參數1指定值任何幫助都將是可觀的。 我不知道我哪里錯了。
用戶是一個用戶定義的類,它不休眠。 您應該提供一種Hibernate已知的類型。 可能是String
, Long
, int
。
我假設用戶有一個名稱字段,查詢將相應地執行。
query.setParameter("user", user.getName());
此外,您應該將實體類賦予Query
類。 可以使用Query#setResultTransformer
。
Query query = session.createQuery(queryString).setResultTransformer(Transformers.aliasToBean(Notification.class));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.