我正在使用 Spring Boot(和休眠)制作一個小型應用程式。我在制作這個應用程式時遇到了一個奇怪的問題,當我使用我的 DAO 物件將我創建的物件保存到資料庫中時。它引發了一個空指標例外,我還沒有弄清楚為什么,我檢查了通常的嫌疑人(DI),但我正在使用注釋@autowired,所以它應該可以作業。我差點把頭發扯掉,也許你會看到我錯過了什么,謝謝?
編輯:我已按照發布的說明進行操作。和 .,它有效,但前提是我注釋掉我的 deleteCategoryByName 方法。我在下面做了一些更改,我將 Demo1 類重命名為 LibraryApplication。我現在得到錯誤:
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryService': Unsatisfied dependency expressed through field 'categoryDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryDao': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract java.lang.Void consid.programmeringstest.library.dao.CategoryDao.deleteCategoryByName(java.lang.String) but parameter 'Optional[categoryName]' not found in annotated query 'DELETE FROM category WHERE name = :category_name '!
我的代碼
DAO 級
package library.dao;
import library.domain.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface CategoryDao extends JpaRepository<Category, Long> {
default Category findByIdOrThrow(Long id) {
return findById(id).orElseThrow();
}
@Modifying
@Query("DELETE FROM category WHERE name = :category_name ")
public void deleteCategoryByName(@Param("categoryName") String categoryName);
}
領域類
package library.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String categoryName;
public Category() {
}
public Category(String name) {
this.categoryName = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String name) {
this.categoryName = name;
}
}
服務級
package library.service;
import library.dao.CategoryDao;
import library.domain.Category;
import org.springframework.beans.factory.annotation.Autowired;
public class CategoryService {
@Autowired
private CategoryDao categoryDao;
public Category createCategory() {
Category result = new Category();
return categoryDao.save(result);
}
public Category createCategory(String name) {
Category result = new Category(name);
return categoryDao.save(result);
}
public void deleteCategoryByName(String name) {
categoryDao.deleteCategoryByName(name);
}
}
package library;
import library.dao.CategoryDao;
import library.domain.Category;
import library.service.CategoryService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableAutoConfiguration
public class LibraryApplication {
public static void main(String[] args) {
SpringApplication.run(LibraryApplication.class, args);
}
@Bean
public CommandLineRunner demo(CategoryDao categoryDao) {
return (args) -> {
categoryDao.save(new Category("Test"));
Category category = categoryDao.findByIdOrThrow(1);
System.out.println(category.getCategoryName());
};
}
}
表格和列的圖片。
uj5u.com熱心網友回復:
1.你應該在上面加一個@Component
/@Service
CategoryService
2.你沒有正確啟動spring boot app。你應該這樣做而不是Demo1
:
package library;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AccessingDataJpaApplication {
public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class, args);
}
}
更多資訊可以在https://spring.io/guides/gs/accessing-data-jpa/中找到
直接回答你的問題,你得到 NPE 的原因是因為categoryDao
inCategoryService
是 null ,因為你沒有正確啟動 spring boot。
附言
您應該注意,將您的應用程式類(AccessingDataJpaApplication
)放在層次結構(library
包)中“更高”的包中,然后您的其余組件將使這項作業脫離書本,否則您將不得不在@SpringBootApplication
注釋中提及正確的包
uj5u.com熱心網友回復:
首先你需要添加的是@SpringBootApplication
主類。第二 -SpringApplication.run(Demo1.class, args);
在 main(executable) 方法的第一行。另外,不要忘記添加@Service
到應用程式的服務類中。
uj5u.com熱心網友回復:
您的 DAO 類的引數名稱不正確categoryName
。應該是category_name
。并且還使用saveAndFlush()
而不僅僅是save()
CategoryDao.java
package library.dao;
import library.domain.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface CategoryDao extends JpaRepository<Category, Long> {
default Category findByIdOrThrow(Long id) {
return findById(id).orElseThrow();
}
@Modifying
@Query("DELETE FROM category WHERE name = :category_name ")
public void deleteCategoryByName(@Param("category_name") String categoryName);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476750.html
上一篇:在圓圈中重新定位物件
下一篇:在陣列方法中查找3的倍數并回傳