在Spring Boot中检索mysql视图时为什么出现“未知列”错误?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Spring Boot中检索mysql视图时为什么出现“未知列”错误?相关的知识,希望对你有一定的参考价值。
我正在尝试在Spring Boot中执行mysql视图,但出现以下错误:
java.sql.SQLSyntaxErrorException:未知列“字段列表”中的“ appointmen0_.lab_collected”
但是很明显,lab_collected变量存在于域bean中。列名是否必须相同?我希望它像“ LabCollected”一样显示,而不是“ lab_collected”。
这里是视图的输出,只是为了向您展示它的工作:
mysql>从vw_appointments中选择*;
+----+------------+-------------+---------------------+-------------------+
| id | Date | Physician | LabCollected | Note |
+----+------------+-------------+---------------------+-------------------+
| 1 | 10/29/2010 | CAMPBELL, J | 2010-10-29 11:09:00 | no note available |
+----+------------+-------------+---------------------+-------------------+
这里是域bean:
package net.tekknow.medaverter.domain;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
@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
private Integer id;
public Integer getId() {
return this.id;
}
@Size(max = 32)
@Column(name="Date")
public String date;
@Column(name="Physician")
public String physician;
@Column(name="LabCollected")
public Timestamp lab_collected;
@Column(name="Note")
public String note;
public String getDate() {
return date;
}
public String getPhysician() {
return physician;
}
public Timestamp getLabCollected() {
return lab_collected;
}
public String getNote() {
return note;
}
}
这里是控制器:
@RestController
public class AppointmentViewController {
@Autowired
AppointmentViewService appointmentViewService;
@CrossOrigin
@GetMapping("/appointment_view")
public List<AppointmentView> viewAppointmentsPage(Model model) {
List<AppointmentView> appointments = appointmentViewService.listAll();
return appointments;
}
}
这里是服务:
@Service
@Transactional
public class AppointmentViewService {
@Autowired
AppointmentViewRepository repo;
public List<AppointmentView> listAll() {
return repo.findAll();
}
}
这里是存储库:
public interface AppointmentViewRepository extends JpaRepository<AppointmentView,Integer> {}
有什么建议吗?
答案
如果您明确提供@Column,则应使用该名称。尝试在application.properties文件中添加spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
。请参考答案:Spring Boot + JPA : Column name annotation ignored以上是关于在Spring Boot中检索mysql视图时为什么出现“未知列”错误?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring Boot 中使用复合主键从 MySql 中检索数据
从 Spring Boot 1.5 升级时为 Spring Boot 2.0 acuator 框架配置安全性
使用 Spring Boot 在 JPA 中访问 MySQL 视图