[英]spring boot jdbcTemplate.query throws table not found auto converting table name to upper case
i have a jdbcTemplate.query method with query bit complex with multiple left joins, so its difficult for me to implement interface based repository methods.我有一个 jdbcTemplate.query 方法,它的查询位复杂,带有多个左连接,所以我很难实现基于接口的存储库方法。
I am using a MySQL db from AWS RDS.我正在使用来自 AWS RDS 的 MySQL 数据库。
when the query is executing spring boot jpa automatically converting table name to upper case and it throws error as: table not found.当查询正在执行 spring 启动 jpa 时自动将表名转换为大写并抛出错误:找不到表。
exception is:例外是:
with path [] threw exception [Request processing failed;带路径[]抛出异常[请求处理失败; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback;嵌套异常是 org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SELECT DISTINCT test_desc FROM test_name_table
];错误的 SQL 语法 [SELECT DISTINCT test_desc FROM test_name_table
]; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "TEST_NAME_TABLE" not found;嵌套异常是 org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到表“TEST_NAME_TABLE”; SQL statement: SELECT DISTINCT test_desc FROM test_name_table
[42102-200]] with root cause org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "TEST_NAME_TABLE" not found; SQL statement: SELECT DISTINCT test_desc FROM test_name_table
[42102-200]] with root cause org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "TEST_NAME_TABLE" not found; SQL statement: SELECT DISTINCT test_desc FROM test_name_table
[42102-200] SQL 声明:SELECT DISTINCT test_desc FROM test_name_table
[42102-200]
here are the solutions i have tried:这是我尝试过的解决方案:
from application.properties:来自 application.properties:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.DefaultNamingStrategy spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect spring.jpa.hibernate.naming_strategy = org.hibernate. cfg.DefaultNamingStrategy
database url:数据库 url:
spring.datasource.dataSourceProperties.serverName=test-instance.us-east-1.rds.amazonaws.com;DATABASE_TO_UPPER=FALSE;CASE_INSENSITIVE_IDENTIFIERS=TRUE; spring.datasource.dataSourceProperties.serverName=test-instance.us-east-1.rds.amazonaws.com;DATABASE_TO_UPPER=FALSE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;
as you see above sql query, i have even put the table name inside backtick.正如您在上面看到的 sql 查询,我什至将表名放在反引号内。 but no luck.但没有运气。
I tried to rename the table name in MySQL to TEST_NAME_TABLE, but still no luck.我试图将 MySQL 中的表名重命名为 TEST_NAME_TABLE,但仍然没有运气。
any recommended fix for this?有什么推荐的解决方法吗?
its a good practice use @Table(name = "table_name")
for a model and @Column(name = "column_name")
annotation for every column in your Model.java对 model 使用@Table(name = "table_name")
并为 Model.Z93F725A07423D31C889FZ448F3 中的每一列使用@Column(name = "column_name")
注释是一个很好的做法
and use @Query("some query in SQL")
for every query you use in ModelRepository.java并对您在 ModelRepository.java 中使用的每个查询使用@Query("some query in SQL")
example:例子:
@Entity
@Table
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
@Column(name = "jobTitle")
private String jobTitle;
@Column(name = "phone")
private String phone;
@Column(name = "imagUrl")
private String imageUrl;
@Column(name = "employeeCode")
private String employeeCode;
}
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query("SELECT s FROM Employee s WHERE s.email = ?1")
Optional<Employee> findEmployeeByEmail(String email);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.