嘗試將課程物件插入到 H2 資料庫中,我在該行遇到錯誤
if(course.getId()==null)
說沒有為引數型別 long,null 定義運算子。也試過
if(course.getId()==0)
同樣的問題。
這是課程物體/班級
@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$$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
uj5u.com熱心網友回復:
Long id;
public long getId() {
return id;
}
該錯誤是由您的回傳型別 long 而不是 Long 引起的。對于long回傳型別,需要通過方法將Long從Long轉換為long:
Long.longValue()
所以這就是為什么當 id 為 null 時你得到 nullpointerexception 的原因。只需將型別 long 更改為 Long 并重新檢查...
更改自:
long getId()
到
Long getId()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322123.html
上一篇:一個區間內的多個最大值
下一篇:無法將值插入到h2資料庫表中
