How to replace a @JoinColumn with a hardcoded value?

后端 未结 2 1610
再見小時候
再見小時候 2020-12-22 06:33

For more context, please see my other question.

I want to join my Employee entity class to Code entity class but the Code PK is composite (

相关标签:
2条回答
  • 2020-12-22 06:49

    This should work?

    @JoinColumn(name = "EMP_TYPE_CODE", referencedColumnName = "CODE")
    @Where(clause = "domain = 'EMP_TYPE'")
    @ManyToOne(optional = false)
    private Code sharedStatute;
    
    0 讨论(0)
  • 2020-12-22 07:00

    Note: Apparantly this solution is OpenJPA only.

    There is a way to do this with javax.persistence annotations only:

    @JoinColumns({
        @JoinColumn(name = "EMP_TYPE_CODE", referencedColumnName = "CODE"),
        @JoinColumn(name = "CODE_TABLE_NAME.DOMAIN", referencedColumnName = "'EMP_TYPE'")})
    @ManyToOne(optional = false)
    private Code sharedStatute;
    

    Note the single quotes in the referencedColumnName, this is the String value you are looking for.

    There is a downside however, when you have multiple Code objects in your entity, the join on DOMAIN will be done only once in jpa, giving bad results. At the moment I circumvent this by making those field load lazily, but that's not ideal.

    More information here.

    0 讨论(0)
提交回复
热议问题