gpt4 book ai didi

java - 错误 util.JDBCExceptionReporter - 使用 Hibernate ORM 没有为参数 1 指定值

转载 作者:行者123 更新时间:2023-11-30 23:12:58 25 4
gpt4 key购买 nike

下面是我的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的映射

用户.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>

通知.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 指定值任何帮助将不胜感激。我不知道我哪里错了。

最佳答案

User 是一个用户定义的类,hibernate 不知道它。您应该提供 Hibernate 已知的类型。它可能是StringLongint

我假设 User 有一个名称字段,查询将相应地执行。

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

此外,您应该将实体类交给Query 类。可以使用 Query#setResultTransformer

Query query = session.createQuery(queryString).setResultTransformer(Transformers.aliasToBean(Notification.class));

关于java - 错误 util.JDBCExceptionReporter - 使用 Hibernate ORM 没有为参数 1 指定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19135768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com