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.
I am using a MySQL db from AWS RDS.
when the query is executing spring boot jpa automatically converting table name to upper case and it throws error as: table not found.
exception is:
with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SELECT DISTINCT test_desc FROM test_name_table
]; nested exception is 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]
here are the solutions i have tried:
from 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
database url:
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. but no luck.
I tried to rename the table name in MySQL to TEST_NAME_TABLE, but still no luck.
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
and use @Query("some query in SQL")
for every query you use in ModelRepository.java
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.