JPA(Spring Data JPA 在使用SpringBoot2.*以上的版本时接口在继承JpaRepository是使用findAll()报错问题)

**一.**nested exception is org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property [class com.json.domain.User.age] of primitive type setter of com.json.domain.User.age; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property [class com.json.domain.User.age] of primitive type setter of com.json.domain.User.age] with root cause

java.lang.IllegalArgumentException: Can not set int field com.json.domain.User.age to null value
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_181]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_181]
at sun.reflect.UnsafeIntegerFieldAccessorImpl.set(UnsafeIntegerFieldAccessorImpl.java:80) ~[na:1.8.0_181]
at java.lang.reflect.Field.set(Field.java:764) ~[na:1.8.0_181]
at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:41) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:649) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:205) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4905) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:190) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1151) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.processResultSet(Loader.java:1010) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:948) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2689) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2672) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.Loader.list(Loader.java:2501) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
原因:是因为你的`package com.json.domain;

import lombok.Data;

import javax.persistence.*;

@Data
@Entity//告诉JPA这是一个实体类,和数据表映射的类
@Table(name=“user1”)//用来指定和那张数据表对应,如果省略默认表名就是类名首字母小写(user)
public class User {
// @Id//这是一个主键
// @GeneratedValue(strategy = GenerationType.IDENTITY)//这是一个自增主键

private String userid;
//可以用属性name来设置数据表的列名,也可以设置字符的长度,默认的列名就是字段名;
//@Column(name = "name",length = 16)

private String name;

private String password;

private int age;

private String sex;
@Id
private Integer orderid;

}`
在字段age上使用了int数据类型,将其改为 Integer就行了,那是因为 int 类型不讲赋值为 null 所以会报错 改了之后就能正常运行了。。。

这是我的`package com.json.repository;

import com.json.domain.User;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository {
@Query(value = “select * from user1”,nativeQuery = true)
List listAll();

}`

这是我的`package com.json.Controller;

import com.json.domain.User;
import com.json.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/jpa")
public class UserController {
@Autowired
UserRepository service;
@RequestMapping("/test1")
public Optional test1(){
return service.findById(5);
}
@RequestMapping("/test2")
public List test2(){

    List list=service.findAll();
    return list;
}

}`

完成的效果在这里插入图片描述

你可能感兴趣的:(异常处理)