Browsed by
Category: Hibernate

Spring Boot Native Query Example

Spring Boot Native Query Example

Spring Boot Native Query Example: In Spring boot, database operations can be performed by implementing CrudRepository<Object, Long> in our @Repository interface. Here Object is our entity class. @Query Annotation: We can execute nativeQueries using @Query annotation. Need to make the nativeQuery boolean to true and provide the native query to value parameter for @Query annotation.   Syntax: @Query(nativeQuery=true, value=”select s.store_name, s.store_url from store s”)   Sample @Repository Interface: Store is our object entity, where we need to perform find/findall/deleteby/count etc. @CrudRepository…

Read More Read More

1,294 total views, 1 views today

JPA Predicate criteriabuilder example for beginners

JPA Predicate criteriabuilder example for beginners

JPA Predicate criteriabuilder example for beginners Predicate is an interface which extends Expression and can be used for conjunction or disjunction of restrictions.   Syntax to create a predicate: Predicate someCondition = criteriaBuilder.equal(affiliateUrlRoot.get("shop_home_url"), "mirthbees");     criteriaBuilder is a CriteriaBuilder, which can be created like this,   @PersistenceContext private EntityManager entityManager; CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); affiliateUrlRoot is the root of your entity model [shop_home_url – is your field name inside the pojo/model class], which can be created, CriteriaQuery criteriaQuery =…

Read More Read More

1,549 total views, 1 views today

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,686 total views, 3 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);   1,122 total views, 1 views today

1,122 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

1,063 total views, 3 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

7,072 total views, no 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

1,222 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

1,190 total views, no 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 [Plain Old Java Objects] 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 ?…

Read More Read More

885 total views, no 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

3,792 total views, 3 views today