http://www.muxuanli.com/lmx/
一对多双向关联(类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"限制。
分享到:
相关推荐
( org.hibernate.MappingException: Repeated column in mapping for entity:YOUR_ENTITY ),因为表databsae不能具有重复的列名。 JPA定义了@AttributeOverride批注来处理此senario。 @AttributeOverrides({ @...
SPSS Repeated measures ANOVA
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....
In this contribution, we propose a TFCI coding scheme for 1.28 Mcps TDD option when 8PSK modulation is used. In 1.28 Mcps TDD option, when QPSK modulation is used, the current TFCI coding scheme is ...
Building hierarchical structures for 3D scenes with repeated elements
y: Analysis of Conflict+Game Theory for Applied Economists:北大光华学习资料,翁盒老师主讲 高级微观经 济专题 北大光华 翁翕老师主讲 Topics in Advanced Micro economics 更详细的内容,请参考下面的“内容...
博客中测试代码 【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符 博客网址:https://blog.csdn.net/dengjin20104042056/article/details/102465638
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...
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 ...
Defending Against Man-In-The-Middle Attack in Repeated Games
Planning Repeated Degradation Testing for Products With Three-Source Variability
Incentive MeIncentive Mechanism for Cooperative Content Discovery in Mobile Wireless Networks: A Repeated Cooperative Game-theoretic Approach
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 ...
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 页数:...
It is not anymore acceptable that, in the age of information technology, millions of Euros are wasted for unnecessarily repeated tests or that patients do not obtain appropriate care because relevant ...
communications and networking.We discuss normal-form, repeated, and Markov games with examples selected from the literature.We also describe ways in which learning can be modeled in game theory, with ...
spss数据分析常用数据集:repeated.sav 统计分析及模型构建中常用的数据集; 学习软件的时候,会苦于没有数据进行实操,而其实一般分析软件都会自带数据,现在介绍如何获取SPSS软件自带的数据。 纽约时报的一篇文章...
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 ...
Three new chapters on methods for repeated measurement and other forms of clustered categorical data, including marginal models and associated generalized estimating equations (GEE) methods, and mixed...
Three new chapters on methods for repeated measurement and other forms of clustered categorical data, including marginal models and associated generalized estimating equations (GEE) methods, and mixed...