Hibernate java.sql.SQLException:参数索引超出范围(3>;参数数,即2)

Hibernate java.sql.SQLException:参数索引超出范围(3>;参数数,即2),hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,我有一个表“AuthorFollow”,其复合键为“authorId”和“userId”,这两个键分别是“AuthorInfo”和“UserInfo”表中的主键 我试图在数据库中保存“AuthorFollow”的对象(我使用的是mysql)。 但是,我得到了一个错误: org.hibernate.exception.GenericJDBCException: could not insert: [com.pojo.hibernate.AuthorFollow] . . . Caused by:

我有一个表“AuthorFollow”,其复合键为“authorId”和“userId”,这两个键分别是“AuthorInfo”和“UserInfo”表中的主键

我试图在数据库中保存“AuthorFollow”的对象(我使用的是mysql)。 但是,我得到了一个错误:

org.hibernate.exception.GenericJDBCException: could not insert: [com.pojo.hibernate.AuthorFollow]
.
.
.
Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
.
.
.
我正在尝试保存对象的代码是:

Transaction transaction = hibernateTemplate.getSessionFactory().getCurrentSession().beginTransaction();
    try {
        AuthorFollow authorFollow = new AuthorFollow();
        authorFollow.setAuthorId(authorInfo.getAuthorId());
        authorFollow.setUserId(userInfo.getUserId());
        authorFollow.setAuthorInfoByAuthorId(authorInfo);
        authorFollow.setUserInfoByUserId(userInfo);

        authorInfo.getAuthorFollowsByAuthorId().add(authorFollow);
        userInfo.getAuthorFollowsByUserId().add(authorFollow);

        updateObject(authorInfo);
        updateObject(userInfo);

        transaction.commit();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        transaction.rollback();
        return false;
    }
映射文件(这些映射由IntellijIdea自动生成): AuthorFollow.hbm.xml:

<hibernate-mapping>
    <class name="com.pojo.hibernate.AuthorFollow" table="author_follow" catalog="book">
    <composite-id mapped="true" class="com.pojo.hibernate.AuthorFollowPK">
        <key-property name="userId" column="user_id"/>
        <key-property name="authorId" column="author_id"/>
    </composite-id>
    <many-to-one name="authorInfoByAuthorId" class="com.pojo.hibernate.AuthorInfo">
        <column name="author_id" not-null="true"/>
    </many-to-one>
    <many-to-one name="userInfoByUserId" class="com.pojo.hibernate.UserInfo">
        <column name="user_id" not-null="true"/>
    </many-to-one>
    </class>
</hibernate-mapping>

来自Hibernate的错误消息不清楚,这使得很难看到问题所在。这就是为什么我给你一个+1

您的错误是:
AuthorFollow的映射不正确。您映射了
作者id
用户id
两次。Hibernate无法处理这个问题。在insert中,计算参数时失败。每个列只应映射一次(除非有一个映射是只读的,但这种特殊情况对您的问题不感兴趣)

有两种解决方案:

1)简单的解决方案:使用ID而不是
AuthorInfo
UserInfo
对象:

<hibernate-mapping>
  <class name="com.pojo.hibernate.AuthorFollow" table="author_follow" catalog="book">
    <composite-id mapped="true" class="com.pojo.hibernate.AuthorFollowPK">
      <key-property name="userId" column="user_id"/>
      <key-property name="authorId" column="author_id"/>
    </composite-id>
  </class>
</hibernate-mapping>

我建议使用简单的解决方案1),除非2)中的更复杂的解决方案有很多用途或其他很好的理由。解决方案2)肯定是更麻烦。

您能帮忙吗。使用
hibernate.cfg.xml
中的
true
切换hibernate跟踪,然后发布结果?生成的语句中似乎有错误。完成,我现在添加了跟踪。非常感谢。我尝试了第二种解决方案&它奏效了(但是,我没有使用“AuthorFollowPK”类)。我还以为列被映射了两次,但是映射是自动生成的&我对映射知之甚少,所以我认为我的代码会有一些问题:)你可以忘记
AuthorFollowPK
类,这是没有必要的。它就在那里,因为我复制并粘贴了你的代码。
<set name="authorFollowsByUserId" inverse="true">
        <key>
            <column name="user_id" not-null="true"/>
        </key>
        <one-to-many not-found="ignore" class="com.pojo.hibernate.AuthorFollow"/>
    </set>
