引起:org.hibernate.MappingException:实体映射中的重复列

时间:2014-01-23 23:55:48

标签: java hibernate postgresql maven jpa

我是使用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 \") "}}

不是那一步,检查所有的posles解决方案,但没有找到任何东西,有人可以帮助我吗?

提前致谢

下面我展示了我的postgres中的SQL代码,并进行了映射。

我有三个表(活动活动照片),其中一个(照片)引用到另外两个(活动事件),但是在一个列中( photo.fid_module

SQL代码(enginer数据库 - > 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
)

现在我在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() {
    }
    .......
}

我正在使用JPA进行持久化(但是mvn clean install和 mvn jboss-as:deploy 几个拉我hibernate依赖项)任何人都可以告诉我我的错误是什么或者可以解决这个问题。谢谢。

2 个答案:

答案 0 :(得分:12)

您有两个使用相同名称映射的列

 @JoinColumn(name = "fid_module", referencedColumnName = "id_activity")
 @JoinColumn(name = "fid_module", referencedColumnName = "id_event")

更改其中一个名称属性!

查看您的例外情况,您可以阅读:

Repeated column in mapping for entity

答案 1 :(得分:8)

如另一个答案所述,您的Java代码为两个字段指定了相同的连接列名称,这两个字段无效。

如果这个Java代码是由netbeans映射工具生成的,就像从笔记中看到的那样

  

现在我在Netbenas的帮助下做了映射,并给了我以下代码......

错误的Java映射可能是由SQL中的约束组合不良引起的。

您在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

尝试使列fid_module引用引用activity的外键以及引用event的外键,这不起作用。

如果您需要从photo到这两个表的外键,则需要使用两个不同的列。