0

I have problem with settings relationship one to many. I want to create relationship between FindingAid and FindingAidVersion. I tried example in Hibernate documentation and many examples in the Internet but I don't know what is wrong.

FindingAid.java

    Public class FindingAid implements Serializable {
        private String id;
        ....
        @OneToMany
        @JoinColumn(name="id")
        private  Set<FindingAidVersion> listVersion = new HashSet<FindingAidVersion();
         ...... generate getter and setter .....
     }

FindingAidVersion.java

        Public class FindingAidVersion implements Serializable {
          private String id;
          private Date closeDate;
          private FindingAid findingAid;
          .......
          @ManyToOne
          @JoinColumn(name = "id", nullable = false)
          public FindingAid getFindingAid() {
                return findingAid;
            }
         ...... generate getter and setter .....
        }

Application code is:

    FindingAid data = new FindingAid();
    data.setCreateDate(new Date());
    data.setName("daniel");

    FindingAidVersion verse = new FindingAidVersion();
    verse.setCloseDate(new Date());
    verse.setIsClose(false);


    data.getListVersion().add(verse);
    this.getSession().save(data);

    this.getTx().commit();

Error is: Repeated column in mapping for entity: cz.tacr.elza.api.model.FindingAidVersion column: id (should be mapped with insert="false" update="false")

I know that problem is in annotation @JoinColumn but I am lost.

Thanks for your suggestions.

Daniel Kohout
  • 31
  • 1
  • 6
  • can you add insert="false" update="false" in FindingAidVersion mapping and try it, because error says it. – Nitesh Virani Aug 06 '15 at 13:15
  • 1
    The `@OneToMany` shouldn't be accompanied with `@JoinColumn`. Instead, you should use: `@OneToMany(mappedBy = "findingAid")` – Ori Dar Aug 06 '15 at 13:15

1 Answers1

0

Have you tried to do what the error message suggest ?

@JoinColumn(name = "id", nullable = false, insert=false, update=false)

And in FindindAid for the listVersion attribute try

@OneToMany(mappedBy="findingAid")

Look at your new error:

 foreign key constraint "fk_3o68boae9f3oamcm6dfy77tfw" cannot be implemented Detail: Key columns "findingaid" and "id" are of incompatible types: bytea and character varying

On one table (finding_aid_version ) you have "findingAid bytea" and on the other (finding_aid) "id varchar(255)",

Erwan C.
  • 709
  • 5
  • 18
  • **I tried your sollution, but now it shows this error:** `2015-08-06 18:14:11.134 ERROR 8928 --- [nio-8080-exec-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table finding_aid_version add constraint FK_3o68boae9f3oamcm6dfy77tfw foreign key (findingAid) references finding_aid 2015-08-06 18:14:11.135 ERROR 8928 --- [nio-8080-exec-1] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: foreign key constraint "fk_3o68boae9f3oamcm6dfy77tfw" cannot be implemented Detail: Key columns "findingaid" and "id" are of incompatible types: bytea and character varying.` – Daniel Kohout Aug 06 '15 at 16:16
  • can you post your db script ? – Erwan C. Aug 06 '15 at 16:20
  • `Hibernate: create table finding_aid ( id varchar(255) not null, date_create date not null, is_delete boolean, name varchar(100) not null, primary key (id) ) Hibernate: create table finding_aid_version ( id varchar(255) not null, date_close date not null, findingAid bytea, is_close boolean not null, primary key (id) ) Hibernate: alter table finding_aid_version add constraint FK_3o68boae9f3oamcm6dfy77tfw foreign key (findingAid) references finding_aid` – Daniel Kohout Aug 06 '15 at 16:23
  • i edited your question. It is easier to read and to see for other people – Erwan C. Aug 06 '15 at 16:27
  • I didnt wrote any error. And why did you delete my edition ? it was more readable – Erwan C. Aug 06 '15 at 20:03
  • Sorry I didn't know that I have deleted your edition – Daniel Kohout Aug 09 '15 at 12:35