2013-04-20 2 views
6

Ich habe eine Tabelle "AuthorFollow" mit Composite-Schlüssel von 'AuthorId' und 'UserId', die Primärschlüssel in "AuthorInfo sind "&" UserInfo "Tabelle jeweils.java.sql.SQLException: Parameter Index außerhalb des Bereichs (3> Anzahl der Parameter, die 2 ist)

Ich versuche, Objekt von "AuthorFollow" in der Datenbank zu speichern (ich verwende mysql). Aber ich erhalte die Fehlermeldung:

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

Der Code, die ich versuche, das Objekt zu speichern ist:

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

Die Mapping-Dateien (diese Zuordnungen werden automatisch von IntelliJ IDEA): 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 (nur Zuordnung für AuthorFollow angezeigt):

<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 (zeigt nur Mapping für 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

Könnten Sie bitte. Schalten Sie den Ruhezustands-Trace mit ' true' in 'hibernate.cfg.xml' ein und veröffentlichen Sie das Ergebnis? Etwas in der erzeugten Aussage scheint falsch zu sein. – Johanna

+0

fertig, ich habe jetzt die Spur hinzugefügt. –

Antwort

5

Die Fehlermeldung aus dem Ruhezustand ist nicht klar, was es schwierig macht, um zu sehen Wo ist das Problem? Deshalb habe ich dir +1 gegeben.

Ihr Fehler ist: In der Zuordnung von AuthorFollow ist nicht korrekt. Sie mappen zweimal author_id und user_id. Hibernate kann damit nicht umgehen. In der Einfügung schlägt es fehl, wenn die Parameter gezählt werden. Jede Spalte sollte nur einmal zugeordnet werden (es sei denn, eine Zuordnung ist schreibgeschützt, aber dieser Sonderfall ist für Ihr Problem nicht von Interesse).

Es gibt zwei Lösungen:

1) Einfache Lösung: Arbeiten Sie mit ids statt AuthorInfo und UserInfo Objekte:

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

Keine <many-to-one> Eigenschaften mehr. Wenn Sie explizit eine AuthorInfo oder UserInfo Instanz benötigen, müssen Sie eine separate Session.load()-Anweisung laden.

2) Komplexe Lösung: Einsatz <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> 

Ich empfehle die einfache Lösung 1), außer zu verwenden, wenn es viel Gebrauch oder andere gute Gründe für die komplexere Lösung in 2). Lösung 2) ist definitiv mehr Ärger.

+0

Vielen Dank. Ich habe die 2. Lösung ausprobiert und es hat funktioniert (Aber ich habe die Klasse 'AuthorFollowPK' nicht benutzt). Ich hatte auch gedacht, dass die Spalten wurden zweimal zugeordnet, aber die Zuordnungen wurden automatisch generiert und ich habe sehr wenig Kenntnis von Zuordnungen, so dachte ich, mein Code hätte ein Problem :) –

+0

Sie können die 'AuthorFollowPK' Klasse vergessen, es ist nicht nötig.Es war nur da drin, weil ich aus deinem Code kopiert und eingefügt habe. – Johanna

Verwandte Themen