JPA @JoinColumn一对多的双向关系,复合键为FK

时间:2017-02-15 09:21:40

标签: java hibernate jpa jpa-2.0 one-to-many

我已经对这个问题持续了好几个星期了,而且我不知道如何绕过它。

我有两张桌子:

  • 作者(author_code,author_number,author_name)author_code和author_number是主键
  • 标题(author_code,title_code,sequence,title_desc)title_code和序列是主键

代码:

        @Entity
        @IdClass(AuthorPK.class)
        @Table(name="Author")
        public class Author implements Serializable {

            @Id
            @Column(name="author_code")
            private long authorCode;

            @Id
            @Column(name="author_number")
            private String authorNumber;

            @Column(name="author_name")
            private String authorName;

            //bi-directional many-to-one association to Title
            @OneToMany(mappedBy="author")
            private List<Title> titles;

            //getter setters
        }

        public class AuthorPK implements Serializable {

                private long authorCode;

                private String authorNumber;

                //getter equals() hashCode()
        }



    @Entity
    @IdClass(TitlePK.class)
    @Table(name="Title")
    public class Title implements Serializable {

        @Id
        @Column(name="title_code")
        private long titleCode;

        @Id
        @Column(name="sequence")
        private long sequence;

        @Column(name="title_desc")
        private String titleDesc;

        //bi-directional many-to-one association to Author
        @ManyToOne
        @JoinColumns({
            @JoinColumn(name="author_code", referencedColumnName="author_code"),
            @JoinColumn(name="author_number", referencedColumnName="author_number")
        })
        private Author author;

    //getters setters
    }

public class TitlePK implements Serializable {

    private long titleCode;

    private long sequence;

//getter equals() hashcode()
}

我需要通过author_code链接这两个实体,但是JPA要求我在@JoinColumn中包含这两个ID ...它因为源表没有其他列而在我的应用程序上抛出错误。我还有另一种方式加入这些实体吗?

0 个答案:

没有答案
相关问题