Hibernate.default_schema usage in Hibernate 5


#1

What does hibernate.default_schema mean to Hibernate ?

Here is my entity definition.

@ Table(name = “cluster”, schema = “schema1”)
@ SecondaryTable(name = “Cluster”, schema = “schema2”, pkJoinColumns = @ PrimaryKeyJoinColumn(name = “clusterId”, referencedColumnName = “objid”))
@ org.hibernate.annotations.Table(appliesTo = “Cluster”, optional = false)
@ Entity
@ OptimisticLocking(type = OptimisticLockType.DIRTY)
@ DynamicUpdate
public class Cluster implements Serializable {

}

If I set hibernate.default_schema to say mock_schema1 does it set for Main table(@Table) or secondary table(@SecondaryTable)?

How can I add default_schema for the Main table? it is working for a secondary table but not for the main table

I was wondering if I can use any similar hibernate property for Dbtest which can use a different schema(mock_schema1) instead of the default schema(schema1).


#2

What does hibernate.default_schema mean to Hibernate ?

It sets the default database schema. Check out the User Guide for more details.

If I set hibernate.default_schema to say mock_schema1 does it set for Main table(@ Table) or secondary table(@ SecondaryTable) ?

The default schema applies to all tables mapped by your entities.

I was wondering if I can use any similar hibernate property for Dbtest which can use a different schema( mock_schema1 ) instead of the default schema( schema1 ).

This question is not very clear. You could use the hibernate.default_catalog if you want to set the default database catalog used by Hibernate, and vary the catalog for production or testing using Maven build profiles.


#4

The below query is generated in the normal case by hibernate.
select
this_.objid as objid1_0_0_,
this_.name as name2_0_0_,
this_.resourceKey as resource3_0_0_,
this_.uuid as uuid4_0_0_,
this_1_.healthStatus as healthSt1_2_0_,
this_1_.lastSync as lastSync2_2_0_
from
schema1.cluster this_
inner join
schema2.Cluster this_1_
on this_.objid=this_1_.clusterId

In case of Dbtest, i want the query to use mock_schema2.Cluster instead of schema2.Cluster.
I want to know how to modify entity mapping metadata without modifying entity definition.


#5

With hibernate property
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver

One more thing i noticed
When i use hibernate.default_schema ZZZZZZ
Hibernate generated query contains
schema2.ZZZZZZ.Cluster this_1_
But when i use hibernate.default_catalog ZZZZZZ
schema2.Cluster this_1_
I was expecting query to be something like ZZZZZZ.Cluster this_1_


#6

Use a XML mapping to override the entity annotation anc use that for testing only.

Check out this article for more details.


#7

It is extra overhead in maintaining, we have hundreds of entities and xml override doesn’t look like a viable solution.
Instead is there a way to update catalog/schema from metadata maybe?


#8

You can write a program to generate all XML mappings based on the entities. Here’s an article about accessing the metadata.