Java example of hibernate validator CDI to inject default Java bean validation classes implementations e.g. javax.validation.ValidatorFactory and javax.validation.Validator with hibernate-validator-cdi dependency. Also learn to inject specialized validator if application has multiple validator implementations like org.hibernate.validator.cdi.HibernateValidator. Please note that if your application runs in an environment which provides this integration out of the box, then you […]
Learn to solve java exception
UnexpectedTypeException while using hibernate validator for java bean validations.
Java example to use hibernate validator to validate Java bean fields. Bean validation API 2 (JSR-380) offers some popular annotations that can be attached to each bean property for the purpose of maintaining data integrity. 1. Maven dependencies Below are required hibernate validator maven dependencies. Bean validation allows expressions inside the error messages. To parse […]
As you know that hibernate does not support lazy initialization for detached objects. Typically, you load an entity using hibernate session and work-through it and then close the session. After closing the session, entity becomes detached entity, so it cannot make further calls to database until again associated with a new session. But loading whole […]
If you are working on Hibernate and HSqlDB, and you are trying to fetch collection of entities, then you may face this error. For single entity you may or may not find this error. Exception will look like this: Solution This error is due to version mismatch of hibernate and hsqldb. Trying updating latest verion […]
You might face this exception if you are trying execute stored procedure using JPA 2.1 and hibernate’s entity manager, and you are not using the entity manager’s registerStoredProcedureParameter() correctly. Incorrect usage may look like below OR the one you have written. Exception will look like this: Solution To fix this error, use correctly the registerStoredProcedureParameter() […]
You might face this exception if you are trying execute any query OR stored procedure, and parameter are not set in correct order. It may happen that accidentally you are setting a String type column to a Integer type column. Exception will look like this: Solution To fix this error, make sure you are mapping […]
In this tutorial, I am giving an example of executing Stored Procedures in with the help of @NamedStoredProcedureQuery annotation. I am using HQLDB (in-memory database) for backend DB in this example. I will first create a stored procedure during start of application, then will run it using EntityManager.createStoredProcedureQuery() method. Table of Contents Maven Configuration Declaring […]
You may get NotYetImplementedException exception when you are trying to include some native SQL queries in your project as named native queries, and you do not expect any result after execution of those sql queries. e.g. SQL UPDATE queries which does not return any result. Problem Let’s say you want to write an update query […]
Hibernate criteria queries example. Learn to use Restrictions with criteria, paging result sets, unique results along with projections and aggregates.
In this HQL tutorial, learn what is hibernate query language, hql syntax for various statements, named queries and native sql queries, associations and aggregations etc.
In previous post, we learned about most commonly used JPA annotations in hibernate and we also learned there about the hibernate annotations which complement those JPA annotations. Using JPA annotations makes your application code portable to other JPA implementations which is a good thing to have. Apart from JPA annotations, hibernate have some of it’s […]
This Hibernate (or JPA 2) Persistence Annotations Tutorial contains overview of all important annotations which you may need while annotating your java POJOs to make them act as persistent JPA entities. This tutorial first defines a POJO “EmployeeEntity“, define some attribute inside it and also has respective getter and setter methods. As we learn the […]
A you may know that prior to the inline annotations, the only way to create hibernate mappings was through XML files. Although various tools from Hibernate and third-party projects allowed part or all of these mappings to be generated from Java source code automatically. Today annotations is the newest way to define mappings but it […]
In any application, hibernate fetches data from databse either in eager or lazy mode. Hibernate lazy loading refer to strategy when data is loaded lazily, on demand. 1. Hibernate lazy loading – why we need it? Consider one of common Internet web application: the online store. The store maintains a catalog of products. At the […]
Learn JPA Cascade Types and how they are related to hibernate Cascade Types. Also see an example of hibernate cascadetype annotation usage in casecade tye orphan removal example.
In previous tutorial, we learned about saving entities using save() or saveOrUpdate() methods in hibernate. There we learned that in most of the cases, you will not even need those methods as hibernate manages the updates on persistent entities automatically. You only need to care about transient objects in most of cases. In this tutorial, […]
hibernate works only with persistent entities and persistent entities are classes which are attached to any hibernate session. Please note that creating an instance of a class, you mapped with a hibernate annotations, does not automatically persist the object to the database. It must be save explicitly after attaching it to a valid hibernate session. […]
Learn to get hibernate entity by id using either session.load() or session.get() method. Learn the difference between get vs load method to fetch entity by id from database. 1. Hibernate load entity – session.load() Hibernate’s Session interface provides several overloaded load() methods for loading entities from the database. Each load() method requires the object’s primary […]
Many times in our application, we face a situation where we have to compare two objects to check their equality for satisfying some business rules. In core java, we have already much knowledge about checking equality of objects, but in hibernate we need to take care of few extra things as well. Let’s learn what […]