JPA Entiy on synonym instead of table

眉间皱痕 提交于 2019-11-30 19:35:12

My guess is that hbm2ddl seaches specifically for tables, and not for synonyms, but that your application should indeed work as if the tables existed in the schema. Try to remove the hbm2ddl option and test your application.

EDIT: it seems my guess is true: https://forum.hibernate.org/viewtopic.php?p=2438033

I still had validation issues because my Oracle driver was not giving the right columns even after setting hibernate.synonyms=true, so instead of disabling schema validation entirely I filtered the synonym table out:

In properties:

hbm2ddl.schema_filter_provider=my.path.to.MyCustomSchemaFilterProvider

Define schema filter provider:

package my.path.to;
..
public class MyCustomSchemaFilterProvider implements SchemaFilterProvider {
    @Override
    public SchemaFilter getCreateFilter() {
        return MySchemaFilter.INSTANCE;
    }

    @Override
    public SchemaFilter getDropFilter() {
        return MySchemaFilter.INSTANCE;
    }

    @Override
    public SchemaFilter getMigrateFilter() {
        return MySchemaFilter.INSTANCE;
    }

    @Override
    public SchemaFilter getValidateFilter() {
        return MySchemaFilter.INSTANCE;
    }
}

SchemaFilter:

..
public class MySchemaFilter implements SchemaFilter {
    public static final MySchemaFilter INSTANCE = new MySchemaFilter();

    @Override
    public boolean includeNamespace(Namespace namespace) {
        return true;
    }

    @Override
    public boolean includeTable(Table table) {
        if (table.getName().toLowerCase().equals("synonymtabletoexclude")){
            return false;
        }
        return true;
    }

    @Override
    public boolean includeSequence(Sequence sequence) {
        return true;
    }
}

This was based off of https://medium.com/@horiaconstantin/excluding-hibernate-entities-from-auto-generation-bce86f8e6d94

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