由以下原因引起:org.hibernate.MappingException:实体映射中的重复列

2022-09-03 06:08:08

我是使用maven和JBOSS处理JPA的初学者,使用Restful使我的应用程序我有以下问题出现,我在做DEVAL

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 Code (enginer database-->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干净安装和mvn jboss-as:部署几个拉我休眠依赖关系),任何人都可以告诉我我的错误是什么或者可以解决这个问题。谢谢。


答案 1

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

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

更改其中一个名称属性!

查看您的异常,您可以阅读:

Repeated column in mapping for entity

答案 2

如另一个答案中所述,您的 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_moduleactivityevent

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


推荐