"Repeated column in mapping for entity" exception in Hibernate

Source: Internet
Author: User

Ext.: http://lijiejava.iteye.com/blog/786535

One-to-many bidirectional association (class item with class Bid):
Item Class:

Java code

    1. public class Item {
    2. private int id;
    3. private String name;
    4. Private Set bids = new HashSet ();
    5. ???
    6. }

Bid class:

Java code

    1. public class Bid {
    2. private int id;
    3. private double amount;
    4. Private item Item;
    5. ???
    6. }

Item.hbm.xml: (T_item table)

Java code

    1. ???
    2. <set name= "bids" table= "T_bid" cascade= "Save-update" >
    3. <key column= "item_id" not-null= "true"/>
    4. <one-to-many class= "value. Bid "/>
    5. </set>
    6. ???

Bid.hbm.xml: (t_bid table)

Java code

    1. ???
    2. <many-to-one name= "Item" class= "value. Item "column=" item_id "not-null=" true "/>
    3. ???

Test code:

Java code

    1. ???
    2. Item item = new Item ();
    3. Item.setname ("item");
    4. Bid B1 = new bid ();
    5. B1.setamount (12.09);
    6. B1.setitem (item);
    7. Bid B2 = new bid ();
    8. B2.setamount (11.98);
    9. B2.setitem (item);
    10. Set bids = new HashSet ();
    11. Bids.add (B1);
    12. Bids.add (B2);
    13. Item.setbids (Bids);
    14. Session.begintransaction ();
    15. Session.save (item);
    16. Session.gettransaction (). commit ();

This is a previous "one-to-many bi-directional association," and today the runtime throws the following exception:

Java code

    1. Exception in thread "main" Java.lang.ExceptionInInitializerError
    2. ???
    3. caused by:org.hibernate.MappingException:Repeated column in mapping for Entity:value. Bid column:item_id (should is mapped with insert= "false" Update= "false")

Carefully examined the mapping file, found that the <key> element in the Item.hbm.xml configuration file has a not-null= "true" limit, it will be removed after the normal operation.
(1)What does adding not-null= "true" mean?
Column mappings defined by the <key> element item_id are foreign key columns of the T_bid table. After adding not-null= "true", it means that if you want to increase the records in the T_bid table, the foreign key column item_id must not be the Null,item side in order to ensure that the item_id field is not null (the item side does not know the condition of the bid end, Therefore, it is not possible to rely on the bid side to ensure that the item_id is not empty, and the field is assigned a value in the T_bid INSERT statement.
In fact, either one-to-many or two-way pairs, as long as the not-null= "true" is set in the <key> element, the columns specified by the column property are incremented in the INSERT statement of the T_bid table (here, item_id). This ensures that the item_id column is not empty. Take one-way-to-many associations as an example: if Not-null= "true" is not set, then the output statement is: Hibernate:insert into t_bid (amount) values (?); If not-null= "true" is set, then the output statement is: Hibernate:insert into T_bid (amount, item_id) values (?,?).
(2)Why is the exception thrown?
The cause of the exception can be seen in the exception information, that is, the field is duplicated. Through the analysis in (1), we can clearly understand the reasons. To view the Bid.hbm.xml mapping file:
<many-to-one name= "Item" class= "value. Item "column=" item_id "not-null=" true "/>
As can be seen, this is a many-to-one association, not-null= "True" indicates that a bid must have its corresponding item entity. The column property specifies that item_id columns in the T_bid table are a foreign key to the primary key of the T_item table.for bid, regardless of whether not-null= "true" is specified, its INSERT statement assigns a value to the item_id field, even if it is null. This is the cause of the exception, and this setting causes Hibernate to emit a similar insert into t_bid (item_id,item_id) VALUES (???) Statement, you are prompted to repeat the field exception.
(3)Solve:
1. Remove the Not-null= "true" from the <key> element, in fact it is not required by the item side in a bidirectional association to ensure that the foreign key column item_id is not NULL.
2. You can follow the prompts to add insert= "false" update= "false" to the Bid.hbm.xml mapping file.
Adding these two restrictions means that this field is not included in the insert and update operations on the T_bid table, which avoids duplication.
3. Add inverse= "True" to the <set> element in Item.hbm.xml, and give the relationship all to the bid side. http://lijiejava.iteye.com/blog/776587
4. The most boring method: Change the column value in the <key> element to item_id_1 and so on, do not repeat with item_id, this will result in redundant data table fields, should not be used.
In real-world applications, not-null= "true" limits should not be added to <key>.

"Repeated column in mapping for entity" exception in Hibernate

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.