Skip to content
Advertisement

JPA createQuery where condition does not work

I am trying to use JPA to fetch records from database. However I am able to insert records indatabse and even get all the records using createQuery method of class EntityManager.

But in below case I am not getting why the condition in where clause is not working.

Please help me figure it out.

POJO class :

@Entity
@Table(name = "frameworks_filter")
public class FilteredFrameworksDbStructure {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    @Column(name = "regular_name")
    private String regularName;
    
    @Column(name = "component_name")
    private String componentName;
    
    @Column(name = "component_owner")
    private String componentOwner;
    
    @Column(name = "frameworks")
    private String frameworks;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getRegularName() {
        return regularName;
    }
    public void setRegularName(String regularName) {
        this.regularName = regularName;
    }
    public String getComponentName() {
        return componentName;
    }
    public void setComponentName(String componentName) {
        this.componentName = componentName;
    }
    public String getComponentOwner() {
        return componentOwner;
    }
    public void setComponentOwner(String componentOwner) {
        this.componentOwner = componentOwner;
    }
    public String getFrameworks() {
        return frameworks;
    }
    public void setFrameworks(String frameworks) {
        this.frameworks = frameworks;
    }
}

DAO class method:

public List<FilteredFrameworksDbStructure> getFilteredFrameworks(String regularName) {

        EntityManager entityManager = entityManagerFactory.createEntityManager();
        
        List<FilteredFrameworksDbStructure> filteredFrameworksDbStructureList = entityManager
                .createQuery("from FilteredFrameworksDbStructure F where F.regularName = :regular", FilteredFrameworksDbStructure.class)
                .setParameter("regular", regularName)
                .getResultList();
        

        return filteredFrameworksDbStructureList;
    }

Issue : Condition in where clause does not work. It simply fetch all the records irrespective of the regularName provided.

Regards, Parag Vinchurkar

Advertisement

Answer

Why don’t you use the JpaRepository or CrudRepository to fetch your results? Check out this tutorial here and here on how to use them.

And you can use your where clause. Please see below the example repository you can use to obtain the same results as the entityManager

public interface FilteredFrameworksDbStructureRepo extends JpaRepository<FilteredFrameworksDbStructure , Integer>{

    List<FilteredFrameworksDbStructure> findAllByRegularName(String regularName)
}

Please note that you will have to change your id member variable from int to Integer

7 People found this is helpful
Advertisement