& Quot; Repeated column in mapping for entity & quot; Exception in Hibernate, hibernaterepeated

Source: Internet
Author: User

"Repeated column in mapping for entity" exception in Hibernate, hibernaterepeated

Turn: http://lijiejava.iteye.com/blog/786535 one-to-many bidirectional Association (class Item and class Bid ):

Item class:

public class Item {       private int id;       private String name;       private Set bids = new HashSet();      •••  }

 

Bid class:

public class Bid {       private int id;       private double amount;       private Item item;      •••  }

Item. hbm. xml :( t_item table)

Bid. hbm. xml: (t_bid table ):

Test code:

•••  Item item = new Item();  item.setName("item");            Bid b1 = new Bid();  b1.setAmount(12.09);   b1.setItem(item);            Bid b2 = new Bid();  b2.setAmount(11.98);   b2.setItem(item);            Set bids = new HashSet();  bids.add(b1);  bids.add(b2);            item.setBids(bids);             session.beginTransaction();    session.save(item);   session.getTransaction().commit();

This is a previous "One-to-multiple bidirectional Association". The following exception is thrown during running today:

Exception in thread "main" java.lang.ExceptionInInitializerError  •••  Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: value.Bid column: item_id (should be mapped with insert="false" update="false")

Check the ing file carefully and find. hbm. the <key> element in the xml configuration file has a not-null = "true" restriction. After removing the restriction, it can run normally.
(1)What does adding not-null = "true" mean?


The item_id ing defined by the <key> element is the foreign key column of the t_bid table. Adding not-null = "true" means that if you want to add records in the t_bid table, the foreign key column item_id must not be null, to ensure that the item_id field is not null ), this field is assigned a value in the t_bid insert statement.
In fact, whether it is one-to-many or two-way, you only need to set not-null = "true" in the <key> element ", in the insert statement of the t_bid table, the column specified by the column attribute is added (item_id here) to ensure that the item_id column is not empty.Take one-to-Multiple Association as an example: if not-null = "true" is not set, the output statement is Hibernate: insert into t_bid (amount) values (?); If not-null = "true" is set, the output statement is Hibernate: insert into t_bid (amount, item_id) values (?, ?) .
(2)Why is an exception thrown?


The cause of the exception can be seen from the exception information, that is, the fields are duplicated. Through the analysis in (1), you can clearly understand the cause. View the Bid. hbm. xml ing file:
<Example-to-one name = "item" class = "value. Item" column = "item_id" not-null = "true"/>
It can be seen that this is a multi-to-one association. not-null = "true" indicates that a Bid must have its corresponding Item entity. The column attribute specifies that the item_id column in The t_bid table is a foreign key of the primary key of the t_item table.For Bid, whether or not-null = "true" is specified, its insert statement will assign values to the item_id field, even if it is null.This is the cause of the exception. This setting will cause Hibernate to issue a statement similar to insert into t_bid (item_id, item_id) values (•), so it will prompt repeated field exceptions.
(3)Solution:


1. Remove not-null = "true" from the <key> element. In fact, in two-way join, the Item end is not required to ensure that the item_id of the foreign key column is not null.
2. Add insert = "false" update = "false" to the Bid. hbm. xml ing file as prompted ".
These two restrictions mean that the insert and update operations on the t_bid table do not contain this field.To avoid duplication.
3. Add inverse = "true" to the <set> element in Item. hbm. xml to hand over all the associations to the Bid end. 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 without repeating item_id. This will lead to redundant data table fields and should not be used.
In actual applications, the not-null = "true" restriction cannot be added to <key>.

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.