如何在Spring Boot中显示mysql视图?

use*_*883 1 java hibernate jpa spring-boot

只要我使用常规 MySQL 表,我就有一个正在运行的 Spring Boot CRUD 应用程序。但我需要显示多个表中的数据,所以我创建了一个 MySQL 视图。但现在出现以下错误:

创建类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义的名为“entityManagerFactory”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:net.tekknow.medaverter.domain.AppointmentView

我正在关注这个例子: https ://www.javabullets.com/calling-database-views-from-spring-data-jpa/

这是域对象:

package net.tekknow.medaverter.domain;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.Size;

@Entity
@Table(name = "vw_appointments")
public class AppointmentView implements Serializable {
    @Size(max = 32)
    @Column(name="Date")
    public String date;

    @Column(name="Physician")
    public String physician;

    @Column(name="LabCollected")
    public Timestamp labCollected;

    @Column(name="Note")
    public String note;

    public String getDate_time() {
        return date;
    }
    public String getPhysician() {
        return physician;
    }
    public Timestamp getDatetime_collected() {
        return labCollected;
    }
    public String getNote() {
        return note;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是视图的 mysql 查询,只是为了向您展示它的工作原理:

mysql> 从 vw_appointments 中选择 *;+------------+-------------+--------------------+ -------------------+ | 日期 | 医师| 实验室收集 | 注意| +------------+-------------+--------------------+ -------------------+ | 2010 年 10 月 29 日 | 坎贝尔,J | 2010-10-29 11:09:00 | 没有可用的注释 | +------------+-------------+--------------------+ -------------------+ 一组 1 行(0.02 秒)

这是服务代码:

@Service
@Transactional
public class AppointmentViewService {

    @Autowired
    AppointmentViewRepository repo;

    public List<AppointmentView> listAll() {
        return repo.findAll();
    }      
}
Run Code Online (Sandbox Code Playgroud)

这是存储库代码:

public interface AppointmentViewRepository extends JpaRepository<AppointmentView,Integer> {}
Run Code Online (Sandbox Code Playgroud)

Hibernate 不处理视图吗?建议?

Oli*_*ter 5

错误消息中说明了所有内容:

没有为实体指定标识符:net.tekknow.medaverter.domain.AppointmentView

设计您的视图,使其具有 1 列,可以使用@Id注释将其映射为实体的标识符字段。

您还可以将@org.hibernate.annotations.Immutable注释添加到实体,以确保 Hibernate 不会尝试传播您对实体所做的更改

@Entity
@Table(name = "vw_appointments")
// Prevent changes from being applied by Hibernate
@org.hibernate.annotations.Immutable
public class AppointmentView implements Serializable {
    // Identifier. Has to be Integer as you implement JpaRepository<AppointmentView,Integer>
    @Id
    @Column(name="Appointment_Id")
    private Integer appointmentId;

    public Integer getAppointmentId() {
        return this.appointmentId;
    }

    public void setAppointmentId(Integer appointmentId) {
        this.appointmentId = appointmentId;
    }

    // Public attributes ???. 
    // If it is not mandatory for technical reasons, prefer private attributes + getter/setter
    @Size(max = 32)
    @Column(name="Date")
    public String date;

    @Column(name="Physician")
    public String physician;
    ...
}
Run Code Online (Sandbox Code Playgroud)