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