JPA utf-8 characters not persisted - jpa

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;

Related

Enable/Disable Eclipselink Shared Cache via jboss-cli

I'm looking to be able to enable or disable the eclipselink shared cache (second level cache) via the jboss-cli.
We generally have the shared-cache-mode set to DISABLE_SELECTIVE, but in certain deployments we would like to be able to disable it. It would be nice if we could do this via the jboss-cli and avoid editing our persistence.xml.
Anyone got any ideas or experience doing anything similar?
I've included our persistence.xml below. I'd be happy to provide anything else that might be helpful.
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="AppPu">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>app.datasource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="eclipselink.persistence-context.flush-mode" value="commit" />
<property name="eclipselink.jdbc.batch-writing" value="jdbc" />
<property name="eclipselink.jdbc.batch-writing.size" value="500"/>
<property name="eclipselink.session-event-listener" value="com.demo.app.common.eclipselink.GeometryInitializer"/>
<property name="eclipselink.logging.logger" value="DefaultLogger"/>
<property name="eclipselink.logging.file" value="sqldump.log"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
In case anyone else ever attempts to do this...
Edit your persistence.xml, setting the shared-cache-mode to a variable as follows:
<shared-cache-mode>${SHARED_CACHE_MODE}</shared-cache-mode>
Then you can add this variable into your standalone.xml by sending the following command to your jboss-cli (substituting DISABLE_SELECTIVE as appropriate):
/system-property=SHARED_CACHE_MODE:add(value=DISABLE_SELECTIVE)
The following article helped us a lot with this: http://www.mastertheboss.com/jboss-frameworks/hibernate-jpa/jpa-configuration/how-to-use-an-env-variable-in-your-persistencexml

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

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

OpenJPA Exception related to JDBC Driver and connection URL in TomEE+

We are facing issue in our application with respect to the persistence configuration. The application runs on TomEE Plus (7.0) and uses Open JPA implementation. Below is the error which we are getting
<openjpa-2.4.2-r422266:1777108 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: <openjpa-2.4.2-r422266:1777108 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "oracle.jdbc.xa.client.OracleXADataSource" and URL "jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM". You may have specified an invalid URL.
We dont' get the above error when using oracle.jdbc.driver.OracleDriver. The XA driver settings were working fine on IBM WebSphere 8.5. Is there anything else which we need to configure in TomEE+ to get it working?
Update
Adding the persistence.xml file as well
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="MySchema" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>myjndi</jta-data-source>
<class>com.abc.jpa.entity.Tuser</class>
<class>com.abc.jpa.entity.Taddress</class>
<properties>
<property name="tomee.jpa.factory.lazy" value="true"></property>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.xa.client.OracleXADataSource" />
<property name="javax.persistence.jdbc.url"
value="jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM" />
<property name="javax.persistence.jdbc.user" value="userName" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
As mentioned, it works fine if I use oracle.jdbc.driver.OracleDriver as the jdbc driver class

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:

Database creation not working in eclipselink with glassfish 4 and Postgres

I want to create the database based on Entities. Configuration:
Glassfish: GlassFish Server Open Source Edition 4.1 (build 13)
Eclipselink: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd (delivered with glassfish)
Database: PostgreSQL Version: 9.4.2
Driver: PostgreSQL Native Driver Version: PostgreSQL 9.4 JDBC4.1 (build 1201)
From the moment eclipselink starts creating the database I see the following statements in the logs after putting a lot of log parameters on finest:
SELECT ID FROM table_name WHERE ID <> ID
SELECT 1
This is repeated 4-5 times. The first query gives the following obvious postgres error:
org.postgresql.util.PSQLException: ERROR: relation "table_name" does not exist
Position: 16
As a result a following queries give the following error:
org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
After this eclipselink continues creating the tables:
CREATE TABLE table_name (ID BIGINT NOT NULL, PRIMARY KEY (ID))
But produces the same error.
For some reason creating the database happens in a transaction. I found the source code responsible, but I can't find out how the transaction is started. I can give more detailed information, but maybe somebody can already help now.
Edit: The persistence.xml
<?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="h ttp://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="AntennasOperatingSystemServerPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>MyDatabase</non-jta-data-source>
<class>... bunch of classes ... </class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.logging.level.sql" value="FINEST"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.target-database" value="PostgreSQL" />
</properties>
</persistence-unit>
</persistence>
Have you tried setting the eclipselink.target-database property to PostgreSQL or org.eclipse.persistence.platform.database.PostgreSQLPlatform?
Please share your persistence.xml file if the above does not resolve the issue.
I did something similar some time ago. The following configuration work well.
<persistence-unit name="EjemploJpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:derby://localhost:1527/example"/>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="123"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
Obviously, once the tables are created, it is necessary to change the value of the property eclipselink.ddl-generation to none.
NOTE: The database was Derby, but I've also tried it with MySQL. I think that it will work with PostgreSQL.