【问题标题】:Spring JPA "And" Methods and Not NullSpring JPA“和”方法并且不为空
【发布时间】:2019-04-21 10:36:07
【问题描述】:

在 CrudReposity 中使用 AND 时遇到了可笑的麻烦。我想做的就是找出两件事是否为 Not Null 并显示它们。

    public interface StudentRepository extends CrudRepository<Student, Integer>{

    List<Student> findByItemAIsNotNullAndItemBIsNotNull();
}

当我运行它时,它似乎将 AND 作为 OR 运行(我都尝试过),所以它显示的东西在其中一个中都为空。

任何帮助将不胜感激

【问题讨论】:

  • 你能看到正在执行的查询是什么吗?
  • 你能显示Student类的完整代码吗?

标签: java spring-mvc spring-data-jpa jpql


【解决方案1】:

您的代码正确可能是其他部分的问题。否则,您可以看到此代码。它可能会帮助你。这里我跳过了服务层,虽然它只是为了测试。

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

}

控制器类

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

    }

}

数据库

回应

【讨论】:

猜你喜欢
  • 2017-04-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 2018-11-11
相关资源
最近更新 更多