Trying to insert a course object into the H2 database, I encountered an error at the line if(course.getId()==null) saying that the operator is not defined for the argument types long,null. Also tried with if(course.getId()==0), same issue.
This is the course 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 "]";
}
}
This is the DAO layer
@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;
}
}
A small snippet from the console
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$$6d4415f4.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$1.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$$862dd48f.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
CodePudding user response:
Long id;
public long getId() {
return id;
}
The error is caused by your return type long instead of Long. with long return type, it's required to convert to long from Long by method:
Long.longValue()
So that's why you got nullpointerexception when id is null. Just change the type long to Long and recheck...
change from:
long getId()
to
Long getId()