中文命名之Hibernate+MySQL演示

最近有個契機, 須要在一個給定開發環境中驗證中文命名的可行性. 達成的例子源碼在: HibernateExampleZhjava

當前用的是Hibernate 3.3.2.GA. 以後測試了更多版本, 彩蛋見最後一部分.mysql

測試環境:

  • 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

功能驗證:

運行com.codeinchinese.App, 在數據庫表中插入一條記錄並進行一次條件查詢.git

數據庫

源碼以下, 與前文相比, 多了索引.github

需修改hibernate.cfg.xml中的數據庫用戶/密碼部分.sql

DROP TABLE IF EXISTS `演示`.`客戶`;
CREATE TABLE  `演示`.`客戶` (
  `客戶_ID` bigint(20) unsigned COLLATE utf8_unicode_ci NOT NULL AUTO_INCREMENT,
  `姓名` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  `建立日期` datetime COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`客戶_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;

ALTER TABLE `演示`.`客戶`
ADD INDEX `姓名_索引` (`姓名` ASC) ;

Java

模型類數據庫

public class 客戶類 implements java.io.Serializable {

  private Long 客戶_ID;
  private String 姓名;
  private Date 建立日期;

  public 客戶類() {
  }

  public 客戶類(String 姓名, Date 建立日期) {
      this.姓名 = 姓名;
      this.建立日期 = 建立日期;
  }

  public Long get客戶Id() {
      return this.客戶_ID;
  }

  public void set客戶Id(Long 客戶Id) {
      this.客戶_ID = 客戶Id;
  }

  public String get姓名() {
      return this.姓名;
  }

  public void set姓名(String 姓名) {
      this.姓名 = 姓名;
  }

  public Date get建立日期() {
      return this.建立日期;
  }

  public void set建立日期(Date 建立日期) {
      this.建立日期 = 建立日期;
  }

  @Override
  public String toString() {
    return 客戶_ID + " " + 姓名 + " " + 建立日期;
  }

}

Hibernate映射文件

客戶.hbm.xmlapache

<hibernate-mapping>
    <class name="com.codeinchinese.客戶.模型.客戶類" table="客戶">
        <id name="客戶Id" type="java.lang.Long">
            <column name="客戶_ID" />
            <generator class="identity" />
        </id>
        <property name="姓名" type="string">
            <column name="姓名" length="45" not-null="true" />
        </property>
        <property name="建立日期" type="timestamp">
            <column name="建立日期" length="19" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

檢查更多Hibernate版本

經測試, 到4.2.21.Final都正常運行, 但從5.0.0.Final開始到最近的6.0.0.Alpha2, 都不支持模型類名的中文命名. 具體報錯以下:oracle

Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 58; cvc-pattern-valid: Value 'com.codeinchinese.客戶.模型.客戶類' 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)

修改成英文類名com.codeinchinese.customer.model.Customer後, 保留MySQL中文命名和Hibernate相關映射, 仍能運行.app

考慮到這是從版本4到5的功能退化, 並且在6.0版本並沒有修復的意思, 應該提issue反饋一下.maven

參考

Maven 3 + Hibernate 3.6 + Oracle 11g Example

相關文章
相關標籤/搜索