Several Schemas X ManyToOne


My scenery:

I have a project that uses three different schemas in MYSQL DataBase:

I would like to have in this same project, in the same instance of MYSQL, three other schemes:

And, being able to map this situation through Hibernate annotations, knowing that for each set of schemas a WAR file (application) will be associated.

WAR_1 for PlanAd_1; PlanAd_files_1; PlanAd_log_1
WAR_2 for PlanAd_2; PlanAd_files_2; PlanAd_log_2

I can even set the value of hibernate.default_schema in my JPA configuration file,
but there is a problem that occurs in the passage between MANYTOONE relationships for example.

If I have one table in PlanAd_1 and one in PlanAd_Files_1, and there is a ManyToOne relationship between them

Table (name = “protocol_name”)

A table inside PlanAd_Files_1 has an attribute:

JoinColumn (name = “Destination_id”)
private Destination first destination;

which I point to with the Target table that is in the PlanAd_1 schema.
When doing the mapping Hibernate can not understand that the Target table is in the other schema PlanAd_1
and tries to locate it within the schema of the table “protocol_name”, causing a table error not found.

How can I indicate for this relation of ManyToOne, in which schema to look for another table, or another side of the N-to-1 relation.


First of all, MySQL does not support schemas. It only supports catalogs.

So, I suppose you have multiple catalogs there. You should use the hibernate.default_schema property which should work for associations as well. If it does not, you need to replicate the issue, and send us a Pull Request that demonstrates it.

Otherwise, you can use XML files instead of annotations and supply the right catalog-specific mappings at build time, as explained in this article.