spring framework show sql parameters

See the binding parameters in JPA query – java logs

see binding parameters in jpa query

What would the world look like if log was not created for us – I ask, and you say nothing! You right, there is also another world :)

Getting back from the crazy thoughts, there is a config spring allows us to see the SQL statements that the JPA is doing behind the scenes..

In you application.yml file

...
spring.jpa.show-sql=true
...

Will show the sql statements on your logs. But it will show with ? for the values used.

To see those values also along with the query add the following on the application file.


logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace

That is it!

Create Tables From Entity Definitions From JPA Spring

How to create database tables from JPA using Hibernate

We know ORM. It will be a nightmare or a bliss based on the angle you are looking at it. This time a bliss :)

If you have started development from the java side and you have all the entities figured out, then hibernate can create the database for you from the entities.

For this example I will be assuming you are using Spring and Hibernate, with that, how do you create the database from the entities

Initial Setup assumptions

So, I will be assuming you have the spring being setup already, the configuration file for your application would look like:


jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/database
jdbc.username = root
jdbc.password = 123
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = create|create-drop|update|none

Here the values to hibernate.hbm2ddl.auto could be create – if you want to crate during the sessionFactory initiation, create-drop, update when you want to update the exiting database when there is a change on the schema or none if you don’t want to do anything with it.

No Persistence provider for EntityManager named Hibernate error

No Persistence provider for EntityManager named

If you are working on hibernate, then there are two ways to provide the configuration to it. Either you would be using hibernate.cfg.xml or persistence.xml

In both cases, hibernate would be using the information like the connection string information and classes associated with tables.

If you are using persistence.xml and you are getting No Persistence provider for EntityManager named error, then there the following would be an issue and here is how you solve those.
Continue reading No Persistence provider for EntityManager named Hibernate error

JSF App slow with JPA connection

If you are working on Java Persistence API JPA on tomcat or any other web server this would be happening if you have multiple threads going off for connections.

The rule of thumb shall be to have one EntityManagerFactory and get EntityManagers out of it. Hence we would have one factory but multiple products that would take care of closing and managing them selves.

What are the signs:

1. Do you instantiate Persistence.createEntityManagerFactory(“name”) from multiple places?
2. What do you see on Process when you run

ps -aux | grep tomcat

Do you see multiple instances

If either or both of the above have yes, then here is the solution.

The first thing have single instance of ManagerFactory


package com.enderase.persistence;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
* Singlton implementation for EntityManagerFactory
*
* @author Kaleb Woldearegay<kaleb@gullele.com>
*/
public class HibernateUtil {
private static final EntityManagerFactory entityManagerFactory;

static {
try {
entityManagerFactory = Persistence.createEntityManagerFactory("jpa");
} catch (Throwable exception) {
//log your error here
throw new ExceptionInInitializerError(exception);
}
}

public static EntityManagerFactory getEntityManager() {
return entityManagerFactory;
}
}

Then make sure you are taking care of the instances of the EntityManagers that are created from the factory using

EntityManagerFactory entityManagerFactory = HibernateUtil.getEntityManager();
EntityManager em = entityManagerFactory.createEntityManager();

Make sure to close them appropriately after using them

This should pretty much take care of the problem

 

An association from the table refers to an unmapped class Hibernate Exception

Got An association from the table

refers to an unmapped class exception while running hibernate on your app?
Here is a solution
Mostly it would be related to the hbm file of the table that it is complaining. On that hbm file, make sure if you have the class attribute on the option properly addressed like if you haven’t added the package on the top, make sure you provide the FQN of the class.

Also make sure if the hbm file is listed on the hibernate.cfg.xml/ where ever the list of the hbm files are listed.

Mostly this would solve the problem.

Adding Unicode character to mysql from Java-hibernate

How to enable/add unicode character in hibernate with mysql

Unicode is ruling, it would come handy when working with non ASCII characters like when dealing with Chinese or Ethiopian alphabet amharic characters.

With internalization being the main concern in softwares, bit it website or mobile application, there is a chance you will need unicode in your application.

Hibernate is an ORM, object relational mapper, that is being used with Java and .net. It will allow to abstract all the database related stuffs with simple interface. In this unicode hibernate tutorial, I will show how you can insert, select update and delete records with unicode.

Why do I get No provider for Entity manager Error and how should I fix it

I will show the fix from the hibernate side for MySQL. But keep in mind that unicode characters have to be enabled on the database side as well with proper encoding.

See how you can avoid could not open hibernate session error

The solution for having unicode characters to be recognized as they are passing though the hibernate world would be done on the config file.

Enabling unicode character in hibernate

Here are the steps to follow:

1. go to your hibernate.cfg.xml file

2. make your the connection url something like: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8 – make sure the the ampersand is the html-encoded ampersand.

Do you know you can have boilerplate java app with maven?

Find k complementary numbers – algorithm in Java

Could not open Hibernate Session for transaction

This problem happens when the connection to the database is disconnected or was not created at all by the application, but pool assumes connection is established.
Remedy for this could be checking credential to the database [username, password, host, port] and also checking the connection object before proceeding to database tasks like reading or updating.

Invocation of init method failed – hibernate exception of caching

While working with hibernate you might come up with this error, as I do :(
Fortunately, here is how to make your java happy:

On your hibernate.xml [ or maybe you might named it other ], there is a part describing property:

<property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 <prop key="hibernate.show_sql">true</prop>
 <prop key="hibernate.c3p0.minPoolSize">5</prop>
 <prop key="hibernate.c3p0.maxPoolSize">200</prop>
 <prop key="hibernate.c3p0.timeout">1800</prop>
 <prop key="hibernate.c3p0.max_statement">50</prop>
 <prop key="hibernate.generate_statistics">true</prop>
 <prop key="hibernate.cache.use_second_level_cache">true</prop>
 <prop key="hibernate.cache.use_query_cache">true</prop>
 <prop key="hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</prop>
 </props>
 </property>

The last two props are the ones we want to blame!!
One way to get rid of this problem would be by just making the values of those to false – hmm… would work but we would miss the capability of caching … Here is the next

Download the ehcache from this location and put it in your classpath.
Update the hibernate.xml file’s property section by adding the following:

<prop key="hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</prop>

Now you+java+hibernate+me HAPPY..
hope would help someone..