Should Hibernate be able to handle overlapping foreign keys?

牧云@^-^@ 提交于 2019-11-27 20:16:26
Manu Navarro

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,

Kawu

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

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!