Hibernate 4 + MySQL demonstration of Chinese naming

Recently, there is an opportunity to verify the feasibility of Chinese naming in a given development environment. The source code of the example is as follows: HibernateExampleZh

Currently, Hibernate 3.3.2.GA is used. Later, more versions are tested. See the last part for eggs

Test environment:

  • Windows 7 Pro 64bit, JDK 1.7.0_80, MySQL 5.5.62, Eclipse Kepler SR2
  • MacOS 10.13.6, JDK 1.8.0_45, MySQL 5.5.24, Eclipse 4.7.3a

Functional verification:

Run com.codeinchinese.App, insert a record in the database table and query the condition once

data base

Source code As follows, Preceding text By contrast, there are many indexes

The database user / password part of hibernate.cfg.xml needs to be modified

DROP TABLE IF EXISTS `Demonstration`.`Customer`;
CREATE TABLE  `Demonstration`.`Customer` (
  `Customer_ID` bigint(20) unsigned COLLATE utf8_unicode_ci NOT NULL AUTO_INCREMENT,
  `Full name` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  `Date of creation` datetime COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`Customer_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;

ALTER TABLE `Demonstration`.`Customer`
ADD INDEX `Full name_Indexes` (`Full name` ASC) ;

Java

Model class

public class Customer class implements java.io.Serializable {

  private Long Customer_ID;
  private String Full name;
  private Date Date of creation;

  public Customer class() {
  }

  public Customer class(String Full name, Date Date of creation) {
      this.Full name = Full name;
      this.Date of creation = Date of creation;
  }

  public Long get Customer Id() {
      return this.Customer_ID;
  }

  public void set Customer Id(Long Customer Id) {
      this.Customer_ID = Customer Id;
  }

  public String get Full name() {
      return this.Full name;
  }

  public void set Full name(String Full name) {
      this.Full name = Full name;
  }

  public Date get Date of creation() {
      return this.Date of creation;
  }

  public void set Date of creation(Date Date of creation) {
      this.Date of creation = Date of creation;
  }

  @Override
  public String toString() {
    return Customer_ID + " " + Full name + " " + Date of creation;
  }

}

Hibernate mapping file

Customer.hbm.xml

<hibernate-mapping>
    <class name="com.codeinchinese.Customer.Model.Customer class" table="Customer">
        <id name="Customer Id" type="java.lang.Long">
            <column name="Customer_ID" />
            <generator class="identity" />
        </id>
        <property name="Full name" type="string">
            <column name="Full name" length="45" not-null="true" />
        </property>
        <property name="Date of creation" type="timestamp">
            <column name="Date of creation" length="19" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

Check for more Hibernate versions

After testing, it is normal to 4.2.21.Final, but from 5.0.0.Final to the latest 6.0.0.Alpha2, Chinese naming of model class name is not supported. The specific error is as follows:

Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 58; cvc-pattern-valid: Value 'com.codeinchinese.Customer.Model.Customer class' is not facet-valid with respect to pattern '([a-zA-Z_$][a-zA-Z\d_$]*\.)*[a-zA-Z_$][a-zA-Z\d_$]*' for type 'ClassNameType'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:452)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3230)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processOneAttribute(XMLSchemaValidator.java:2825)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(XMLSchemaValidator.java:2762)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2050)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:740)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:570)
    at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:101)
    at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:75)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(StAXEventConnector.java:261)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:130)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:460)

After the English class name is changed to com.codeinchinese.customer.model.Customer, the MySQL Chinese name and Hibernate related mapping are preserved and can still run

Considering that this is a functional degradation from version 4 to 5, and there is no intention of repairing in version 6.0, issue feedback should be provided

Reference resources

Maven 3 + Hibernate 3.6 + Oracle 11g Example

Tags: Java Apache xml Hibernate

Posted on Mon, 04 Nov 2019 12:20:16 -0500 by brucec