Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2)

I have a table "AuthorFollow" having composite key of 'authorId' and 'userId' which are primary keys in "AuthorInfo" & "UserInfo" table respectively.

I am trying to save object of "AuthorFollow" in database(I am using mysql). But, I'm getting the error :

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).
.
.
.

The code, which i'm trying, to save the object is :

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;
    }

The mapping files (these mappings are auto generated by 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>

AuthorInfo.hbm.xml (showing only mapping for AuthorFollow):

<set name="authorFollowsByAuthorId" inverse="true">
        <key>
            <column name="author_id" not-null="true"/>
        </key>
        <one-to-many not-found="ignore" class="com.pojo.hibernate.AuthorFollow"/>
    </set>

UserInfo.hbm.xml (showing only mapping for AuthorFollow):

<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>

UPDATE :

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 (?, ?)
like image 372
Ashish Tanna Avatar asked Apr 20 '13 17:04

Ashish Tanna


1 Answers

The error message from Hibernate is not clear, which makes it difficult to see where the problem is. This is why I gave you a +1.

Your error is: In the mapping of AuthorFollowis not correct. You map author_id and user_id twice. Hibernate can't handle this. In the insert it fails when counting the parameters. Each column should be mapped only once (except if one mapping is read only, but that special case is of no interest for your problem).

There are two solutions:

1 ) Simple Solution: Work with ids instead of AuthorInfo and UserInfo objects:

<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>

No <many-to-one> properties any more. If you explicitely need an AuthorInfo or UserInfo instance, you have to load in a separate Session.load() statement.

2 ) Complex solution: Use <key-many-to-one>:

<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>

I recommend to use the simple solution 1) except if there is a lot use or other good reasons for the more complex solution in 2). Solution 2) definitively is more trouble.

like image 160
Johanna Avatar answered Oct 05 '22 13:10

Johanna