java org.hibernate.MappingException:实体映射中的重复列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13322935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.MappingException: Repeated column in mapping for entity
提问by user182944
I am using Hibernate 3.2.5. I am getting the above exception while using many-to-one mapping. The training table is having a many to one relation with Department table, i.e. One Depatement is capable of taking more than one training.
我正在使用 Hibernate 3.2.5。我在使用多对一映射时遇到上述异常。培训表与部门表是多对一的关系,即一个部门可以接受多个培训。
The exception is asking me to add insert="false" update="false"
in my hbm file. If I add this bit in hbm file, then the code works fine.
例外是要求我添加insert="false" update="false"
hbm 文件。如果我在 hbm 文件中添加这个位,那么代码工作正常。
Here is the hbm file:
这是 hbm 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.infy.model.Training" table="training">
<id name="Id" type="integer" column="ID">
<generator class="assigned"></generator>
</id>
<property name="trainerName">
<column name="TRAINER_NAME"></column>
</property>
<property name="deptId">
<column name="DEPT_ID"></column>
</property>
<property name="trainingSubject">
<column name="TRAINING_SUBJECT"></column>
</property>
<many-to-one name="departmentDetails" column="DEPT_ID"></many-to-one>
</class>
</hibernate-mapping>
If I change this line to:
如果我将此行更改为:
<many-to-one name="departmentDetails" column="DEPT_ID" insert="false" update="false"></many-to-one>
Then the code works. I want to know what is the exact reason for adding this.
然后代码工作。我想知道添加这个的确切原因是什么。
Regards,
问候,
回答by Guillaume
You have mapped the DEPT_ID column twice, here:
您已经在此处映射了 DEPT_ID 列两次:
<property name="deptId">
<column name="DEPT_ID"></column>
</property>
And here:
和这里:
<many-to-one name="departmentDetails" column="DEPT_ID"></many-to-one>
When executing a select statement, Hibernate will be fine populating two properties of your object from the same column, however when doing an insert or an update it cannot decide which property to persist in the database.
执行 select 语句时,Hibernate 可以很好地从同一列填充对象的两个属性,但是在执行插入或更新时,它无法决定将哪个属性保留在数据库中。
Why do you need two properties mapped to the same column in the first place? If you need access to the deptId, you can probably remove the deptId property and instead do
为什么首先需要映射到同一列的两个属性?如果您需要访问 deptId,您可以删除 deptId 属性,而是执行
training.getDepartmentDetails().getId()
回答by Augusto
The error message for this scenario is quite clear (you haven't put it here, but I've seen it a few times). The problem is that you've mapped the column DEPT_ID
to two different fields in your class.
这个场景的错误信息很清楚(你没有放在这里,但我已经看过几次了)。问题是您已将该列映射DEPT_ID
到类中的两个不同字段。
First, you've mapped it to the property deptId
and then to departmentDetails
. As you found out, hibernate allows to do this only if one of the mappings is configured to be insert="false" update="false"
.
首先,您已将其映射到属性deptId
,然后映射到departmentDetails
. 正如您所发现的,只有当映射之一配置为insert="false" update="false"
.
The reason is quite simple. If you would change deptId
to another id, hibernate would need to change the class that is mapped in departmentDetails
, which is quite complicated.
原因很简单。如果您要更改deptId
为另一个 id,hibernate 将需要更改映射到的类departmentDetails
,这非常复杂。
if you need to get the deptId, you can add a getDeptId method on Training
that returns departmentDetails.getId()
. And don't provide a setDeptId
.
如果您需要获取 deptId,您可以在Training
该返回上添加一个 getDeptId 方法departmentDetails.getId()
。并且不要提供setDeptId
.
回答by sandeep gupta
If you are using the same column name twice in your mapping file. might be you get mapping Exception
如果您在映射文件中两次使用相同的列名。可能是你得到映射异常
Initial SessionFactory creation failed.org.hibernate.MappingException:
Also if u mark insert=flase and update=false .
此外,如果你标记 insert=flase 和 update=false 。
if u try to update or insert in records in table or another legacy system try to update these column value. it wouldn't update or insert that filed.
如果您尝试更新或插入表或其他旧系统中的记录,请尝试更新这些列值。它不会更新或插入该文件。
Please check the below link .it will help to find your solutions.
请检查以下链接。它将有助于找到您的解决方案。
http://www.techienjoy.com/hibernate-insert-update-control.php
http://www.techienjoy.com/hibernate-insert-update-control.php
Thanks Sandeep G.
谢谢桑迪普 G。