`
paladin1988
  • 浏览: 329527 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【转】Hibernate中的"Repeated column in mapping for entity"异常

 
阅读更多


文章来源:http://www.iteye.com/topic/786535

作者:lijiejava

 


一对多双向关联(类Item与类Bid):

Item类:

 

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

 

Bid类:

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

 

Item.hbm.xml:(t_item表)

 

   <hibernate-mapping>  
        •••  
            <set name="bids" table="t_bid" cascade="save-update">  
                <key column="item_id" not-null="true"/>  
                <one-to-many class="value.Bid"/>  
           </set>   
         •••   
   </hibernate-mapping>  
 

 

Bid.hbm.xml: (t_bid表)

   <hibernate-mapping>  
        •••  
    <many-to-one name="item" class="value.Item" column="item_id" not-null="true" />   
        •••  
   </hibernate-mapping>  
 

 

测试代码:

•••  
 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(); 
 

这是以前的一个"一对多双向关联",今天运行时抛出了如下异常:

 

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

 

仔细检查了映射文件,发现在Item.hbm.xml配置文件的<key>元素中多了一个not-null="true"限制,将其去掉后就可以正常运行。

(1) 加入not-null="true"意味着什么?

由<key>元素定义的列映射item_id是t_bid表的外键列。加入not-null="true"之后,意味着如果要增加 t_bid表中的记录,那么外键列item_id一定不能为null,Item端为了确保item_id字段不为null(Item端不知道Bid端的情 况,所以它不可能依赖Bid端来确保item_id不为空),会在t_bid的插入语句中为该字段赋值。
事实上,不论是单向一对多还是双向一对多,只要在<key>元素中设置了not-null="true",那么在t_bid表的insert语句中都会增加column属性所指定的列(此处即item_id),以此确保item_id列不为空。 以 单向一对多关联为例:如果未设定not-null="true",那么输出的语句为:Hibernate: insert into t_bid (amount) values (?);而如果设定了not-null="true",那么输出的语句就是:Hibernate: insert into t_bid (amount, item_id) values (?, ?) 。

(2) 抛出异常的原因?

异常的原因可以从异常信息中看出,即字段重复。通过(1)中的分析,可以很清楚地明白其中的原因。查看Bid.hbm.xml映射文件:
<many-to-one name="item" class="value.Item" column="item_id" not-null="true" />
可以看出,这是一个多对一关联,not-null="true"表明一个Bid肯定有其对应的Item实体。column属性指定t_bid表中item_id列是t_item表主键的一个外键。对于Bid而言,不论是否指定了not-null="true",它的insert语句都会为item_id字段赋值,即使为null。 这就是异常产生的原因,这样的设置会使Hibernate发出类似 insert into t_bid (item_id,item_id) values (•••)的语句,所以会提示重复字段异常。

(3) 解决:
1. 把<key>元素中的not-null="true"去掉,事实上在双向关联中根本不需要由Item端来确保外键列item_id不为null。
2. 可以按照提示,在Bid.hbm.xml映射文件中加入insert="false" update="false"。
加入这两个限制,意味着对t_bid表的insert与update操作中不包含这个字段 ,这样可避免重复。
3. 在Item.hbm.xml中的<set>元素内加入inverse="true",将关联关系全部交给Bid端。http://lijiejava.iteye.com/blog/776587
4. 最无聊的方法:将<key>元素中的column值改成item_id_1等,不与item_id重复,这样会导致数据表字段的冗余,不应该使用。

在实际应用中,不应该在<key>中加入not-null="true"限制。

 

 

 

 

 

=================================================================

 

这个问题再写XML和注解的时候否碰到过,而且非常的频繁。

通过这篇帖子,也再次说明了,数据库中的外键是可以为空的。



下面是关于数据库外键的几篇帖子:

http://topic.csdn.net/u/20070525/00/523a0ee7-6d80-404d-ab70-fdd4920b9c45.html

http://blog.csdn.net/wkupaochuan/article/details/7633173

http://gwj41.iteye.com/blog/643467

 

 

分享到:
评论

相关推荐

    EmbeddableTypes

    ( org.hibernate.MappingException: Repeated column in mapping for entity:YOUR_ENTITY ),因为表databsae不能具有重复的列名。 JPA定义了@AttributeOverride批注来处理此senario。 @AttributeOverrides({ @...

    hibernate错误解决方案

    nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.xindeco.myregister.pojo.MyRegisterInfo column: password (should be mapped with insert="false" update=...

    repeated限定修饰符的使用

    在protobuf中,`repeated`限定修饰符是一个非常重要的概念,用于表示一个字段可以有多个值,类似于数组或列表。本文将深入探讨`repeated`限定修饰符的使用及其相关知识点。 首先,我们需要理解protobuf的基本语法。...

    详解protobuf-c之在C语言中如何使用repeated生成数组和字符串(包含配置pb-callback-t)

    本篇文章将详细解释如何在C语言环境中使用protobuf-c处理`repeated`字段,创建数组和字符串,并特别关注`pb_callback_t`这一特殊类型。 首先,我们需要理解`repeated`字段在protobuf语义中的含义。在protobuf的定义...

    编程珠玑全部源代码 分享

    Column 13: Set representations for the problem in Column 12 sets.cpp -- Several data structures for sets. genbins.c (Column 9) implements the bin data structure in C. Column 14: Heaps priqueue....

    SPSS Repeated measures ANOVA

    在心理学研究方法中,**重复测量方差分析(Repeated Measures ANOVA)**是一种常用的统计方法,用于处理涉及同一组参与者在不同时间点或不同条件下的数据。这种方法能够有效地控制个体差异的影响,从而更准确地评估...

    Docker-in-Action.pdf

    Docker-in-Action.pdf In 2011, I started working at Amazon.com. In that first week my life was changed as I learned how to use their internal build, dependency modeling, and deployment tool- ing. This ...

    Repeated Games and Reputation+Game Theory: Analysis of Conflict+

    y: Analysis of Conflict+Game Theory for Applied Economists:北大光华学习资料,翁盒老师主讲 高级微观经 济专题 北大光华 翁翕老师主讲 Topics in Advanced Micro economics 更详细的内容,请参考下面的“内容...

    05_repeated限定修饰符测试代码.rar

    博客中测试代码 【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符 博客网址:https://blog.csdn.net/dengjin20104042056/article/details/102465638

    plsqldev13.0.0.1882x32主程序+ v12中文包+keygen

    The newly typed text will be repeated on each line of the column selection. You can now increase or decrease the editor font size by pressing Ctrl +/- on the numeric keypad. The "Go to line" function...

    Python for Bioinformatics 第二版,最新版

    3.5.1 Mapping: Calling Each Value by a Name 54 3.5.2 Operating with Dictionaries 56 3.6 SETS 59 3.6.1 Unordered Collection of Objects 59 3.6.2 Set Operations 60 3.6.3 Shared Operations with Other Data...

    statistics for biology and health

    - **《Moyé: Multiple Analyses in Clinical Trials: Fundamentals for Investigators》**:讨论临床试验中多重假设检验的策略和方法,防止I型错误累积。 ### 分子进化统计方法 - **《Nielsen: Statistical ...

    MyDAC7.6.11

    Bug with TMyDump.BackupQuery repeated call is fixed 7.5.9 05-Sep-12 Rad Studio XE3 is supported Windows 8 is supported Bug with storing empty MapRules collection in DFM is fixed Bug with AV ...

    Incentive MeIncentive Mechanism for Cooperative Content Discovery in Mobile Wireless Networks: A Repeated Cooperative Game-theoretic Approach

    Incentive MeIncentive Mechanism for Cooperative Content Discovery in Mobile Wireless Networks: A Repeated Cooperative Game-theoretic Approach

    DevArt dbForge Studio for SQL Server Enterprise Edition 5.0.337

    dbForge Studio for SQL Server is a powerful IDE for SQL Server management, administration, development, data reporting and analysis. The tool will help SQL developers to manage databases, speed up ...

    protobuf文件定义及转化为java对象

    定义protobuf文件(包含enum,message,required,optional,repeated, 结构体定义中引用另一个结构体), 生成java文件,能够构建java对象,并转化为字节byte或者流,能够将流或字节转化为对象

    Building hierarchical structures for 3D scenes with repeated elements

    本文将根据提供的研究论文摘要内容,详细阐述如何构建含有重复元素(如教室中的多个桌椅组合)的3D场景层次结构,并探讨该方法的重要性和具体实现步骤。 #### 研究背景与动机 当前,由大量真实模型构成的3D场景...

    基于protobuf反射特性的pb、json相互转换的实例程序(C++)

    关于protobuf的反射特性可以参照这篇文章 ...代码中也对关键处理步骤进行了注释说明,相信有了这个实例后json、pb相互转换对你来说将变得一目了然。 使用过程如果有问题可以私信博主,我看到后会第一时间解答。

    DNA中信息的结构感知智能编码和解码(计算机博士论文英文参考资料).pdf

    The encoding process in Shoshanna Llewellyn's thesis involves mapping digital data into DNA codewords, carefully designed to avoid the aforementioned issues. The use of a graph-based approach allows ...

    SPSS Data Analysis for Univariate, Bivariate, and Multivariate Statistics

    SPSS Data Analysis for Univariate, Bivariate, and Multivariate Statistics By 作者: Daniel J. Denis ISBN-10 书号: 1119465818 ISBN-13 书号: 9781119465812 Edition 版本: 1 出版日期: 2018-09-25 pages 页数:...

Global site tag (gtag.js) - Google Analytics