运算符 == 未定义参数类型 long、null

The operator == is undefined for the argument type(s) long, null

试图将课程对象插入到 H2 数据库中时,我在第 行遇到错误

if(course.getId()==null)

表示没有为参数类型 long,null 定义运算符。也试过

if(course.getId()==0)

同样的问题。

这是课程entity/class

@Entity
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

private String name;

public Course() {
}

public Course(String name) {
    super();
    this.name = name;
}

public long getId() {
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return "Course [name=" + name + "]";
}

}


这是DAO层

@Repository
@Transactional
public class CourseRepository {

@Autowired
EntityManager em;

public Course findById(Long id) {
    return em.find(Course.class, id);
}

public void deleteById(Long id) {
    em.remove(findById(id));
    
}

public Course save(Course course) {
        if(course.getId()==null){ 
        em.persist(course);
    }
    else {
        em.merge(course);
    }
    return course;
}

}

来自控制台的一小段

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:794) ~[spring-boot-2.5.5.jar:2.5.5]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:775) ~[spring-boot-2.5.5.jar:2.5.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:345) ~[spring-boot-2.5.5.jar:2.5.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.5.jar:2.5.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.5.jar:2.5.5]
    at com.in28minutes.jpa.hibernate.demo.DemoApplication.main(DemoApplication.java:21) ~[classes/:na]
Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because "this.id" is null
    at com.in28minutes.jpa.hibernate.demo.entity.Course.getId(Course.java:25) ~[classes/:na]
    at com.in28minutes.jpa.hibernate.demo.repository.CourseRepository.save(CourseRepository.java:29) ~[classes/:na]
    at com.in28minutes.jpa.hibernate.demo.repository.CourseRepository$$FastClassBySpringCGLIB$d4415f4.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.10.jar:5.3.10]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.10.jar:5.3.10]
    at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.10.jar:5.3.10]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.10.jar:5.3.10]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.10.jar:5.3.10]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.10.jar:5.3.10]
    at com.in28minutes.jpa.hibernate.demo.repository.CourseRepository$$EnhancerBySpringCGLIB$2dd48f.save(<generated>) ~[classes/:na]
    at com.in28minutes.jpa.hibernate.demo.DemoApplication.run(DemoApplication.java:29) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) ~[spring-boot-2.5.5.jar:2.5.5]
    ... 5 common frames omitted
Long id;    
public long getId() {
        return id;
    }

错误是由于您 return 键入的是 long 而不是 Long。 对于 long return 类型,需要通过方法将 Long 转换为 long:

Long.longValue()

这就是为什么当 id 为 null 时出现 nullpointerexception。只需将类型 long 更改为 Long 并重新检查...

更改自:

long getId()

Long getId()