因为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);


补充:可能会出现如下报错

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.*;

相关文章:

  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2021-09-20
  • 2021-07-29
猜你喜欢
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2021-06-18
  • 2021-06-09
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案