Hibernate: select userinfo0_.user_id as user1_21_, userinfo0_.first_name as first2_21_, userinfo0_.last_name as last3_21_, userinfo0_.user_gender as user4_21_, userinfo0_.user_img as user5_21_, userinfo0_.user_birthdate as user6_21_, userinfo0_.user_occupation as user7_21_, userinfo0_.user_qualification as user8_21_, userinfo0_.user_postal_code as user9_21_, userinfo0_.user_address as user10_21_, userinfo0_.user_city as user11_21_, userinfo0_.user_contact as user12_21_, userinfo0_.user_balance as user13_21_, userinfo0_.user_website as user14_21_, userinfo0_.email_verified as email15_21_, userinfo0_.email_id as email16_21_ from book.user_info userinfo0_ where userinfo0_.email_id=?
Hibernate: select authorinfo0_.author_id as author1_3_0_, authorinfo0_.author_name as author2_3_0_, authorinfo0_.author_pen_name as author3_3_0_, authorinfo0_.author_gender as author4_3_0_, authorinfo0_.author_description as author5_3_0_, authorinfo0_.author_blog_link as author6_3_0_, authorinfo0_.author_img as author7_3_0_, authorinfo0_.author_lives as author8_3_0_, authorinfo0_.author_born as author9_3_0_, authorinfo0_.author_died as author10_3_0_, authorinfo0_.author_notable_works as author11_3_0_ from book.author_info authorinfo0_ where authorinfo0_.author_id=?
Hibernate: select userinfo0_.user_id as user1_21_, userinfo0_.first_name as first2_21_, userinfo0_.last_name as last3_21_, userinfo0_.user_gender as user4_21_, userinfo0_.user_img as user5_21_, userinfo0_.user_birthdate as user6_21_, userinfo0_.user_occupation as user7_21_, userinfo0_.user_qualification as user8_21_, userinfo0_.user_postal_code as user9_21_, userinfo0_.user_address as user10_21_, userinfo0_.user_city as user11_21_, userinfo0_.user_contact as user12_21_, userinfo0_.user_balance as user13_21_, userinfo0_.user_website as user14_21_, userinfo0_.email_verified as email15_21_, userinfo0_.email_id as email16_21_ from book.user_info userinfo0_ where userinfo0_.user_id=?
Hibernate: select authorfoll0_.author_id as author2_3_1_, authorfoll0_.user_id as user1_1_, authorfoll0_.author_id as author2_1_, authorfoll0_.user_id as user1_1_0_, authorfoll0_.author_id as author2_1_0_ from book.author_follow authorfoll0_ where authorfoll0_.author_id=?
Hibernate: select authorfoll0_.user_id as user1_21_1_, authorfoll0_.user_id as user1_1_, authorfoll0_.author_id as author2_1_, authorfoll0_.user_id as user1_1_0_, authorfoll0_.author_id as author2_1_0_ from book.author_follow authorfoll0_ where authorfoll0_.user_id=?
Hibernate: insert into book.author_follow (author_id, user_id) values (?, ?)
<hibernate-mapping>
  <class name="com.pojo.hibernate.AuthorFollow" table="author_follow" catalog="book">
    <composite-id mapped="true" class="com.pojo.hibernate.AuthorFollowPK">
      <key-property name="userId" column="user_id"/>
      <key-property name="authorId" column="author_id"/>
    </composite-id>
  </class>
</hibernate-mapping>
<hibernate-mapping>
  <class name="com.pojo.hibernate.AuthorFollow" table="author_follow" catalog="book">
    <composite-id mapped="true" class="com.pojo.hibernate.AuthorFollowPK">
      <key-many-to-one name="authorInfoByAuthorId" class="com.pojo.hibernate.AuthorInfo" column="author_id"/>
      <key-many-to-one name="userInfoByUserId" class="com.pojo.hibernate.UserInfo" column="user_id"/>
    </composite-id>
  </class>
</hibernate-mapping>