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?

Hanumanthraju
  • 77
  • 1
  • 2
  • 11
  • 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. – JB Nizet Dec 23 '16 at 07:10
  • Please add the piece of code where you have the issue. – Mario Santini Dec 23 '16 at 07:11
  • And please, don't overwrite the edits I made if it makes the post look worse. – JB Nizet Dec 23 '16 at 07:12
  • I'm using the same **database and storeNumber**, aslo i'm just reading the existing data from DB. – Hanumanthraju Dec 23 '16 at 07:12
  • Try running `commit` command directly and restart your app and check the results. Try printing the query after setting params. – Kiran Kumar Dec 23 '16 at 07:13
  • passing same store number too – Hanumanthraju Dec 23 '16 at 07:14
  • Hi Kiran, 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] – Hanumanthraju Dec 23 '16 at 07:17
  • Can you post the repository code? – richersoon Dec 23 '16 at 07:17
  • final query with bind parameter value – Hanumanthraju Dec 23 '16 at 07:18
  • Query looks good, can you post the repository code. – Kiran Kumar Dec 23 '16 at 07:19
  • `import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.transaction.annotation.Transactional; import com.searsgateway.model.StoreManagers; @Transactional(readOnly = true) public interface StoreManagersRepository extends JpaRepository{ List findByStoreNumber(String storeNumber); }` – Hanumanthraju Dec 23 '16 at 07:19
  • Can you add implementation code of your method for better visibility? – Nimesh Dec 23 '16 at 07:21
  • I have not done implementation as I'm using spring boot as for the spring boot documentation, entity class and repository interface extending the JpaRepository is enough to query the result..By the way i'm able to retrive the result by calling another method findByManagerId(String managerId). But i've only problem with findBystoreNumber method. – Hanumanthraju Dec 23 '16 at 07:33
  • If possible copy your StoreManagers bean – Nimesh Dec 23 '16 at 07:43
  • Added Bean class code – Hanumanthraju Dec 23 '16 at 08:08
  • I have doubt about your code. You have not set "manager_discount_id" for manager Id then why it is printing "storemanag0_.manager_discount_id" and not "manager_id"? I think your code is running something else. – Nimesh Dec 23 '16 at 09:25

1 Answers1

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)
Kihats
  • 3,326
  • 5
  • 31
  • 46
  • using the same..please see the entity class code in the question section – Hanumanthraju Dec 23 '16 at 08:12
  • By default he can use underscore in the database, camel case will be split into underscore. http://stackoverflow.com/a/29088398/3448799 – richersoon Dec 23 '16 at 08:17
  • 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` – Kihats Dec 23 '16 at 10:15