繁体   English   中英

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

[英]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已知的类型。 可能是StringLongint

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM