C
C
Chvalov2018-01-20 03:46:41
Java
Chvalov, 2018-01-20 03:46:41

JpaRepository returns null while the SQL query is correct, where to look?

Root controller in which to debage

import com.chvalov.library.Repository.AuthorRepository;
import com.chvalov.library.Repository.BookRepository;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@Log
public class RedirectController {

    @Autowired
    private AuthorRepository authorRepository;

    @Autowired
    private BookRepository bookRepository;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String baseUrlRedirect(HttpServletRequest request, HttpServletResponse httpServletResponse) {

        // Пустота
        System.out.println(bookRepository.findByNameContainingIgnoreCaseOrAuthorFioContainingIgnoreCaseOrderByName("а","па"));
        System.out.println(authorRepository.findByFioContainingIgnoreCaseOrderByFio("ча"));

        // Работает
        System.out.println(authorRepository.findOne(25L));
        System.out.println(authorRepository.findAll());

        System.out.println(bookRepository.findOne(20L));
        System.out.println(bookRepository.findAll());
        return "ok";
    }
}
DAO:
Spoiler (Cleene)
AuthDao :
import com.chvalov.library.Entity.Author;

// описывает специфичное поведение для работы с авторами
public interface AuthorDao extends GeneralDAO<Author>{

}
GeneralDao:
import java.util.List;

// общее поведения для всех DAO объектов
public interface GeneralDAO<T>{

    List<T> getAll();
    List<T> search(String... searchString);

    T get(long id); // получение объекта по id
    T save(T obj);// save - обновляет или добавляет объект (один метод на 2 действия)
    void delete(T object);

}


The problem is that when accessing the repository:
import com.chvalov.library.Entity.Author;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository // специальный Spring bean, который помечает интерфейс как Repository
public interface AuthorRepository extends JpaRepository<Author, Long> {// JpaRepository - содержит CRUD функционал + постраничность

    // на основании имени метода будет построен Hibernate запрос
    List<Author> findByFioContainingIgnoreCaseOrderByFio(String fio);
}

Returns empty:
bookRepository.findByNameContainingIgnoreCaseOrAuthorFioContainingIgnoreCaseOrderByName("а","па"));
authorRepository.findByFioContainingIgnoreCaseOrderByFio("ча");
AIqpLuf.png
The request from the console is executed perfectly in phpMyAdmin & Navicat
I0nfgjy.png
When accessing the repository directly, everything works fine:
System.out.println(authorRepository.findOne(25L));
System.out.println(authorRepository.findAll());
kGC4eO1.png
What could be the problem ???
maven.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.chvalov.library</groupId>
  <artifactId>springlibrary</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springlibrary</name>
  <description></description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version> <!-- TODO: 1.5.9.RELEASE -->
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
UPD:
Corrected the log level, here are the results of the Log execution:
System.out.println(authorRepository.findOne(25L));
2018-01-20 05:12:29.544 DEBUG 51619 --- [nio-8080-exec-1] org.hibernate.SQL                        : 
    select
        author0_.id as id1_0_0_,
        author0_.birthday as birthday2_0_0_,
        author0_.fio as fio3_0_0_ 
    from
        library.author author0_ 
    where
        author0_.id=?
Hibernate: 
    select
        author0_.id as id1_0_0_,
        author0_.birthday as birthday2_0_0_,
        author0_.fio as fio3_0_0_ 
    from
        library.author author0_ 
    where
        author0_.id=?
2018-01-20 05:12:29.544 TRACE 51619 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [25]
2018-01-20 05:12:29.555 TRACE 51619 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([birthday2_0_0_] : [TIMESTAMP]) - [1895-04-04 00:00:00.0]
2018-01-20 05:12:29.556 TRACE 51619 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([fio3_0_0_] : [VARCHAR]) - [Николай Лесков]
2018-01-20 05:12:29.562 TRACE 51619 --- [nio-8080-exec-1] org.hibernate.type.CollectionType        : Created collection wrapper: [com.chvalov.library.Entity.Author.books#25]
Николай Лесков

And here is the method I need (Which returns emptiness):
System.out.println(authorRepository.findByFioContainingIgnoreCaseOrderByFio("ча"));
There is something wrong with the select:
2018-01-20 05:12:29.383  INFO 51619 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
2018-01-20 05:12:29.434  INFO 51619 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2018-01-20 05:12:29.511 DEBUG 51619 --- [nio-8080-exec-1] org.hibernate.SQL                        : 
    /* select
        generatedAlias0 
    from
        Author as generatedAlias0 
    where
        upper(generatedAlias0.fio) like upper(:param0) 
    order by
        generatedAlias0.fio asc */ select
            author0_.id as id1_0_,
            author0_.birthday as birthday2_0_,
            author0_.fio as fio3_0_ 
        from
            library.author author0_ 
        where
            upper(author0_.fio) like upper(?) 
        order by
            author0_.fio asc
Hibernate: 
    /* select
        generatedAlias0 
    from
        Author as generatedAlias0 
    where
        upper(generatedAlias0.fio) like upper(:param0) 
    order by
        generatedAlias0.fio asc */ select
            author0_.id as id1_0_,
            author0_.birthday as birthday2_0_,
            author0_.fio as fio3_0_ 
        from
            library.author author0_ 
        where
            upper(author0_.fio) like upper(?) 
        order by
            author0_.fio asc
2018-01-20 05:12:29.521 TRACE 51619 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [%ча%]
[]

For several hours now I can not understand what the joke is :(
UPD 2: (MySQL log)
See comment on this question (Limit 10k characters)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chvalov, 2018-01-20
@Chvalov

&useUnicode=yes&characterEncoding=UTF-8
Registered in application.properties

### Database for MySQL ###
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/library?useSSL=false&useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username=root

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question