title: how to use mysql function "convert" in spring-data-jpa
body: <p>The dependency is</p>
  <pre><code>    &lt;dependency&gt;
        &lt;groupId&gt;mysql&lt;/groupId&gt;
        &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
        &lt;scope&gt;ruime&lt;/scope&gt;
        &lt;version&gt;8.0.21&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
        &lt;version&gt;2.2.2.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  <p>The Pet Entity is</p>
  <pre><code>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 = &quot;owner&quot;)
      private Person owner;
  
      @Version
      private int version;
  }
  </code></pre>
  <p>The Pet Specification is</p>
  <pre><code>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&lt;Pet&gt; orderNameDesc() {
          return (root, query, cb) -&gt; {
              String value = &quot;'name' USING GBK&quot;;
              Expression&lt;String&gt; convertRealnameFunction = cb.function(&quot;convert&quot;, String.class, cb.literal(value));
              Order nameDesc = cb.desc(convertRealnameFunction);
              Predicate restriction = query.orderBy(nameDesc).getRestriction();
              return restriction;
          };
      }
  }
  </code></pre>
  <p>The test is</p>
  <pre><code>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&lt;Pet&gt; findAll = petDal.findAll(PetSpecification.orderNameDesc());
          findAll.forEach(System.out::println);
      }
  }
  </code></pre>
  <p>The error is</p>
  <pre><code>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
  </code></pre>
  <p>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 &quot;convert('name' USING GBK)&quot; or &quot;convert(name USING GBK)&quot;.</p>
  <p>SO how to fix this.</p>