只要我使用常规 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/