How to resolve the Hibernate global database schema and catalog for native SQL queries
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.
The best way to implement equals, hashCode, and toString with JPA and Hibernate
Bytecode enhancement and toString Last week, Mark Struberg, who is an Apache Software Foundation member and OpenJPA contributor, made the following statement:
How to use external XML mappings files (outside of JAR) with JPA and Hibernate
Introduction Flemming Harms has asked a very good question on Twitter: Basically, we want to move the JPA XML mappings outside of the application JAR so that we can change the mapping without affecting the jar file.
How to update only a subset of entity attributes using JPA and Hibernate @DynamicUpdate
Introduction One of my Twitter followers asked me to answer the following question on StackOverflow. This article aims to explain various techniques for controlling the UPDATE SQL statement that is executed whenever an entity is modified by the data access layer.
The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate
Introduction StackOverflow is a neverending source of great questions. This time, we are covering this question about using Java 1.8 Optional with JPA and Hibernate. Java 1.8 introduced the java.util.Optional container object that may or may not contain a certain value. Combining Optional and streams is very handy. Therefore, you might want that some nullable entity attributes be exposed as Optional. This article is going to demonstrate what are caveats of using Optional with entity attributes, and how you can overcome them.
How to customize the JDBC batch size for each Persistence Context with Hibernate
Introduction JDBC batching has a significant impact on reducing transaction response time. As previously explained, you can enable batching for INSERT, UPDATE and DELETE statements with just one configuration property: However, this setting affects every Persistence Context, therefore every business use case inherits the same JDBC batch size. Although the hibernate.jdbc.batch_size configuration property is extremely useful, it would be great if we could customize the JDBC batch size on a per Persistence Context basis. This article demonstrates how easily you can accomplish this task.
How to implement a custom basic type using Hibernate UserType
Introduction In this article, we are going to see how we can implement a custom type using the Hibernate UserType interface. If you wonder why you’d ever want to do this, then check out this question. Our Hibernate user wants a resilient CharacterType that works with NULL or empty values. To make it even more interesting, we are going to make it work even if the underlying database column contains more than one character.
The best way to lazy load entity attributes using JPA and Hibernate
Introduction When fetching an entity, all attributes are going to be loaded as well. This is because every entity attribute is implicitly marked with the @Basic annotation whose default fetch policy is FetchType.EAGER. However, the attribute fetch strategy can be set to FetchType.LAZY, in which case the entity attribute is loaded with a secondary select statement upon being accessed for the first time. This configuration alone is not sufficient because Hibernate requires bytecode instrumentation to intercept the attribute access request and issue the secondary select statement on demand.

