Encoding UTF-8 problem while JPA/hibernate execute sql-script with Intelli-J - postgresql

Im developing a Jee webapp using JPA/Hibernate as ORM framework, PostgreSQL as db, and Tomcat as server.
When i start the app i want entitymanager to inject some data in my db.
I do that whith
<property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/>
in persistence.xml
Everything works fine except that i get some bad encoding like "Ardèche" instead of "Ardèche".
My whole project is in utf-8, my database too.
I had encoding output problems in Intelli-j terminal with tomcat, that i managed to resolve using -Dfile.encoding=UTF-8 in Help | Edit Custom VM Options.
But my data in my db is still wrong, even if in Intelli-J output i get the good result.
If i execute the script straight into pgadmin there is no problem.
I tried everything i could find to solve that, but nothing worked.
I probably have conflict in the configuration now, cause i tried too many different stuff.
My persistence.xml
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/DB" />
<property name="javax.persistence.jdbc.user" value="db" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
<property name="hibernate.connection.autocommit" value="true" />
</properties>
It's my first message on stackoverflow so i hope i did everything properly !
Thanks !

thanks for your answer, i just managed to fix it by adding <property name="hibernate.hbm2ddl.charset_name" value="UTF-8"/> to persistence.xml. I didn't check enough the hibernate doc !

Related

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

Getting exceptions (Missing descriptor / No [EntityType] was found for the key class) on the below scenario

Getting exceptions (Missing descriptor / No [EntityType] was found for the key class) on below scenario.
Start the weblogic server
Deploy the WAR
Test the application ‘n’ of times
Delete the WAR from web logic admin console
Re deploy the same WAR
Basically, if we do redeploy we will got the above exception. This issue will get resolved if you restart the server.
Persistence.xml
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>DEV</jta-data-source>
<class>com.Class1</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="eclipselink.allow-zero-id" value="true"/>
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.parameters" value="true" />
<property name="eclipselink.target-server" value="WebLogic"/>
<property name="javax.persistence.query.timeout" value="120000"/>
<property name="javax.persistence.lock.timeout" value="120000"/>
</properties>
</persistence-unit>
Bean Configuration to handle spring transaction
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"/>
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="loadTimeWeaver" ref="loadTimeWeaver"/>
</bean>
<bean id="loadTimeWeaver" class="org.springframework.instrument.classloading.weblogic.WebLogicLoadTimeWeaver"/>
<bean id="em" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
The WL Server throwing this error because of the ServerSession is cached in the EntityManagerSetupImpl static, so on createEntityManagerFactory() the old one is used with the old descriptors/class before the redeployment.
**
We managed to take the EntityManagerFactory Object and closed the same
in the destroy method in one of the InitializerServlet.
**

How do I properly setup SQL logging for EclipseLink?

I want to see the generated SQL statements from EclipseLink. The persistence.xml contains the following properties:
<properties>
<property name="eclipselink.logging.file" value="/workspace/logs/JPA.log" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.logging.level.sql" value="ALL" />
<property name="eclipselink.logging.logger" value="JavaLogger"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mariadb://192.168.178.42:3306/halsol" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="pw" />
<property name="jdbc.persistence.logging" value="true" />
<property name="jdbc.persistence.logfile" value="/workspace/logs/JDBC.log" />
</properties>
The file "JPA.log" gets generated (as well as JDBC.log) but is has no contents after loading some rows from the database. I'm using EclipseLink 2.5.
I tried the same properties you have, it should work if you remove the following line:
<property name="eclipselink.logging.logger" value="JavaLogger"/>
The eclipselink.logging.logger parameter is provided for overriding default eclipselink logger.
Try either not setting this property or setting up java.util.logging within your application.

connecting to Oracle XE with myBatis using JDBC in Eclipse

I'm using Eclipse with Maven, in myBatis-config.xml I have the following codes. The H2 part of the code works as I can connect to H2 with my program and access the database. The Oracle part of my code doesn't work. I'm using ORACLE DATABASE XE 11.2, application express with a workspace: test, username: name, password: 123. When I run a testing class in Eclipse, I could pass the H2 tests, but when I run the same test using oracle instead, it gets an error. "Error selecting key or setting result to parameter object. Case: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist.
<environment id="H2">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:tcp://localhost:9096/sample/testDB" />
<property name="username" value="sa" />
<property name="password" value="123" />
</dataSource>
</environment>
<environment id="ORACLE">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="123" />
</dataSource>
</environment>
Hello reading the documentation from the official site of MyBatis I could obtain the following information:
In case of using the multi-db feature you will need to inform the databaseIdProvider property in the following way:
In case of using the multi-db feature you will need to inform the databaseIdProvider property in the following way:
<bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="SQL Server">sqlserver</prop>
<prop key="DB2">db2</prop>
<prop key="Oracle">oracle</prop>
<prop key="MySQL">mysql</prop>
</props>
</property>
</bean>
<bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
<property name="properties" ref="vendorProperties"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
<property name="databaseIdProvider" ref="databaseIdProvider"/>
</bean>
Hope it has been helpful.
Greetings.
NOTE Since 1.3.0, configuration property has been added. It can be specified a Configuration instance directly without MyBatis XML configuration file. For example:
mybatis.org/spring/es/factorybean.html

Mybatis caching and concurrent modfications

I am using mybatis 3.0.4 for a test against a mysql 5.5 database with mysql-connector JDBC driver, version 5.1.16.
The problem I am experiencing is that if I get a SqlSession via openSession() method and i retrieve data via a select from database, subsequent selects in same session are not aware of changes made (and committed) to database even if i call clearCache() on session. To concurrently modify database I am using Mysql command line client. Setting cacheEnabled as false in configuration file doesn't help too.
I enclose configuration file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="cacheEnabled" value="false"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="poolMaximumIdleConnections" value="20"></property>
<property name="poolMaximumActiveConnections" value="80"></property>
<property name="poolMaximumCheckoutTime" value="600"></property>
<property name="poolTimeToWait" value="600"></property>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/TestMapper.xml" />
</mappers>
</configuration>
Ok, solved by myself. It was not a mybatis issue, it depended on Transaction isolation on JDBC driver, which was by default TRANSACTION_REPEATABLE_READ, I needed TRANSACTION_READ_COMMITTED instead. Solved with:
getSessionFactory().openSession(TransactionIsolationLevel.READ_COMMITTED);