【问题标题】:JpaRepository findAll() will not return rows with null fieldsJpaRepository findAll() 不会返回带有空字段的行
【发布时间】:2023-03-08 03:21:01
【问题描述】:

我有一个这样的 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 列表中的所有五条记录?

【问题讨论】:

  • 你的 id 类型是 string 并且你的存储库是 JpaRepository&lt;DocumentTagsView, Long&gt;Long 更改为 String 并再试一次
  • 好收获!改变了它;没有骰子。
  • 你能证明findAll()返回null-elements的代码吗?
  • 你为什么使用@IdClass(DocumentTagsView.class)
  • @hosseinrasekhi,就是这样!来自不同班级的剩余复制粘贴。

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


【解决方案1】:

你正在使用 @IdClass 注释,用于复合 PK 和 jpa 忽略空值。

【讨论】:

    【解决方案2】:

    此外,您可能必须为您的实体创建 getter 和 setter。不确定 Hibernate 或您使用的任何 ORM 是否知道如何填充实体。

    【讨论】:

    • 谢谢!我有 getter 和 setter;不想包括,因为它们不是“特殊的”。
    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 2020-04-16
    • 2021-05-20
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多