java - 如何在Spring Boot中显示mysql View ?

标签 java hibernate spring-boot jpa

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

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: 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;
    }
}

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

mysql> select * from vw_appointments; +------------+-------------+---------------------+-------------------+ | Date | Physician | LabCollected | Note | +------------+-------------+---------------------+-------------------+ | 10/29/2010 | CAMPBELL, J | 2010-10-29 11:09:00 | no note available | +------------+-------------+---------------------+-------------------+ 1 row in set (0.02 sec)

这是服务代码:

@Service
@Transactional
public class AppointmentViewService {

    @Autowired
    AppointmentViewRepository repo;

    public List<AppointmentView> listAll() {
        return repo.findAll();
    }      
}

这是存储库代码:

public interface AppointmentViewRepository extends JpaRepository<AppointmentView,Integer> {}

Hibernate 不处理 View 吗?有建议吗?

最佳答案

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

No identifier specified for entity: net.tekknow.medaverter.domain.AppointmentView

设计您的 View ,使其具有 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;
    ...
}

关于java - 如何在Spring Boot中显示mysql View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61504340/

相关文章:

java - Spring 集成: TcpOutboundGateway getting DestinationResolutionException: no output-channel or replyChannel header available

java - 如何在Java中读回一个字节?

java - 查询在 MySQL 提示符中执行,但不在应用程序中执行

java - 如何使用 JPA (Hibernate) 创建与同一实体的关系?

java - SpringBoot使用@RequestBody注释将整数原子转换为 boolean 值?如何拒绝将整数转换为 boolean 值?

java - 尽管文件存在,Mockito 返回 FileNotFoundException

java - java.time 中 Calendar.roll 的等价物是什么?

java - 转换过程中的一般错误 : Unsupported class file major version 61 in Linux when I build an android app

java - 如何修复 Hibernate LazyInitializationException : failed to lazily initialize a collection of roles, 无法初始化代理 - 无 session

java - Eclipse + hibernate 开发。需要生成sql脚本(或创建数据库)的工具