我正在使用SPring提供的JPA依赖项来列出User表中的所有项目。一些用户在数据库中有电话号码,我的错误率低于此值。有没有办法忽略DB的空值?
java.lang.IllegalArgumentException: Can not set long field com.freelancer.Entity.User.phone to null value
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_161]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_161]
at sun.reflect.UnsafeLongFieldAccessorImpl.set(UnsafeLongFieldAccessorImpl.java:80) ~[na:1.8.0_161]
at java.lang.reflect.Field.set(Field.java:764) ~[na:1.8.0_161]
at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:41) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
实体类如下:
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt"},
allowGetters = true)
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotBlank
private String name;
@NotBlank
private String email;
@NotBlank
private String password;
private long phone;
private String about;
private int profile_id;
private String skills;
答案 0 :(得分:1)
long
等原始类型不能包含空值。建议您将phone
类型更改为Long
。此外,当您访问时,您可以进行空检查。