简体   繁体   中英

Spring JPA “And” Methods and Not Null

Been having ridiculous trouble with using the AND in the CrudReposity. All I want to do it find if two things are Not Null and display them.

    public interface StudentRepository extends CrudRepository<Student, Integer>{

    List<Student> findByItemAIsNotNullAndItemBIsNotNull();
}

When I run this, it seems to be running the AND as an OR (I tried both), so it's showing things that all null in one of them.

Any Help will be appreciated

You Code is correct may be problem in other section. Otherwise you can see this code. It may help you. Here I skipped service layer though its for only test.

package com.example.stack;

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

@Entity
public class Data{
    @Id
    @GeneratedValue
    Integer id;
    String itemA;
    String itemB;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getItemA() {
        return itemA;
    }
    public void setItemA(String itemA) {
        this.itemA = itemA;
    }
    public String getItemB() {
        return itemB;
    }
    public void setItemB(String itemB) {
        this.itemB = itemB;
    }

}

Repository Class

package com.example.stack;

import java.util.List;

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


public interface TestRepository extends CrudRepository<Data, Integer>{

  List<Data> findByItemAIsNotNullAndItemBIsNotNull();

}

Controller CLass

package com.example.stack;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    TestRepository repo;

    @GetMapping
    public List<Data> getTest()
    {
        return (List<Data>) repo.findByItemAIsNotNullAndItemBIsNotNull();

    }

}

Database :

测试数据库

Response :

邮递员回应

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM