Hibernate一对多关系注释

时间:2015-08-06 13:05:01

标签: java spring hibernate entity-relationship hibernate-annotations

我遇到设置关系一对多的问题。我想在FindingAid和FindingAidVersion之间建立关系。我在Hibernate文档和互联网上的许多例子中尝试过例子,但我不知道出了什么问题。

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

应用程序代码为:

    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();

错误是:  实体映射中的重复列:cz.tacr.elza.api.model.FindingAidVersion column:id(应使用insert =“false”update =“false”映射)

我知道问题出在@JoinColumn注释中,但我迷失了。

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

您是否尝试过执行错误消息提示的内容?

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

在FindindAid中查看listVersion属性,试试

@OneToMany(mappedBy="findingAid")

看看你的新错误:

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

在一个表格(finding_aid_version)上,你有&#34; findAid bytea&#34;另一方面(finding_aid)&#34; id varchar(255)&#34;,