17

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

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??

Thanks in advance

Below I show the SQL code in postgres that I have and I did the mapping.

I have three tables (activity, event and photo) where one of them (photo) refers to the other two (activity and event) but in a single column (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
)

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).

@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: deploy several pulls me hibernate dependencies) could anyone tell me what is my mistake or could solve this problem. Thank you.

Ikar Pohorský
  • 4,617
  • 6
  • 39
  • 56
rodrixd
  • 510
  • 2
  • 6
  • 15

2 Answers2

13

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
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
gipinani
  • 14,038
  • 12
  • 56
  • 85
  • 1
    +1: I think he's generating this Java, and the root problem is in the DDL, but this is definitely correct. – Don Roby Jan 24 '14 at 00:20
  • You have reason the name attibutes are the same , but The problem is that I need that the column of photo of this referenced with the tables: event and activity, how could I do that in the mapping? – rodrixd Jan 24 '14 at 14:23
  • I don't understand the answer, the mapping points to two different tables, or not? Why can you not have in both of those tables the same column name? – Angel O'Sphere Jul 25 '19 at 16:38
  • 1
    Yeah, but why can't I have same column with two different entities? Can anyone suggest solution to this please? – Pratik Ambani Apr 05 '21 at 09:48
10

As noted in another answer, your Java code specifies the same join-column name for two fields, which can't work.

If this Java code is generated by a netbeans mapping tool, as it seems from your note

Now the mapping I did with the help of Netbenas and gave me the following code ...

the bad Java mapping is probably caused by a bad combination of constraints in your SQL.

You have in your definition of the photo table:

  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.

If you need foreign keys from photo to both of those tables, you'll need to use two different columns.

Don Roby
  • 40,677
  • 6
  • 91
  • 113