1

The dependency is

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>ruime</scope>
      <version>8.0.21</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <version>2.2.2.RELEASE</version>
    </dependency>

The Pet Entity is

package com.taimi.jpa.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Version;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import lombok.Data;

@Entity
@Data
@DynamicUpdate
@DynamicInsert
public class Pet {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private int age;
    @ManyToOne(cascade =  {CascadeType.MERGE})
    @JoinColumn(name = "owner")
    private Person owner;

    @Version
    private int version;
}

The Pet Specification is

package com.taimi.jpa.dal.specification;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;

import com.taimi.jpa.entity.Pet;

import org.springframework.data.jpa.domain.Specification;

public class PetSpecification {
    
    public static Specification<Pet> orderNameDesc() {
        return (root, query, cb) -> {
            String value = "'name' USING GBK";
            Expression<String> convertRealnameFunction = cb.function("convert", String.class, cb.literal(value));
            Order nameDesc = cb.desc(convertRealnameFunction);
            Predicate restriction = query.orderBy(nameDesc).getRestriction();
            return restriction;
        };
    }
}

The test is

package com.taimi.jpa.dal;

import java.util.List;

import com.taimi.jpa.dal.specification.PetSpecification;
import com.taimi.jpa.entity.Pet;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class PetTest {

    @Autowired
    private PetDal petDal;

    @Test
    // @Rollback
    public void testOrderName() {
        List<Pet> findAll = petDal.findAll(PetSpecification.orderNameDesc());
        findAll.forEach(System.out::println);
    }
}

The error is

SQL Error: 1064, SQLState: 42000
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') desc' at line 1

The reason is the incorrect sql statement:select * ... from ... order by convert('name USING GBK') desc(Pay attention to single quotation marks).The correct convert function is "convert('name' USING GBK)" or "convert(name USING GBK)".

SO how to fix this.

bonaluo
  • 11
  • 1

0 Answers0