【问题标题】:how to display mysql view in spring boot?如何在spring boot中显示mysql视图?
【发布时间】:2020-08-13 16:35:42
【问题描述】:

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

创建类中定义的名称为“entityManagerFactory”的 bean 时出错 路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: 调用 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;
    }
}

这是视图的 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();
    }      
}

这里是存储库代码:

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

Hibernate 不处理视图吗?有什么建议吗?

【问题讨论】:

    标签: java hibernate spring-boot jpa


    【解决方案1】:

    错误信息中都说了:

    没有为实体指定标识符: 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;
        ...
    }
    

    【讨论】:

    • 非常感谢奥利维尔!添加了您的建议,现在不再出现错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2023-01-02
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多