2013-04-20 13 views
6

Ho una tabella "AuthorFollow" con chiave composta di "authorId" e "userId" che sono chiavi primarie in "AuthorInfo" "&" UserInfo "rispettivamente.java.sql.SQLException: indice parametro fuori intervallo (3> numero di parametri, che è 2)

Sto cercando di salvare l'oggetto di "AuthorFollow" nel database (sto usando mysql). Ma, sto ottenendo l'errore:

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

Il codice, che sto cercando, per salvare l'oggetto è:

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

I file di mapping (questi mapping sono auto generati da 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 (mostrando solo la mappatura per 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 (mostrando solo la mappatura per 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 (?, ?) 
+0

Potrebbe pls. passare in hibernate trace con ' true' in 'hibernate.cfg.xml' e pubblicare il risultato? Qualcosa nella dichiarazione generata sembra essere sbagliato. – Johanna

+0

fatto, ho aggiunto la traccia ora. –

risposta

5

Il messaggio di errore da Hibernate non è chiaro, il che rende difficile vedere dove il problema è Questo è il motivo per cui ti ho dato un +1.

Il tuo errore è: nella mappatura di AuthorFollow non è corretto. Si mappa author_id e user_id due volte. Hibernate non può gestire questo. Nell'inserto fallisce nel conteggio dei parametri. Ogni colonna deve essere mappata una sola volta (eccetto se una mappatura è di sola lettura, ma quel caso speciale non interessa per il tuo problema).

ci sono due soluzioni:

1) soluzione semplice: lavorare con ids invece di AuthorInfo e UserInfo oggetti:

<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> proprietà più. Se hai bisogno esplicitamente di un'istanza AuthorInfo o UserInfo, devi caricare una dichiarazione separata Session.load().

2) Complessa soluzione: utilizzare <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> 

vi consiglio di utilizzare la soluzione semplice 1) a meno che non v'è un uso molto o altre buone ragioni per la soluzione più complessa in 2). Soluzione 2) definitivamente è più problemi.

+0

Grazie mille. Ho provato la seconda soluzione e ha funzionato (ma non ho usato la classe 'AuthorFollowPK'). Avevo anche pensato che le colonne fossero mappate due volte, ma le mappature sono state generate automaticamente e ho poca conoscenza delle mappature, quindi ho pensato che il mio codice avrebbe qualche problema :) –

+0

Puoi dimenticare la classe 'AuthorFollowPK', è non necessario.Era solo lì perché ho fatto copia e incolla dal tuo codice. – Johanna

Problemi correlati