JPA 2: multiple column usage in foreign keys

前端 未结 2 1137
萌比男神i
萌比男神i 2021-02-19 05:27

I am using Hibernate as persistence provider and modelling my entities with JPA 2.

Now a question came up and i hope you can help me.

In my application you can

相关标签:
2条回答
  • 2021-02-19 05:53

    Are you sure you can't use insertable = false, updateable = false for these @JoinColumns?

    As far as I understand, you can initialize gameid once by setting game property, and after that you don't need to change it since Tiles and Groups belong to the same Game.

    0 讨论(0)
  • 2021-02-19 05:59

    You need to do this:

    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="gameid", referencedColumnName = "gameid", insertable = false, updatable = false ),
        @JoinColumn(name="groupTag", referencedColumnName = "grouptag", insertable = false, updatable = false)
    })
    private Group group;
    

    EDIT: as mentioned in the comments, @JoinColumn is a repeatable annotation (since Java 8) that doesn't need wrapping. This simplifies the solution to:

    @ManyToOne
    @JoinColumn(name="gameid", referencedColumnName = "gameid", insertable = false, updatable = false ),
    @JoinColumn(name="groupTag", referencedColumnName = "grouptag", insertable = false, updatable = false)
    private Group group;
    
    0 讨论(0)
提交回复
热议问题