spring-data-jpa Error creating bean with name 'entityManagerFactory' defined in class path resource
阿新 • • 發佈:2018-12-05
專案地址:https://github.com/heng1234/springdata-jpa
問題描述:
剛開始實體類程式碼是這樣寫的
package com.hvly.springjp_1.com.hlvy.entity; import lombok.Data; import javax.persistence.*; /** * Customer * * @author heng **/ /** * 1、在@Entity下增加@NamedQuery定義 需要注意 query裡面的值也是JPQL 查詢引數也要和實體類對應起來 * 因為實際場景中這種破壞ENtity的侵入式很不美 也不方便 所以這種方式容易一萬 工作中也很少推薦 * 2、與之相對應的還有@NamedNativeQuery 用法一樣 唯一不同的是 query裡面放置的是原生SQL語句 而非實體類的欄位名字 * name query的名稱 規則:實體類.方法名` */ @NamedQuery(name = "Customer.findByFirstName", query = "select c from Customer c where c.firstaname = ?1") //@Data @Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)//自動增遞 private Long id; private String firstname; private String lastname; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } }
CustomerJpaRepository
package com.hvly.springjp_1.com.hlvy.repository; import com.hvly.springjp_1.com.hlvy.entity.Customer; import org.springframework.data.jpa.repository.JpaRepository; /** * Customer * * @author heng **/ public interface CustomerJpaRepository extends JpaRepository<Customer,Long>{ /** * @NamedQuery查詢 * @param firstName * @return */ Customer findByFirstName(String firstName); }
Controller
package com.hvly.springjp_1.com.hlvy.controller; import com.hvly.springjp_1.com.hlvy.entity.Customer; import com.hvly.springjp_1.com.hlvy.repository.CustomerJpaRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * CustomerController * * @author heng **/ @RestController @RequestMapping("customer") public class CustomerController { @Autowired private CustomerJpaRepository customerJpaRepository; /** * @NamedQuery查詢 * @param firstName * @return */ @RequestMapping("findByFirstName") Customer findByFirstName(String firstName){ return customerJpaRepository.findByFirstName(firstName); } }
結果啟動報錯: sql打印出來 from後面跟的是實體類路徑
接著我去網上看到這個https://blog.csdn.net/uhb6577/article/details/78998883
發現我已經加了@Entity註解
最後發現@NamedQuery(name = "Customer.findByFirstName",
query = "select c from Customer c where c.firstaname = ?1")
多寫了個字母a 應該是 where c.firstname = ?1 而不是 where c.firstaname = ?1