# Hibernate.default\_schema usage in Hibernate 5

**URL:** https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208
**Category:** Hibernate ORM
**Created:** [February 4, 2019, 4:45pm UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208 "2019-02-04T16:45:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sandeep\_kamath](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sandeep_kamath/32/817_2.png) [@sandeep\_kamath](https://discourse.hibernate.org/u/sandeep_kamath)
#### Post date: [February 4, 2019, 4:45pm UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208/1 "2019-02-04T16:45:59Z")

</div>

What does `hibernate.default_schema` mean to Hibernate ?

Here is my entity definition.

```auto
@ 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`).

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [February 4, 2019, 5:30pm UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208/2 "2019-02-04T17:30:42Z")

</div>

> What does `hibernate.default_schema` mean to Hibernate ?

It sets the default database schema. Check out the [User Guide](http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#configurations-mapping) 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.

---

<div class="post-metadata">

### Author: ![sandeep\_kamath](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sandeep_kamath/32/817_2.png) [@sandeep\_kamath](https://discourse.hibernate.org/u/sandeep_kamath)
#### Post date: [February 8, 2019, 4:21am UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208/4 "2019-02-08T04:21:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![sandeep\_kamath](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sandeep_kamath/32/817_2.png) [@sandeep\_kamath](https://discourse.hibernate.org/u/sandeep_kamath)
#### Post date: [February 8, 2019, 5:04am UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208/5 "2019-02-08T05:04:24Z")

</div>

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\_

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [February 8, 2019, 5:23am UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208/6 "2019-02-08T05:23:03Z")

</div>

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

Check out [this article for more details](https://vladmihalcea.com/how-to-replace-the-table-identifier-generator-with-either-sequence-or-identity-in-a-portable-way/).

---

<div class="post-metadata">

### Author: ![sandeep\_kamath](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sandeep_kamath/32/817_2.png) [@sandeep\_kamath](https://discourse.hibernate.org/u/sandeep_kamath)
#### Post date: [February 8, 2019, 9:48am UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208/7 "2019-02-08T09:48:22Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [February 8, 2019, 10:03am UTC](https://discourse.hibernate.org/t/hibernate-default-schema-usage-in-hibernate-5/2208/8 "2019-02-08T10:03:02Z")

</div>

You can write a program to generate all XML mappings based on the entities. Here’s [an article](https://vladmihalcea.com/how-to-get-the-entity-mapping-to-database-table-binding-metadata-from-hibernate) about accessing the metadata.
