244boy 244boy - 9 months ago 31
MySQL Question

Use hobernate save data to MySQL get ultraedit

I use hibernate to save data into MySQL, but I got ultraedit.

enter image description here

My code is below:

@Test
public void testA(){

Employee emp = new Employee();
emp.setEmpName("邓海");
emp.setWorkDate(new Date());

Configuration conf = new Configuration();
conf.configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction();
session.save(emp);
ts.commit();
session.close();
sf.close();

}


And my .hbm.xml is below:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.ypd.a.entity">
<class name="Employee" table="t_employee">

<id name="empId" column="id">
<generator class="native"/>
</id>

<property name="empName" column="emp_name"></property>
<property name="workDate" column="workDate"></property>

</class>
</hibernate-mapping>


My hibernate.xml is :

hibernate-configuration >
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="hibernate.show_sql">true</property>
<property name="javax.persistence.validation.mode">none</property>
<mapping resource="Employee.hbm.xml"></mapping>
</session-factory>

</hibernate-configuration>


Edit

enter image description here

If there is some settings I don't know? please show me the correct setting.

Answer Source

Try this in your sessionFactory:

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>

You should appoint out the charset when you communication with the server.