繁体   English   中英

Spring JPA“和”方法且不能为空

[英]Spring JPA “And” Methods and Not Null

在CrudReposity中使用AND遇到了荒唐的麻烦。 我要做的就是查找两件事是否不是Null并显示它们。

    public interface StudentRepository extends CrudRepository<Student, Integer>{

    List<Student> findByItemAIsNotNullAndItemBIsNotNull();
}

当我运行它时,它似乎在将AND作为OR(我都尝试过)运行,因此它显示了其中一个都为空的事物。

任何帮助将不胜感激

您的代码正确可能是其他部分的问题。 否则,您可以看到此代码。 它可能会帮助您。 在这里,我跳过了服务层,尽管仅用于测试。

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;
    }

}

储存库类

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();

}

控制器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();

    }

}

数据库

测试数据库

回应

邮递员回应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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