When JPA encounters MySQL table names are all uppercase

I'm working on an HRMS recently. The function is not complicated. Press it first. Here is a small question about the soul, which is very tangled and annoying.

The company had a project that used MySQL before, and I managed it. This time this project is not big, just continue to use that MySQL. I have always known that MySQL has a case problem of field names and table names on Linux, so in order to avoid this problem, I chose all uppercase field names and table names. I said that although we are using JPA, we only need to use annotations to clearly write table names and fields. The name is capitalized, so it's okay.
It actually proves that imagination is richer than life, folks. When I use JPA to log in, I get an error saying that the table does not exist, and the table name is all lowercase. Next, the tossing has just begun. It was ten o'clock in the morning.
To solve this problem, the first step must be to change the cnf of MySQL to be case-insensitive. I thought that this would solve the problem. It turns out that the pattern Tucson is broken. . . After changing it to case-insensitive, this table can't be accessed at all. Whether it is uppercase or lowercase, show tables can be seen, but no matter how much tossing it out when selecting.
Helpless, can only change back. But I have not backed up the table structure, and it is troublesome to install an Oracle, what should I do? Since MySQL has trouble solving it, let's think of a solution in Java. Briefly analyze the process, the browser clicks the button, initiates a request to the background, the Controller finds the corresponding dao through the service after receiving the request, and the dao finds the JPA specification implemented by Hibernate through Spring Data, and then converts it into a SQL statement and sends it to the database for execution. As long as you can change the table name to uppercase before sending it will solve the problem.
So where does this change the table name to uppercase or lowercase? I don't know if students still remember, JPA has a conversion for the field, a_b will be converted into aB. This is of course not implemented by the Java kernel, this is done by Hibernate's JPA implementation. Specific to the code, this thing is org.hibernate.cfg.ImprovedNamingStrategy, the naming rule class. This is implemented by Hibernate, which is the function of converting the camel case rule. The table name in my annotation is written in uppercase, but the output sql statement is in lowercase. There is no doubt that there is code converted to lowercase. Then I just need to implement a method that converts the table name to uppercase.

public class MySQLUpperCaseStrategy extends ImprovedNamingStrategy {

private static final long serialVersionUID = 1383021413247872469L;

@Override
public String tableName(String tableName) {
return tableName.toUpperCase();
}
}

Simple and easy to use, no need to change any configuration of the original database.

Reprinted from http://xhrtechblog.com/2014/08/24/jpa_uppercase/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325490676&siteId=291194637