2013-04-20 36 views
6

Tôi có một bảng "AuthorFollow" có khóa tổng hợp của 'authorId' và 'userId' là khóa chính trong "AuthorInfo "&" Bảng UserInfo "tương ứng.java.sql.SQLException: Chỉ số tham số ngoài phạm vi (3> số tham số, là 2)

Tôi đang cố gắng lưu đối tượng của "AuthorFollow" trong cơ sở dữ liệu (Tôi đang sử dụng mysql). Nhưng, tôi nhận được lỗi:

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

Mã, mà tôi đang cố gắng để lưu các đối tượng là:

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

Các tập tin bản đồ (các ánh xạ được tự động tạo ra bởi 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 (chỉ hiển thị bản đồ cho 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 (chỉ hiển thị bản đồ cho 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

Bạn có thể bắt đầu không. chuyển đổi trong dấu vết hibernate với ' true' trong 'hibernate.cfg.xml' và đăng kết quả? Nội dung nào đó trong câu lệnh được tạo có vẻ sai. – Johanna

+0

hoàn tất, tôi đã thêm dấu vết ngay bây giờ. –

Trả lời

5

Các thông báo lỗi từ Hibernate là không rõ ràng, mà làm cho nó khó khăn để xem nơi mà vấn đề là. Đây là lý do tại sao tôi đã cho bạn một +1.

Lỗi của bạn là: Trong ánh xạ AuthorFollow không chính xác. Bạn lập bản đồ author_iduser_id hai lần. Hibernate không thể xử lý điều này. Trong khi chèn nó không thành công khi đếm các tham số. Mỗi cột phải được ánh xạ chỉ một lần (trừ khi một ánh xạ chỉ đọc, nhưng trường hợp đặc biệt đó không quan tâm đến vấn đề của bạn).

Có hai giải pháp:

1) Giải pháp đơn giản: Làm việc với id thay vì AuthorInfoUserInfo đối tượng:

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

Không <many-to-one> tính nữa. Nếu bạn cần một trường hợp AuthorInfo hoặc UserInfo cụ thể, bạn phải tải trong tuyên bố riêng biệt Session.load().

2) giải pháp Complex: Sử dụng <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> 

Tôi khuyên bạn nên sử dụng các giải pháp đơn giản 1) ngoại trừ nếu có một sử dụng nhiều hoặc lý do chính đáng khác cho các giải pháp phức tạp hơn trong 2). Giải pháp 2) dứt khoát là rắc rối hơn.

+0

Cảm ơn rất nhiều. Tôi đã thử giải pháp thứ 2 và nó hoạt động (Nhưng, tôi đã không sử dụng lớp 'AuthorFollowPK'). Tôi cũng nghĩ rằng các cột đã được ánh xạ hai lần, nhưng ánh xạ được tạo tự động và tôi có rất ít kiến ​​thức về ánh xạ, vì vậy tôi nghĩ mã của tôi sẽ có một số vấn đề :) –

+0

Bạn có thể quên lớp 'AuthorFollowPK', không cần thiết.Nó chỉ ở đó vì tôi đã sao chép và dán từ mã của bạn. – Johanna

Các vấn đề liên quan