database table name capitalization issues when Spring Boot + JPA (Hibernate 5) was developed

Source: Internet
Author: User

These days in the Spring Boot development project, in the development process encountered a problem hibernate when executing SQL, the Always prompt table does not exist.

After finding out, the table was built with a uniform capitalization. Hibernate converts uppercase to lowercase. And MySQL is case sensitive under Linux.

Solve:

1. Try to change the cnf file of MySQL to be case insensitive. After the modification, it was found that the problem was not resolved, and a new problem arose, with the table name failing in both case and capitalization.

2. Database-level modifications have no effect, table structure and table names can not be modified, it can only be implemented through the code to solve the problem.

First of all, thank the three bloggers for the information provided


Address a http://blog.csdn.net/q979076061/article/details/51539960

Address two http://blog.csdn.net/jackyxwr/article/details/8618908

Address three http://blog.csdn.net/qinshijangshan/article/details/53444499


The first and second blogs provide me with a good idea, and then follow the practice that the default naming definition strategy for Hibernate 5 uses the Org.hibernate.cfg.ImprovedNamingStrategy class. The above blog said very clearly here will not repeat. It would be nice to have a custom naming policy that integrates this class directly.

After the code was written, the configuration did not resolve the problem. The idea is correct, can only follow the next look. After seeing the third blog post and finding out what was wrong with it, hibernate modified it after the 5.1 release.

Quote the third blog from the original article:


It turns out that the Hibernate5.1 related configuration has changed.

Reference: http://412887952-qq-com.iteye.com/blog/2315686

Reference: Http://stackoverflow.com/questions/32165694/spring-hibernate-5-naming-strategy-configuration

Hibernate.ejb.naming_strategy will no longer be supported, but replaced with two attributes:
Hibernate.physical_naming_strategy
Hibernate.implicit_naming_strategy

There are two common configurations for Physical_naming_strategy:

Org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy Org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

forPhysicalnamingstrategystandardimplhave aDefaultnamingstrategythe effect, forSpringphysicalnamingstrategyhave aImprovednamingstrategythe effect.


After looking at these two classes of source code, found that the above thinking is still possible the following is my Code

Import Org.hibernate.boot.model.naming.identifier;import Org.hibernate.boot.model.naming.physicalnamingstrategystandardimpl;import Org.hibernate.engine.jdbc.env.spi.jdbcenvironment;import org.springframework.stereotype.component;@ Componentpublic class Mysqluppercasestrategy extends Physicalnamingstrategystandardimpl {private static final long Serialversionuid = 1383021413247872469L; @Overridepublic Identifier tophysicaltablename (Identifier name, Jdbcenvironment context) {//Converts the table name all to uppercase string tableName = Name.gettext (). toUpperCase (); return Name.toidentifier ( tableName);}}


Configuration of the Yml:

#配置hibernate的命名策略 Hibernate:naming:physical-strategy:com.xx.xx.common.mysqluppercasestrategy


After testing, this problem is finally resolved.






This article is from "Rookie One" blog, please be sure to keep this source http://4528195.blog.51cto.com/4518195/1983780

database table name capitalization issues when Spring Boot + JPA (Hibernate 5) was developed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.