Java 引起:org.hibernate.MappingException:实体映射中的重复列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21321814/
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
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity
提问by rodrixd
I am beginner in handling JPA with maven and JBOSS, with Restful to make my application I have the following problem arose me doing DEPLOY
我是使用 maven 和 JBOSS 处理 JPA 的初学者,使用 Restful 来制作我的应用程序我在做 DEPLOY 时遇到了以下问题
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: com.company.test_resources_war_1.0-SNAPSHOTPU] Unable to build EntityManagerFactory
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: database.Photo column: fid_module (should be mapped with insert = \ "false \" update = \ "false \") "}}
Not that step, check all posles solutions, but did not find anything, can someone help me??
不是那一步,检查所有posles解决方案,但没有找到任何东西,有人可以帮助我吗??
Thanks in advance
提前致谢
Below I show the SQL code in postgres that I have and I did the mapping.
下面我展示了我在 postgres 中的 SQL 代码,我做了映射。
I have three tables (activity, eventand photo) where one of them (photo) refers to the other two (activityand event) but in a single column (photo.fid_module)
我有三个表(活动、事件和照片),其中一个(照片)指的是另外两个(活动和事件),但在一个列中(photo.fid_module)
SQL Code (enginer database-->Postgresql):
SQL 代码(工程师数据库-->Postgresql):
CREATE TABLE activity (
id_activity integer not null,
name character varying(150),
description text,
CONSTRAINT id_activity_pk PRIMARY KEY (id_activity)
)
CREATE TABLE event (
id_event integer not null,
name character varying(150),
description text,
date timestamp without time zone,
CONSTRAINT id_event_pk PRIMARY KEY (id_event)
)
CREATE TABLE photo(
id_photo integer not null,
path character varying(150),
fid_module integer not null,
CONSTRAINT id_photo_pk PRIMARY KEY (id_photo),
CONSTRAINT fk_photo_activity FOREIGN KEY (fid_module)
REFERENCE activity (id_activity) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_photo_event FOREIGN KEY (fid_module)
REFERENCE event (id_event) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
Now the mapping I did with the help of Netbenas and gave me the following code (I did the mapping for the three tables, but in presenting me the problem is in the class Photo.java).
现在我在 Netbenas 的帮助下进行了映射,并给了我以下代码(我对三个表进行了映射,但在向我展示问题时,问题出在类Photo.java 中)。
@Entity
@Table(name = "photo")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "photo.findAll", query = "SELECT p FROM Photo p"),
@NamedQuery(name = "photo.findByFidPhoto", query = "SELECT p FROM Photo p WHERE p.fidphoto = :fidphoto"),
@NamedQuery(name = "photo.findByIdPhoto", query = "SELECT p FROM Photo p WHERE p.idphoto = :idphoto")})
public class Photo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_photo")
private Integer idPhoto;
@Column(name = "path")
private Recurso fidPath;
@JoinColumn(name = "fid_module", referencedColumnName = "id_activity")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private SliderWebHome fidModule;
@JoinColumn(name = "fid_module", referencedColumnName = "id_event")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Publicacion fidModule1;
public ModuloRecurso() {
}
.......
}
I am using JPA for persistence (but do mvn clean install and mvn jboss-as: deployseveral pulls me hibernate dependencies) could anyone tell me what is my mistake or could solve this problem. Thank you.
我正在使用 JPA 进行持久化(但是执行 mvn clean install 和mvn jboss-as:部署几个拉我休眠依赖项)谁能告诉我我的错误是什么或可以解决这个问题。谢谢你。
采纳答案by Don Roby
As noted in another answer, your Java code specifies the same join-column name for two fields, which can't work.
如另一个答案中所述,您的 Java 代码为两个字段指定了相同的连接列名称,这是行不通的。
If this Java code is generated by a netbeans mapping tool, as it seems from your note
如果此 Java 代码是由 netbeans 映射工具生成的,如您的注释所示
Now the mapping I did with the help of Netbenas and gave me the following code ...
现在我在 Netbenas 的帮助下完成的映射并给了我以下代码......
the bad Java mapping is probably caused by a bad combination of constraints in your SQL.
错误的 Java 映射可能是由 SQL 中的错误约束组合引起的。
You have in your definition of the photo
table:
您在photo
表的定义中有:
CONSTRAINT fk_photo_activity FOREIGN KEY (fid_module)
REFERENCE activity (id_activity) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_photo_event FOREIGN KEY (fid_module)
REFERENCE event (id_event) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
which attempts to make the column fid_module
a foreign key referencing activity
and also a foreign key referencing event
, which can't work.
它试图使列fid_module
成为外键引用activity
和外键引用event
,但这是行不通的。
If you need foreign keys from photo
to both of those tables, you'll need to use two different columns.
如果您需要来自photo
这两个表的外键,则需要使用两个不同的列。
回答by gipinani
You have two column mapped with the same name
你有两列映射同名
@JoinColumn(name = "fid_module", referencedColumnName = "id_activity")
@JoinColumn(name = "fid_module", referencedColumnName = "id_event")
Change one of the name attribute!
更改名称属性之一!
Looking in your exception, you can read:
查看您的异常,您可以阅读:
Repeated column in mapping for entity