springdataJpa對無主鍵表或視圖查詢的支持


因為jpa在映射實體是需要一個id,所以我們的實體類必須至少需要一個id字段,當對無主鍵表或視圖查詢時,我們可以定義一個空的@id即可。

示例如下

實體:

@Data
@Entity
@Table(name="Student")
public class Student {
   @Id // 添加一個空的id標識,因為jpa在映射實體是需要一個id,這個必須

   @Column(name = "shool")
private Long shool;

private String name;

private int age;

private String address;
}
接口:

  @Query(value = "select s from Student s where  age in (?1))
  public List<Student > findByIdToIn(  List<Integer> sysage);


補充:可能會出現如下報錯

AnnotationException: No identifier specified for entity

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.liuyang.idea.Girl
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.6.BUILD-20180503.204253-49.jar:5.0.6.BUILD-SNAPSHOT]

原因可能是:entity  是否有引用錯包,錯誤的引入了Spring的注解的了

import org.springframework.data.annotation.Id;

更正引入以下包即可

import javax.persistence.*;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM