在实体中将DB的空值从0获取

时间:2018-04-10 11:11:10

标签: spring-data-jpa

我有一张这样的表:(文件)

+-------------+---------------+---------------+---------+
| document_id | document_name | document_type | deleted |
+-------------+---------------+---------------+---------+
|           1 | doc1          |             3 | null    |
|           2 | doc2          |             3 | 0       |
|           3 | doc3          |             2 | 1       |
+-------------+---------------+---------------+---------+

同一个表的实体类:

/* Entity */
@Table(name="CDS_DOCUMENT_METADATA")
public class StudentDocument implements Serializable {

    @Id
    @Column(name = "document_id")
    private int documentId;

    @Column(name = "document_name")
    private String name;

    @Column(name = "document_type")
    private int docType;

    @JsonIgnore
    @Column(name = "DELETED")
    private Integer deleted;

    /* getters for all */
}

JPA查询findAllByDeletedNot(1)仅返回第二行。但我也希望得到第一行。

经过一些Stackoverflow Q& A,

1.我尝试将int和Integer数据字段都删除

2.实现默认值

@Column(name = "DELETED", columnDefinition = "SMALLINT(2) default 0")
private Integer deleted;

@Column(name = "DELETED")
private Integer deleted = 0;

3.将属性转换器实现为

public class IntegerConvertorTest implements AttributeConverter<Integer,Integer> {

    @Override
    public Integer convertToDatabaseColumn(Integer attribute) {
        return null;
    }

    @Override
    public Integer convertToEntityAttribute(Integer dbData) {
        return dbData == null ? 0 : dbData;
    }
}

/* Entity */
@Column(name = "DELETED")
@Convert(converter = IntegerConvertorTest.class)
private Integer deleted;

这些方法都没有帮助我获取匹配的数据document_type is null&amp; 0

0 个答案:

没有答案