Browsed by
Category: Hibernate

Spring Boot with CriteriaBuilder for Dynamic Search Queries

Spring Boot with CriteriaBuilder for Dynamic Search Queries

Spring Boot with CriteriaBuilder for Dynamic Search Queries: Spring Boot with CriteriaBuilder for Dynamic Search Queries: When you are developing an enterprise application, you have to provide a search option dynamically with “and” condition. Our grand father way to do is using stringbuilder to construct the query in java and return the result. But hibernate has provided criteria for the same with better performance. But in spring boot you can use JPA to get the better results. In your service…

Read More Read More

1,017 total views, 6 views today

[Solved] The type CriteriaQuery is not generic;

[Solved] The type CriteriaQuery is not generic;

[Solved] The type CriteriaQuery is not generic; It is because of the incorrect import. CriteriaQuery is available in both hibernate and persistence package. We have to import from persistence package in order to use criteriabuilder. CriteriaQuery should be imported from import javax.persistence.criteria.CriteriaQuery;   not from import org.hibernate.criterion.CriteriaQuery;   Sample snippet: @PersistenceContextprivate EntityManager em; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<AffiliateUrl> cq = cb.createQuery(AffiliateUrl.class); Root<AffiliateUrl> from = cq.from(AffiliateUrl.class);   700 total views, 1 views today

700 total views, 1 views today

How to use Set in Hibernate Example ?

How to use Set in Hibernate Example ?

How to use Set in Hibernate Example ?   Set is a collection which stores only unique values, if you like to store the duplicates then you can go with List. As we know creating a session factory is the mandatory basic one for any hibernate, creating a hibernate session factory now, package in.javadomain; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateConfig { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if(sessionFactory==null){ try { // do…

Read More Read More

567 total views, 1 views today

Difference between session and sessionfactory hibernate?

Difference between session and sessionfactory hibernate?

Difference between session and sessionfactory hibernate? Basically session will be created from the sessionfactory instances. I have listed down the few differences between the session and sessionfactory in hibernate. Sessionfactory: It is one instance per datasource/database. It is thread safe. It is a heavy weight object, because it maintains datasources, mappings, hibernate configuration information’s etc. Sessionfactory will create and manage the sessions. If you have 5 datasources/databases, then you must create 5 session factory instances. sessionfactory is an¬†immutable object and…

Read More Read More

5,077 total views, 11 views today

Java Program with Hibernate is not terminating

Java Program with Hibernate is not terminating

Java Program with Hibernate is not terminating: In hibernate you would have created the hibernate session factory to open the session and work with the databases. In that case you will face the program not termination issue sometimes. Rootcause: Once the program execution finishes session and sessionfactory should be closed, When it is not closed then you may face Java program not termination issue with hibernate. Quickfix: Close the session and sessionfactory at the last of the program execution. If…

Read More Read More

732 total views, no views today

How to set up Java Hibernate with MySQL in Eclipse ?

How to set up Java Hibernate with MySQL in Eclipse ?

Hibernate+MySQL+Java+Eclipse: How ? Today we are going to see how to integrate Hibernate with mysql in Java using Eclipse.   PreRequisite: 1. Ensure you have Eclipse Juno [you can also have some other version of eclipse, but we installed hibernate tools in eclipse Juno only. I would recommend you to download eclipse juno to install hibernate tools]. 2. Hibernate tools installed in Eclipse Juno[not mandatory, but it’s recommended to use HQL in eclipse itself]. If you do not know which…

Read More Read More

847 total views, 1 views today

How to create a hibernate mapping XML file automatically ?

How to create a hibernate mapping XML file automatically ?

How to create a hibernate mapping XML file automatically using hibernate tools?   In hibernate ORM (Object Relational Mapping) we have to use either XML or annotations to map the Java POJO with table entities.   Everyone is thinking that mapping using XML is very difficult, but it is very simple in eclipse.   Prerequisite:¬† Hibernate tools should be installed in your eclipse. Don’t you have idea on how to install hibernate tools in eclipse ? Don’t worry. Just follow…

Read More Read More

595 total views, 3 views today

How to create a sessionfactory in hibernate 4.3.6?

How to create a sessionfactory in hibernate 4.3.6?

How to create a sessionfactory in hibernate 4.3.6? Hibernate is a ORM (Object Relation mapping) framework and creating a sessionfactory is very basic one to start with hibernate. Before creating sessionfactory, it is very important to know and understand what is sessionfactory ? If you are new to hibernate then you may not knowing this, but if you have worked with hibernate already, then sessionfactory is the familiar repeated word in hibernate. that’s fine, let’s understand what is sessionfactory. Sessionfactory:…

Read More Read More

1,207 total views, 4 views today

Difference between session.get() vs session.load() in hibernate ?

Difference between session.get() vs session.load() in hibernate ?

Difference between session.get() vs session.load() in hibernate ? Both are used to retrieve the objects from the database.   Differences: 1. session.load() will return the hibernate proxy object, but session.get() will return real object. 2. session.load() will not hit the database, but session.get() will hit the database all the time. 3. session.load() will throw object not found exception when no row found, but session.get() will return null when no row found in db. session.get() Sample syntax: session.get(arg0, arg1) Eg: session.get(Author.class,2);…

Read More Read More

764 total views, 2 views today

Hibernate Criteria Duplicate Records Issue [Solved]

Hibernate Criteria Duplicate Records Issue [Solved]

Hibernate Criteria Duplicate Records Issue: Problem: Duplicate same records with hibernate join. In Hibernate if we did join, we may get duplicates sometime. Below are the possible ways to resolve the duplicate same record value issue, Possible Solutions: 1. criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 2. criteria.setProjection(Projections.distinct(Projections.property(“id”))); 3. criteria.setProjection(Projections.distinct(Projections.id())); Recommended Solution: criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); If nothing is working then change the list to set, so that duplicates will be avoided from java side. Hibernate Recommended Books: Feel free to post your comments/feedbacks.. 559 total views, no views today

559 total views, no views today