How to specify character encoding for h2 in memory database scripts? - jpa

I have troubles with character encoding in my JPA test class.
In my h2 in memory database I have this insert query :
INSERT INTO MYTABLE (ID,FIELD1,FIELD2) VALUES (100,'ABC','Réclamation');
(please notice the "é" character in "Réclamation")
In my JUnit Test, I try to assert that the value of FIELD2 column is equal to "Réclamation" (which is the case as you can see)
But it fails with the following error :
org.junit.ComparisonFailure: expected:R[é]clamation but
was: R[�]clamation
I wonder if there is a way to specify character encoding in persistence.xml file (maybe ? or somewhere else)
Here is my persistence.xml test file :
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myTestPU">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.myproject.myclass1</class>
<class>com.myproject.myclass2</class>
<properties>
<property
name="javax.persistence.schema-generation.database.action"
value="none" />
<property
name="javax.persistence.sharedCache.mode"
value="ENABLE_SELECTIVE" />
<property
name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:ddl/schema.sql'\;RUNSCRIPT FROM 'classpath:ddl/data.sql'" />
<property
name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property
name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property
name="hibernate.show_sql"
value="true" />
<property
name="hibernate.format_sql"
value="true" />
</properties>
</persistence-unit>
</persistence>
I've already tried these solutions :
adding the following properties in persistence.xml test file :
<property
name="hibernate.connection.characterEncoding"
value="utf8" />
<property
name="hibernate.connection.useUnicode"
value="true" />
<property
name="hibernate.connection.charSet"
value="UTF-8" />
adding ?useUnicode=yes&characterEncoding=UTF-8 in my URL property
None of them worked for me...
NB : I don't use spring framework in my application

I had my insert queries in an import.sql file and those values were encoded incorrectly.
Adding the property
<property name="hibernate.hbm2ddl.charset_name" value="UTF-8" />
fixed it for me.
If you have an older version of hibernate (pre 5.2.3 apparently), maybe try the other solutions in this thread:
Hibernate/JPA import.sql utf8 characters corrupted

Related

Hibernate - Postgresql properties access

I am not able to access the postgresql db properties, which are in small letters. I would like to access the properties using camelCase. Please suggest me the required configuration .
for configure postgreSQL using JPA you can use this persistence.xml
For that you have to create one folder called "META-INF" in your project. and in that folder you have to store this file.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="give_some_name" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/database_name" />
<property name="javax.persistence.jdbc.user" value="username" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Or using hibernate.cfg.xml
(Here no need to create "META-INF folder")
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/database_name</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Show logging parameter for (INSERT,UPDATE,SELECT) in EclipseLink JPA

I used logging in persistence.xml for EclipseLink JPA implementation, from
stackoverflow reference
but the parameters appears as ? how can i show them properly in my glassfish log output window.
The log level configuration is included in the definition of the persistence unit in the persistence.xml file, as follows:
The logging of SQL parameters can be enabled, or disabled through the following properties:
Disable:
Enable:
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
full persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ProjPU" transaction-type="JTA">
<jta-data-source>jdbc/POS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.cache" value="FINEST" />
<property name="eclipselink.logging.parameters" value="true" />
</properties>
</persistence-unit>
</persistence>
Note: Setting eclipselink.logging.level to FINE is not sufficient (as of EclipseLink 2.4.0 - Juno), you have to set eclipselink.logging.level.sql to FINE.
This property will also control how parameters are logged in exceptions. By default parameters are only logged for log level < CONFIG.
Refernce:Documentation,Wiki Ecipse link

Integrate BoneCP and EclipseLink with LOCAL_RESOURCE transaction-type

I am setting up a minimal working example where I use EclipseLink in a Java EE (JSF) project. My example works perfectly with the following configuration and code:
persistence.xml
<persistence
version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
>
<persistence-unit name="issat_PU_Local" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>pro.entity.Utilisateur</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/issat" />
<property name="javax.persistence.jdbc.user" value="java" />
<property name="javax.persistence.jdbc.password" value="JaVa" />
<property name="eclipselink.logging.level.sql" value="FINE" />
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Since I don't want to use JTA I am using RESOURCE_LOCAL as transaction type and perfectly leaving with "manually" handling transaction inside my DAO.
I now want to use BoneCP connection pool with my example.
How should I precisely modify my persistence.xml to do that? I found many configuration examples in the internet using also Hibernate, but I want to stay with EclipseLink.
I couldn't do it with bonecp, but I could get a pool connection with C3PO.
I followed this link : http://javabeginnerstutorial.com/hibernate/connection-pooling-with-hibernate-4/
I copied the dependency and the properties but, instead of <property name="hibernate.c3p0.min_size">1</property> put the value in the tag like this<property name="hibernate.c3p0.min_size" value="1"/>, otherwise I got an error.
Finally mi persitence.xml look this way:

JPA utf-8 characters not persisted

I have a simple web application where I use JPA.
I have an entity called BlogEntry.
When I submit a new BlogEntry, when I debug my application I see the utf8 characters just fine.
For example
em.persist(entity);
In this line, if I debug for example:
entity.getTitle()
I can successfuly see utf-8 characters in the IDE. ( like ğğ, or çç )
Also, my database has UTF8 collation and I can insert utf-8 characters just fine with sql using like "INSERT INTO..."
However, with JPA, the characters are persisted as ????
Why might this be?
Regards.
Here is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tugay.blog.core.model.Blogentry</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/blogdatabase"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="aabbccdd"/>
</properties>
</persistence-unit>
</persistence>
use the character encoding in the property of persistence.xml file
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/blogdatabase?useUnicode=yes&characterEncoding=UTF-8"/>
This helped in Spring Boot:
spring.datasource.url=jdbc:mysql://localhost:3306/securitydb?useUnicode=yes&characterEncoding=UTF-8
This solved it nicely:
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
Edit: with hibernate 4.3.1 this works:
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">utf-8</property>
Another common mistake can be the wrong encoding of the database.
If you just cerated the database without the correct encoding this error can also be a result.
use
create database mydb character set utf8 collate utf8_general_ci;
instead of
create database mydb;

I have just configured a new datasource on jboss

And created a new user, I have granted it select/update delete rights on specific tables as required.
Will this be enough to use persistence, or will jboss need admin rights as well ?
No, it shouldn't need admin rights.
Easiest answer - try it and see.
No, no need to assign admin rights. It will enough by using persistence.xml
please add following in your persistence.xml
<persistence-unit name="abc" transaction-type="JTA"> and
<jta-data-source>java:jboss/datasources/data_source_name</jta-data-source>
persistence.xml should like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="abc" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/data_source_name</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- Map entity classes -->
<class> com.packege.classname </class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/DB_NAME" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
and provide the persistence unit name to createEntityManagerFactory method in Hibernate util class.
Persistence.createEntityManagerFactory("abc");