0

I'm facing a similar issue as described in this thread, but my case involves using Spring Data JDBC instead of JPA. I have an entity defined as follows:

@Table("TEST_TABLE")
@Data
public class TestTableEntity {

    @Id
    private int id;

    private int order;

}

When I call the method provided by CrudRepository, I encounter the error message Incorrect syntax near the keyword 'order'. The generated query is:

Executing prepared SQL statement [SELECT TEST_TABLE.id AS id, TEST_TABLE.order AS order FROM TEST_TABLE]

To address this issue, I tried annotating the order property with @Column("\"ORDER\""). This changes the generated query to:

Executing prepared SQL statement [SELECT TEST_TABLE.id AS id, TEST_TABLE."ORDER" AS "ORDER" FROM TEST_TABLE]

Although the error no longer occurs, the mapping of the column value to the property fails, and I always receive 0 as the value for order.

I also attempted to override the getColumnName method of the NamingStrategy, but it did not resolve the problem. Here's the code I used:

@Bean
public NamingStrategy namingStrategy() {
    return new NamingStrategy() {
        @Override
        public String getColumnName(RelationalPersistentProperty property) {
            Assert.notNull(property, "Property must not be null.");
            return "\"" + ParsingUtils.reconcatenateCamelCase(property.getName(), "_") + "\"";
        }
    };
}

It appears that the issue lies with the AS "ORDER" clause, as it treats the double quotes as part of the column name, resulting in a failed mapping to the property.

Any suggestions or insights would be greatly appreciated.

Newbie
  • 1,584
  • 9
  • 33
  • 72
  • What version of Spring Data JDBC are you using? Spring Data JDBC does quoting of column names for a long time so this looks like the behaviour of an old version. Can you upgrade to a current version of Spring Data JDBC? – Jens Schauder Jun 15 '23 at 07:53
  • org.springframework.data:spring-data-jdbc:jar:2.4.6 – Newbie Jun 15 '23 at 10:43
  • That is rather old. You should at least update to 2.4.12 or better yet to 3.1.0. – Jens Schauder Jun 15 '23 at 12:32
  • 1
    Anyway, even that version should have quoting enabled by default. Are you configuring the JdbcMappingContext to not force quoting? – Jens Schauder Jun 15 '23 at 12:35

1 Answers1

0

Did you try using backticks? The solution described in the linked thread?

@Column("`order`")

Alternatively for SQL Server:

@Column("[order]")
Mar-Z
  • 2,660
  • 2
  • 4
  • 16