title: Spring-Data Project is not receiving my Get/Post No entity found for query
body: <p>I finished creating a simple Spring-boot project in which I can enter users and through the Get command it returns me the name (from a list of identical names) with the oldest entry date. Unfortunately, every time I ask for Get it returns this <strong>ERROR</strong>:</p>
  <pre><code>D:\&gt;curl -G localhost:8080/demo/first -d name=Biagio
  {&quot;timestamp&quot;:&quot;2020-10-05T08:52:34.741+00:00&quot;,&quot;status&quot;:500,&quot;error&quot;:&quot;Internal Server Error&quot;,&quot;message&quot;:&quot;&quot;,&quot;path&quot;:&quot;/demo/first&quot;}
  </code></pre>
  <p>In the Spring-Boot terminal I have this <strong>ERROR</strong>:</p>
  <pre><code>Biagio
  2020-10-05 10:52:34.722 ERROR 8860 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.EmptyResultDataAccessException: No entity found for query; nested exception is javax.persistence.NoResultException: No entity found for query] with root cause
  </code></pre>
  <p>Even when I try to POST / add I get a message that it has saved in the DB but hasn't actually saved anything</p>
  <p>Below I add all interested parties:</p>
  <p><strong>AccessingDataMysqlApplication.java</strong></p>
  <pre><code>package com.example.accessingdatamysql;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.domain.EntityScan;
  import org.springframework.context.annotation.ComponentScan;
  
  
  @SpringBootApplication
  public class AccessingDataMysqlApplication {
  
    public static void main(String[] args) {
      SpringApplication.run(AccessingDataMysqlApplication.class, args);
    }
  
  }
  </code></pre>
  <p><strong>MainController.java</strong></p>
  <pre><code>package com.example.accessingdatamysql.rest;
  
  import javax.persistence.NoResultException;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.transaction.annotation.Transactional;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.example.accessingdatamysql.model.UserDto;
  import com.example.accessingdatamysql.service.UserService;
  
  @RestController
  @RequestMapping(&quot;/demo&quot;)
  public class MainController {
  
  @Autowired
  private UserService userService;
  
  @Transactional
  //@RequestMapping(value = &quot;/add/&quot;, method = RequestMethod.POST)
  @PostMapping(path=&quot;/demo/add&quot;)
  public String addNewUser(@PathVariable(&quot;name&quot;) String name, @PathVariable(&quot;email&quot;) String email,
  @PathVariable(&quot;surname&quot;) String surname) {
  
  UserDto n = new UserDto();
  n.setName(name);
  n.setSurname(surname);
  n.setEmail(email);
  userService.create(n);
  return &quot;User Saved in DB&quot;;
  }
  
  @SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
  //@RequestMapping(value = &quot;/fetchUser/{name}&quot;, method = RequestMethod.GET)
  @GetMapping(&quot;/demo/first&quot;)
  public ResponseEntity&lt;UserDto&gt; fetchUser(@PathVariable(&quot;name&quot;) String name) {
  System.out.println(name);
  
  try {
  UserDto namefound = userService.findFirstByName(name);
  System.out.println(&quot;Name found&quot;);
  ResponseEntity&lt;UserDto&gt; user = new ResponseEntity&lt;UserDto&gt;(namefound, HttpStatus.OK);
  return user;
  } catch(NoResultException ne) {
  System.out.println(&quot;User not found&quot;);
  return new ResponseEntity(&quot;User not found with name : &quot; + name, HttpStatus.NOT_FOUND);
  }
  
  }
  }
  </code></pre>
  <p><strong>UserService.java</strong></p>
  <pre><code>package com.example.accessingdatamysql.service;
  
  import org.springframework.stereotype.Service;
  
  import com.example.accessingdatamysql.model.UserDto;
  @Service
  public interface UserService {
  
      UserDto findFirstByName(String name);
      
      void create(UserDto user);
          
  }
  </code></pre>
  <p><strong>UserServiceImpl.java</strong></p>
  <pre><code>package com.example.accessingdatamysql.service;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.stereotype.Service;
  
  import com.example.accessingdatamysql.model.UserDto;
  import com.example.accessingdatamysql.model.UserEntity;
  import com.example.accessingdatamysql.repo.UserRepository;
  import com.example.accessingdatamysql.util.UserMapper;
  
  @Service
  public class UserServiceImpl implements UserService {
      
      @Autowired
      private UserRepository userRepository;
  
      @Autowired
      UserMapper mapper;
  
      @Override
      public UserDto findFirstByName(String name) {
          UserEntity entity = userRepository.findFirstByName(name);
          return mapper.toDtoMapper(entity);
      }
  
      @Override
      public void create(UserDto user) {
          UserEntity entity = mapper.toEntityMapper(user);
          userRepository.create(entity);
      }
  
  }
  </code></pre>
  <p><strong>UserMapper.java</strong></p>
  <pre><code>package com.example.accessingdatamysql.util;
  
  import org.mapstruct.Mapper;
  
  import com.example.accessingdatamysql.model.UserDto;
  import com.example.accessingdatamysql.model.UserEntity;
  
  
  
  @Mapper(componentModel = &quot;spring&quot;)
  public interface UserMapper {   
      
      public UserEntity toEntityMapper (UserDto user);    
      public UserDto toDtoMapper (UserEntity userEntity);
      
  }
  </code></pre>
  <p><strong>UserRepository.java</strong></p>
  <pre><code>package com.example.accessingdatamysql.repo;
  
  
  import org.springframework.stereotype.Repository;
  
  import com.example.accessingdatamysql.model.UserEntity;
  
  
  @Repository
  public interface UserRepository {
      UserEntity findFirstByName(String name);
      void create(UserEntity entity);
      
  }
  
  </code></pre>
  <p><strong>UserRepositoryImpl.java</strong></p>
  <pre><code>package com.example.accessingdatamysql.service;
  
  import javax.persistence.EntityManager;
  import javax.persistence.TypedQuery;
  import javax.persistence.criteria.CriteriaBuilder;
  import javax.persistence.criteria.CriteriaQuery;
  import javax.persistence.criteria.Root;
  
  import org.springframework.stereotype.Component;
  
  import com.example.accessingdatamysql.model.UserEntity;
  import com.example.accessingdatamysql.repo.UserRepository;
  
  @Component
  public class UserRepositoryImpl implements UserRepository {
  
      private final EntityManager em;
  
      public UserRepositoryImpl(EntityManager entityManager) {
          this.em = entityManager;
      }
  
      @Override
      public UserEntity findFirstByName(String name) {
          CriteriaBuilder builder = em.getCriteriaBuilder();
          CriteriaQuery&lt;UserEntity&gt; criteria = builder.createQuery(UserEntity.class);
          Root&lt;UserEntity&gt; root = criteria.from(UserEntity.class);
          criteria.select(root).where(builder.equal(root.get(&quot;name&quot;), name));
          criteria.orderBy(builder.asc(root.get(&quot;timestamp&quot;)));
          TypedQuery&lt;UserEntity&gt; query = em.createQuery(criteria).setMaxResults(1);
          return query.getSingleResult();
  
      }
  
      @Override
  //  per la creazione//
      public void create(UserEntity entity) {
          em.persist(entity);
      }
  
  }
  </code></pre>
  <p><strong>UserDto.java</strong></p>
  <pre><code>package com.example.accessingdatamysql.model;
  
  import java.io.Serializable;
  import java.sql.Timestamp;
  
  public class UserDto implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = -7621330660870602403L;
      /**
       * 
       */
  
      private String name;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Timestamp getTimestamp() {
          return timestamp;
      }
  
      public void setTimestamp(Timestamp timestamp) {
          this.timestamp = timestamp;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getSurname() {
          return surname;
      }
  
      public void setSurname(String surname) {
          this.surname = surname;
      }
  
      private Timestamp timestamp;
      private String email;
      private String surname;
  
  }
  </code></pre>
  <p>If you need also I could provide User.java and the pom file, but the pom has no problems as the dependencies are all correct.</p>