3

I have table with 2 columns store_number and manager_id and I created an entity and a repository class for the this table.

I'm trying to query manager_id by store_number using repository method below

Bean class:

@Entity public class StoreManagers { @Id @GeneratedValue @Column(name="store_number") private String storeNumber; private String managerId; public StoreManagers() { } public String getStoreNumber() { return storeNumber; } public void setStoreNumber(String storeNumber) { this.storeNumber = storeNumber; } public String getManagerId() { return managerId; } public void setManagerId(String managerId) { this.managerId = managerId; } @Override public String toString() { return "StoreManagers [storeNumber=" + storeNumber + ", manageId=" + managerId + "]"; } } public interface StoreManagersRepository extends JpaRepository<StoreManagers, String> { List<StoreManagers> findByStoreNumber(String storeNumber); } 

Here is the hibernate query and result

Hibernate: select storemanag0_.manager_discount_id as manager_1_2_, storemanag0_.store_number as store_nu2_2_ from storemanagers storemanag0_ where storemanag0_.store_number=? 2016-12-23 12:16:15.763 TRACE 644 --- [nio-4000-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [00012] : [] 

It's returning an empty List, but when I execute above query directly I'm getting results.

Please help me with this. Wat am I doing wrong here?

16
  • 1
    Either you're not passing the same storeNumber, or you and the app are not using the same database, or the data you're seeing has been inserted but not committed yet. Commented Dec 23, 2016 at 7:10
  • Please add the piece of code where you have the issue. Commented Dec 23, 2016 at 7:11
  • And please, don't overwrite the edits I made if it makes the post look worse. Commented Dec 23, 2016 at 7:12
  • I'm using the same database and storeNumber, aslo i'm just reading the existing data from DB. Commented Dec 23, 2016 at 7:12
  • Try running commit command directly and restart your app and check the results. Try printing the query after setting params. Commented Dec 23, 2016 at 7:13

1 Answer 1

5

If you wish to keep the underscored field names in your database annotate your entity fields using @Column(name="store_number") and rename you field to be in camel-case.

@Column(name="store_number") private int storeNumber; 

In your repository, you can use the method

findByStoreNumber(int storeNumber) 
Sign up to request clarification or add additional context in comments.

3 Comments

using the same..please see the entity class code in the question section
By default he can use underscore in the database, camel case will be split into underscore. stackoverflow.com/a/29088398/3448799
Try changing your storeNumber to an int or long or if your storeNumber is alphanumeric, provide another field that will be the id and annotated by @GeneratedValue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.