Imagine having a tool that can automatically detect JPA and Hibernate performance issues.
Hypersistence Optimizer is that tool!
Introduction
When your relation database system uses multiple schemas, then you can instruct Hibernate to use a global schema using the hibernate.default_schema configuration property:
While Hibernate can imply the default schema whenever dealing with entity queries, for native queries, you need a little trick. This post is going to demonstrate how you can imply the default schema for native SQL queries as well.
Domain Model
One we defined a global schema, there is no need to specify the schema on the entity level:
@Entity(name = "Event")
@Table(name = "event")
public class Event {
@Id
@GeneratedValue
private long id;
private String name;
@Column(name = "created_on")
private Timestamp createdOn;
//Getters and setters omitted for brevity
}
javax.persistence.PersistenceException:
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.postgresql.util.PSQLException: ERROR: relation "event" does not exist
The event table does not exist in the default public schema, so we need to change all native queries to use the forum schema instead. While this works, it would be so nice if Hibernate would do that automatically on our behalf.
Working with a default schema is so much easier when the schema name is defined only one, by the hibernate.default_schema configuration property. While this works just fine for JPQL queries, for native queries you need to provide the {h-schema} placeholder.
Hibernate also defines h-domain and h-catalog placeholders as well. The {h-catalog} resolve the global catalog defined by the hibernate.default_catalog configuration property. The {h-domain} resolves both the global catalog and schema, injecting them like this: catalog.schema.