The expected Spring JPA query is not being returned





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm learning how to use Spring JPA and am have configured my database to successfully add elements to my database. I'm just having trouble formulating a query to retrieve my results (a list of Employee, filtered by lastname):



//Employee entity:



@Entity
public class Employee {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
long id;
String firstName;
String lastName;

public Employee() { }

public Employee(long id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (id != other.id)
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}

@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}

}


And my problem is within the query in my repository interface below. When invoking the findByLastName(String lastName) method, I'm always receiving an empty Collection -- even when passing a lastName that I know exists in my database:



public interface EmployeeRepository
extends CrudRepository<Employee, Long> {

@Query("select u from Employee u where u.lastName = ?1")
public Collection<Employee> findByLastName(String lastName);
}


It's also worth mentioning that the column i'm querying is named last_name in my database column (with underscores). I'm not sure if this makes any difference.



Where did I go wrong?










share|improve this question





























    0















    I'm learning how to use Spring JPA and am have configured my database to successfully add elements to my database. I'm just having trouble formulating a query to retrieve my results (a list of Employee, filtered by lastname):



    //Employee entity:



    @Entity
    public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    long id;
    String firstName;
    String lastName;

    public Employee() { }

    public Employee(long id, String firstName, String lastName) {
    super();
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    }

    public long getId() {
    return id;
    }
    public void setId(long id) {
    this.id = id;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
    result = prime * result + (int) (id ^ (id >>> 32));
    result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
    return result;
    }
    @Override
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (getClass() != obj.getClass())
    return false;
    Employee other = (Employee) obj;
    if (firstName == null) {
    if (other.firstName != null)
    return false;
    } else if (!firstName.equals(other.firstName))
    return false;
    if (id != other.id)
    return false;
    if (lastName == null) {
    if (other.lastName != null)
    return false;
    } else if (!lastName.equals(other.lastName))
    return false;
    return true;
    }

    @Override
    public String toString() {
    return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }

    }


    And my problem is within the query in my repository interface below. When invoking the findByLastName(String lastName) method, I'm always receiving an empty Collection -- even when passing a lastName that I know exists in my database:



    public interface EmployeeRepository
    extends CrudRepository<Employee, Long> {

    @Query("select u from Employee u where u.lastName = ?1")
    public Collection<Employee> findByLastName(String lastName);
    }


    It's also worth mentioning that the column i'm querying is named last_name in my database column (with underscores). I'm not sure if this makes any difference.



    Where did I go wrong?










    share|improve this question

























      0












      0








      0








      I'm learning how to use Spring JPA and am have configured my database to successfully add elements to my database. I'm just having trouble formulating a query to retrieve my results (a list of Employee, filtered by lastname):



      //Employee entity:



      @Entity
      public class Employee {

      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      long id;
      String firstName;
      String lastName;

      public Employee() { }

      public Employee(long id, String firstName, String lastName) {
      super();
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
      }

      public long getId() {
      return id;
      }
      public void setId(long id) {
      this.id = id;
      }
      public String getFirstName() {
      return firstName;
      }
      public void setFirstName(String firstName) {
      this.firstName = firstName;
      }
      public String getLastName() {
      return lastName;
      }
      public void setLastName(String lastName) {
      this.lastName = lastName;
      }

      @Override
      public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
      result = prime * result + (int) (id ^ (id >>> 32));
      result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
      return result;
      }
      @Override
      public boolean equals(Object obj) {
      if (this == obj)
      return true;
      if (obj == null)
      return false;
      if (getClass() != obj.getClass())
      return false;
      Employee other = (Employee) obj;
      if (firstName == null) {
      if (other.firstName != null)
      return false;
      } else if (!firstName.equals(other.firstName))
      return false;
      if (id != other.id)
      return false;
      if (lastName == null) {
      if (other.lastName != null)
      return false;
      } else if (!lastName.equals(other.lastName))
      return false;
      return true;
      }

      @Override
      public String toString() {
      return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
      }

      }


      And my problem is within the query in my repository interface below. When invoking the findByLastName(String lastName) method, I'm always receiving an empty Collection -- even when passing a lastName that I know exists in my database:



      public interface EmployeeRepository
      extends CrudRepository<Employee, Long> {

      @Query("select u from Employee u where u.lastName = ?1")
      public Collection<Employee> findByLastName(String lastName);
      }


      It's also worth mentioning that the column i'm querying is named last_name in my database column (with underscores). I'm not sure if this makes any difference.



      Where did I go wrong?










      share|improve this question














      I'm learning how to use Spring JPA and am have configured my database to successfully add elements to my database. I'm just having trouble formulating a query to retrieve my results (a list of Employee, filtered by lastname):



      //Employee entity:



      @Entity
      public class Employee {

      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      long id;
      String firstName;
      String lastName;

      public Employee() { }

      public Employee(long id, String firstName, String lastName) {
      super();
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
      }

      public long getId() {
      return id;
      }
      public void setId(long id) {
      this.id = id;
      }
      public String getFirstName() {
      return firstName;
      }
      public void setFirstName(String firstName) {
      this.firstName = firstName;
      }
      public String getLastName() {
      return lastName;
      }
      public void setLastName(String lastName) {
      this.lastName = lastName;
      }

      @Override
      public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
      result = prime * result + (int) (id ^ (id >>> 32));
      result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
      return result;
      }
      @Override
      public boolean equals(Object obj) {
      if (this == obj)
      return true;
      if (obj == null)
      return false;
      if (getClass() != obj.getClass())
      return false;
      Employee other = (Employee) obj;
      if (firstName == null) {
      if (other.firstName != null)
      return false;
      } else if (!firstName.equals(other.firstName))
      return false;
      if (id != other.id)
      return false;
      if (lastName == null) {
      if (other.lastName != null)
      return false;
      } else if (!lastName.equals(other.lastName))
      return false;
      return true;
      }

      @Override
      public String toString() {
      return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
      }

      }


      And my problem is within the query in my repository interface below. When invoking the findByLastName(String lastName) method, I'm always receiving an empty Collection -- even when passing a lastName that I know exists in my database:



      public interface EmployeeRepository
      extends CrudRepository<Employee, Long> {

      @Query("select u from Employee u where u.lastName = ?1")
      public Collection<Employee> findByLastName(String lastName);
      }


      It's also worth mentioning that the column i'm querying is named last_name in my database column (with underscores). I'm not sure if this makes any difference.



      Where did I go wrong?







      java mysql spring jpa






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 2:16









      Vismark JuarezVismark Juarez

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          -1














          You need add @Column on the field to let jpa know how to create the mappings between database and java class.



          For example:



          @Entity
          @Table(name="target_table_name")
          public class Employee {

          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private long id;

          @Column(name="first_name")
          private String firstName;

          @Column(name="last_name")
          private String lastName;
          //getter and setter and something else
          }


          Without @Table,the entity's name will be recognized by jpa as the table name.If they are not same,you should add @Table on the class and specify the table name.



          "first_name" and "last_name" in the annotation is the actual name of the database column,you should change it if necessary.



          By the way,it's better to set the access of entities' fields as private.






          share|improve this answer


























          • I modified my Employee class accordingly, but am still not getting any values returned when invoking the findByLastName(...) method. Any other thoughts as to why this might be happening?

            – Vismark Juarez
            Nov 23 '18 at 6:31











          • ....Did you add @Table on the class?Make sure you're dealing with the correct table and scheme~

            – AokoQin
            Nov 23 '18 at 6:48













          • I just added it -- still no luck

            – Vismark Juarez
            Nov 23 '18 at 6:59











          • It turned out to be an unrelated problem with the RestController that was passing the query parameter into the findByLastName(...) method. Nonetheless, the changes you suggested were also needed in order to successfully query the DB.

            – Vismark Juarez
            Nov 23 '18 at 7:06






          • 1





            The "@Column" annotations do nothing in our case since the provider will automatically map the camel case Entity fields to snake case database columns.

            – garfield
            Nov 23 '18 at 7:22














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439909%2fthe-expected-spring-jpa-query-is-not-being-returned%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          -1














          You need add @Column on the field to let jpa know how to create the mappings between database and java class.



          For example:



          @Entity
          @Table(name="target_table_name")
          public class Employee {

          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private long id;

          @Column(name="first_name")
          private String firstName;

          @Column(name="last_name")
          private String lastName;
          //getter and setter and something else
          }


          Without @Table,the entity's name will be recognized by jpa as the table name.If they are not same,you should add @Table on the class and specify the table name.



          "first_name" and "last_name" in the annotation is the actual name of the database column,you should change it if necessary.



          By the way,it's better to set the access of entities' fields as private.






          share|improve this answer


























          • I modified my Employee class accordingly, but am still not getting any values returned when invoking the findByLastName(...) method. Any other thoughts as to why this might be happening?

            – Vismark Juarez
            Nov 23 '18 at 6:31











          • ....Did you add @Table on the class?Make sure you're dealing with the correct table and scheme~

            – AokoQin
            Nov 23 '18 at 6:48













          • I just added it -- still no luck

            – Vismark Juarez
            Nov 23 '18 at 6:59











          • It turned out to be an unrelated problem with the RestController that was passing the query parameter into the findByLastName(...) method. Nonetheless, the changes you suggested were also needed in order to successfully query the DB.

            – Vismark Juarez
            Nov 23 '18 at 7:06






          • 1





            The "@Column" annotations do nothing in our case since the provider will automatically map the camel case Entity fields to snake case database columns.

            – garfield
            Nov 23 '18 at 7:22


















          -1














          You need add @Column on the field to let jpa know how to create the mappings between database and java class.



          For example:



          @Entity
          @Table(name="target_table_name")
          public class Employee {

          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private long id;

          @Column(name="first_name")
          private String firstName;

          @Column(name="last_name")
          private String lastName;
          //getter and setter and something else
          }


          Without @Table,the entity's name will be recognized by jpa as the table name.If they are not same,you should add @Table on the class and specify the table name.



          "first_name" and "last_name" in the annotation is the actual name of the database column,you should change it if necessary.



          By the way,it's better to set the access of entities' fields as private.






          share|improve this answer


























          • I modified my Employee class accordingly, but am still not getting any values returned when invoking the findByLastName(...) method. Any other thoughts as to why this might be happening?

            – Vismark Juarez
            Nov 23 '18 at 6:31











          • ....Did you add @Table on the class?Make sure you're dealing with the correct table and scheme~

            – AokoQin
            Nov 23 '18 at 6:48













          • I just added it -- still no luck

            – Vismark Juarez
            Nov 23 '18 at 6:59











          • It turned out to be an unrelated problem with the RestController that was passing the query parameter into the findByLastName(...) method. Nonetheless, the changes you suggested were also needed in order to successfully query the DB.

            – Vismark Juarez
            Nov 23 '18 at 7:06






          • 1





            The "@Column" annotations do nothing in our case since the provider will automatically map the camel case Entity fields to snake case database columns.

            – garfield
            Nov 23 '18 at 7:22
















          -1












          -1








          -1







          You need add @Column on the field to let jpa know how to create the mappings between database and java class.



          For example:



          @Entity
          @Table(name="target_table_name")
          public class Employee {

          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private long id;

          @Column(name="first_name")
          private String firstName;

          @Column(name="last_name")
          private String lastName;
          //getter and setter and something else
          }


          Without @Table,the entity's name will be recognized by jpa as the table name.If they are not same,you should add @Table on the class and specify the table name.



          "first_name" and "last_name" in the annotation is the actual name of the database column,you should change it if necessary.



          By the way,it's better to set the access of entities' fields as private.






          share|improve this answer















          You need add @Column on the field to let jpa know how to create the mappings between database and java class.



          For example:



          @Entity
          @Table(name="target_table_name")
          public class Employee {

          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private long id;

          @Column(name="first_name")
          private String firstName;

          @Column(name="last_name")
          private String lastName;
          //getter and setter and something else
          }


          Without @Table,the entity's name will be recognized by jpa as the table name.If they are not same,you should add @Table on the class and specify the table name.



          "first_name" and "last_name" in the annotation is the actual name of the database column,you should change it if necessary.



          By the way,it's better to set the access of entities' fields as private.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 7:01

























          answered Nov 23 '18 at 2:56









          AokoQinAokoQin

          894




          894













          • I modified my Employee class accordingly, but am still not getting any values returned when invoking the findByLastName(...) method. Any other thoughts as to why this might be happening?

            – Vismark Juarez
            Nov 23 '18 at 6:31











          • ....Did you add @Table on the class?Make sure you're dealing with the correct table and scheme~

            – AokoQin
            Nov 23 '18 at 6:48













          • I just added it -- still no luck

            – Vismark Juarez
            Nov 23 '18 at 6:59











          • It turned out to be an unrelated problem with the RestController that was passing the query parameter into the findByLastName(...) method. Nonetheless, the changes you suggested were also needed in order to successfully query the DB.

            – Vismark Juarez
            Nov 23 '18 at 7:06






          • 1





            The "@Column" annotations do nothing in our case since the provider will automatically map the camel case Entity fields to snake case database columns.

            – garfield
            Nov 23 '18 at 7:22





















          • I modified my Employee class accordingly, but am still not getting any values returned when invoking the findByLastName(...) method. Any other thoughts as to why this might be happening?

            – Vismark Juarez
            Nov 23 '18 at 6:31











          • ....Did you add @Table on the class?Make sure you're dealing with the correct table and scheme~

            – AokoQin
            Nov 23 '18 at 6:48













          • I just added it -- still no luck

            – Vismark Juarez
            Nov 23 '18 at 6:59











          • It turned out to be an unrelated problem with the RestController that was passing the query parameter into the findByLastName(...) method. Nonetheless, the changes you suggested were also needed in order to successfully query the DB.

            – Vismark Juarez
            Nov 23 '18 at 7:06






          • 1





            The "@Column" annotations do nothing in our case since the provider will automatically map the camel case Entity fields to snake case database columns.

            – garfield
            Nov 23 '18 at 7:22



















          I modified my Employee class accordingly, but am still not getting any values returned when invoking the findByLastName(...) method. Any other thoughts as to why this might be happening?

          – Vismark Juarez
          Nov 23 '18 at 6:31





          I modified my Employee class accordingly, but am still not getting any values returned when invoking the findByLastName(...) method. Any other thoughts as to why this might be happening?

          – Vismark Juarez
          Nov 23 '18 at 6:31













          ....Did you add @Table on the class?Make sure you're dealing with the correct table and scheme~

          – AokoQin
          Nov 23 '18 at 6:48







          ....Did you add @Table on the class?Make sure you're dealing with the correct table and scheme~

          – AokoQin
          Nov 23 '18 at 6:48















          I just added it -- still no luck

          – Vismark Juarez
          Nov 23 '18 at 6:59





          I just added it -- still no luck

          – Vismark Juarez
          Nov 23 '18 at 6:59













          It turned out to be an unrelated problem with the RestController that was passing the query parameter into the findByLastName(...) method. Nonetheless, the changes you suggested were also needed in order to successfully query the DB.

          – Vismark Juarez
          Nov 23 '18 at 7:06





          It turned out to be an unrelated problem with the RestController that was passing the query parameter into the findByLastName(...) method. Nonetheless, the changes you suggested were also needed in order to successfully query the DB.

          – Vismark Juarez
          Nov 23 '18 at 7:06




          1




          1





          The "@Column" annotations do nothing in our case since the provider will automatically map the camel case Entity fields to snake case database columns.

          – garfield
          Nov 23 '18 at 7:22







          The "@Column" annotations do nothing in our case since the provider will automatically map the camel case Entity fields to snake case database columns.

          – garfield
          Nov 23 '18 at 7:22






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439909%2fthe-expected-spring-jpa-query-is-not-being-returned%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?