Should Hibernate be able to handle overlapping foreign keys?

后端 未结 4 2032
难免孤独
难免孤独 2020-12-05 09:08

I have a table that has two foreign keys to two different tables with both foreign keys sharing one column:

CREATE TABLE ZipAreas
(
  countr         


        
相关标签:
4条回答
  • 2020-12-05 09:15

    This is still not solved in Hibernate 5. However, if I use @JoinColumnsOrFormulas I get ClassCastException. Appending insertable = false, updatable = false on all join columns solved my problem:

    Example:

    @ManyToOne
    @JoinColumns(value = {
        @JoinColumn(name = "country_code", referencedColumnName = "country_code", insertable = false, updatable = false),
        @JoinColumn(name = "state_code", referencedColumnName = "state_code", insertable = false, updatable = false), 
        @JoinColumn(name = "city_name", referencedColumnName = "name", insertable = false, updatable = false)})
    private City city = null;
    
    0 讨论(0)
  • 2020-12-05 09:18

    There is a way to bypass the validation and get it to work, thus indicating the column is a "@JoinColumnsOrFormulas" then put the solution:

    Error:

    @ManyToOne
    @JoinColumns(value = {
        @JoinColumn(name = "country_code", referencedColumnName = "country_code"), 
        @JoinColumn(name = "zip_code", referencedColumnName = "code")})
    private Zip zip = null;
    
    @ManyToOne
    @JoinColumns(value = {
        @JoinColumn(name = "country_code", referencedColumnName = "country_code", insertable = false, updatable = false),
        @JoinColumn(name = "state_code", referencedColumnName = "state_code"), 
        @JoinColumn(name = "city_name", referencedColumnName = "name")})
    private City city = null;
    

    OK:

    @ManyToOne
    @JoinColumns(value = {
        @JoinColumn(name = "country_code", referencedColumnName = "country_code"), 
        @JoinColumn(name = "zip_code", referencedColumnName = "code")})
    private Zip zip = null;
    
    @ManyToOne
    @JoinColumnsOrFormulas(value = {
        @JoinColumnOrFormula(formula = @JoinFormula(value = "country_code", referencedColumnName = "country_code")),
        @JoinColumnOrFormula(column = @JoinColumn(name = "state_code", referencedColumnName = "state_code")),
        @JoinColumnOrFormula(column = @JoinColumn(name = "city_name", referencedColumnName = "name"))
    })
    private City city = null;
    

    Regards,

    0 讨论(0)
  • 2020-12-05 09:22

    I am using hibernate 5 and I still get this the exception. If you add insert="false", update="false" only to one, you will get an exception stating that you mixed insertable and non-insertable columns and that this is not allowed. This is an issue that is already in the tracker, but seems not to be resolved. Hibernate throws AnnotationException on column used by multiple overlapping foreign keys

    In our case, this meant that we migrated to EclipseLink, which is in fact pretty easy given that you mainly need to replace the persistence.xml and rewrite the HSQL (Hibernate SQL) to JPQL (JPA SQL). Also you might need to replace custom naming strategies (Eclipse calls them SessionCustomizer). Of course it might be harder to do if you use special features of hibernate such as hibernate search etc. But in our case, we tried to fix overlapping foreign keys for weeks when the migration only took hours in the end.

    0 讨论(0)
  • 2020-12-05 09:30

    Will be supported with Hibernate 5, see https://hibernate.atlassian.net/browse/HHH-6221

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