I am trying to store gujarati in postgreSQL database using Java Spring Hibernate Project But it is storing something like this
મà«àª¦à«àª¨àª¾ àªàª¯-પરાàªàª¯ પાàªàª³ ઠવà«àª¯àªà«àª¤àª¿àª¨à«àª àªà«àªà«àª
instead of
મોદીના જય-પરાજય પાછળ આ વ્યક્તિનું ભેજું
In my database encoding is UTF-8, if I copy paste directly in postgreSQL
it is storing properly but from html
form in web application it is not storing properly.
Following is my hibernate.hbm.cfg
file
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
org.postgresql.Driver
</property>
<property name="connection.url">
jdbc:postgresql://192.168.6.51:5432/JayHind?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8
</property>
<property name="connection.username">postgres</property>
<property name="connection.password">pshiv</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<mapping class="com.models.Role" />
<mapping class="com.models.UserAttempts" />
<mapping class="com.models.UserLogin" />
<mapping class="com.models.UserRole" />
<mapping class="com.models.Program" />
<mapping class="com.models.NationalProgram" />
<mapping class="com.models.StateProgram" />
<mapping class="com.models.OtherProgram" />
<mapping class="com.models.Video" />
<mapping class="com.models.Contact" />
<mapping class="com.models.Heading" />
</session-factory>
</hibernate-configuration>
I have also used
% @ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" % >
in jsp pages this make gujarati display properly but in form submission there is still problem.
In model class
package com.models;
import static javax.persistence.GenerationType.IDENTITY;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;
import com.sun.istack.internal.NotNull;
@Entity
@Table(name = "tbl_heading_info", uniqueConstraints = {
@UniqueConstraint(columnNames = "id")})
public class Heading {
private int id;
@NotNull
@NotBlank
private String message;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "message", nullable = false)
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
/*byte ptext[] = null;
try {
ptext = message.getBytes("ISO_8859_1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String value = new String(ptext, "UTF-8");
this.message=value;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
}
}
I have also put filter in web.xml
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
Expected Output
Current Output