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.*;
posted @   狗艳艳花  阅读(5903)  评论(5编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示