java - JpaRepository findAll() 将不会返回具有空字段的行

标签 java mysql spring-data-jpa entity repository-pattern

我有一个像这样的 JPA 存储库:

@Repository
public interface DocumentTagsViewRepository extends JpaRepository<DocumentTagsView, Long>, JpaSpecificationExecutor<DocumentTagsView> {

像这样的实体:

@Entity
@IdClass(DocumentTagsView.class)
@Table(name="document_tags_view", schema="pdf_processing")
public class DocumentTagsView implements Serializable {

    private static final long serialVersionUID = -8222170971385430496L;

    @Id
    @Column
    String vid;

    @Column(nullable=true)
    Long did;

    @Column
    Long tid;

    @Column
    String text;

public DocumentTagsView () {}

public String getVid() {
    return vid;
}

public void setVid(String vid) {
    this.vid = vid;
}

public Long getDid() {
    return did;
}

public void setDid(Long did) {
    this.did = did;
}

public Long getTid() {
    return tid;
}

public void setTid(Long tid) {
    this.tid = tid;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

@Override
public String toString() {
    return "DocumentTagsView [vid=" + vid + ", did=" + did + ", tid=" + tid + ", text=" + text + "]";
}

我的表有 5 条记录,其中两条在 did 列中具有空值:

vid                                     did     tid text
a31a1aa8-f29a-11e9-ba9d-d8cb8abfa8f6    2       6   A
a31a1a79-f29a-11e9-ba9d-d8cb8abfa8f6    2       4   B
a31a1ad2-f29a-11e9-ba9d-d8cb8abfa8f6    2       7   C
a31a19e8-f29a-11e9-ba9d-d8cb8abfa8f6    (null)  1   D
a31a1a57-f29a-11e9-ba9d-d8cb8abfa8f6    (null)  3   E

当我执行 DocumentTagsViewRepository.findAll() 时,我得到一个包含 5 个位置的列表,但最后两个为空。我希望在这些位置获得两个 DocumentTagsView 对象,但 did 值为 null。

如何获取 findAll 列表中的所有五个记录?

最佳答案

您正在使用用于复合 PK 的 @IdClass 注释,而 jpa 会忽略 null 值。

关于java - JpaRepository findAll() 将不会返回具有空字段的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466704/

相关文章:

java - 在不同 map 上使用按键

java - 使用 Ehcache 进行网页缓存

php - 简单的 Ajax Jquery 脚本 - 如何获取表中每一行的信息?

java - 在 Spring Data JPA 存储库中使用 EntityGraph 进行过滤

java - 读取 XML 元素的内部 XML

java - 在Netty NIO中动态添加线程到ExecutorService

MySQL连接包含多个 View 值的多个选择语句

php - 准备好的语句中的递归

java - Spring数据分页和排序存储库,具有多个字段和日期

java - JPA 持久化上下文在线程之间是否隔离?