2016-10-04 6 views
0

Ich möchte eine Abfrage, die 2 Spalten innerhalb meiner Tabelle vergleichen und daher keine Argumente zu der Abfrage hinzufügen müssen. Zum Beispiel: Ich habe eine Tabelle Artikel, und ich möchte alle Produkte finden, die einen Bestand hat < = stock_minSpring Data JPA - Abfrage ohne Argumente

+----+-----------+-------+-----------+ 
| ID | NAME | STOCK | STOCK_MIN | 
+----+-----------+-------+-----------+ 
| 1 | product 1 |  2 |   3 | 
| 2 | product 2 |  3 |   1 | 
| 3 | product 3 |  4 |   5 | 
+----+-----------+-------+-----------+ 

Wie kann ich das mit Spring Data JPA?

wie dies in meinem ProductRepository Deklarieren etwas wirft einen Fehler

List<Product> findByStockIsLessThanEqualStockMin(); 

dies auch nicht

@Query("select p from product p where p.stock <= p.stockmin") 
List<Product> findByStockIsLessThanEqualStockMin(); 

die Beispiele Alle in der Spring Data JPA documentation einige Argumente in der Abfrage verwenden funktioniert, aber ich don Ich möchte das nicht tun, weil das Attribut stockMin für jede Zeile in meiner Produkttabelle unterschiedlich ist.

[FIXED]
Das Problem war super dumm. Ich vergesse, meine Entität zu kapitalisieren und die select war nicht notwendig. Unter Frage funktioniert gut:

@Query("from Product p where p.stock <= p.stockMin") 
List<Product> findByStockIsLessThanEqualStockMin(); 

pd: das war meine erste Frage hier. Danke euch allen! :)

+0

Nicht sicher, ob ich Ihnen helfen kann, also haben Sie versucht, FindAllBy ...? –

+0

können Sie versuchen, so: findByStockLessThanEqual (stockmin); Ref: http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation – Shaheer

+0

Verwenden Sie eine native Abfrage http://docs.spring.io /spring-data/jpa/docs/current/reference/html/#jpa.query-methods.named-queries –

Antwort

0

Unten erwähnten Code funktioniert gut. Möglicherweise liegt ein Problem mit der in Ihrer Entitätsklasse vor.

package com.demo.account.model; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

import com.fasterxml.jackson.annotation.JsonInclude; 

@Entity 
@JsonInclude(JsonInclude.Include.NON_NULL) 
public class Product { 

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

    private String name; 

    private Integer stock; 

    private Integer stockMin; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Integer getStock() { 
     return stock; 
    } 

    public void setStock(Integer stock) { 
     this.stock = stock; 
    } 

    public Integer getStockMin() { 
     return stockMin; 
    } 

    public void setStockMin(Integer stockMin) { 
     this.stockMin = stockMin; 
    } 

    public Product() { 

    } 

    public Product(String name, Integer stock, Integer stockMin) { 
     super(); 
     this.name = name; 
     this.stock = stock; 
     this.stockMin = stockMin; 
    } 

} 


package com.demo.account.dao; 

import java.util.List; 

import org.springframework.data.jpa.repository.Query; 
import org.springframework.data.repository.CrudRepository; 
import org.springframework.stereotype.Repository; 

import com.demo.account.model.Product; 

@Repository 
public interface ProductDao extends CrudRepository<Product, Long> { 

    @Query("from Product p where p.stock <= p.stockMin") 
    List<Product> findByStockIsLessThanEqualStockMin(); 

} 

Product product = new Product("Mobile",10,3); 
      productDao.save(product); 

      product = new Product("Curtains",15,15); 
      productDao.save(product); 

      product = new Product("Clothing",5,15); 
      productDao.save(product); 

      System.out.println(productDao.findByStockIsLessThanEqualStockMin()); 
+0

Die Entity-Klasse ist in Ordnung, aber Ihre Antwort geben das Innere !. Mein Problem ist, dass ich "Produkt" anstelle von "Produkt" in meinem @Query verwende. Vielen Dank! – plucero

Verwandte Themen