gpt4 book ai didi

java - Hibernate列别名用作实际列名?

转载 作者:行者123 更新时间:2023-12-01 18:15:20 25 4
gpt4 key购买 nike

正在开发一个项目,我需要与数据库交互并检索各种列。当我尝试从数据库查询时,Hibernate 创建与数据库的实际列名不匹配的别名。因此,我收到 java.sql.SQLException: Column not found 异常。

映射的类:

package com.ots.Models;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="My_Table")
public class MyRecord {

@EmbeddedId
private RecordId id;

@Column(name="FILE_NAME")
private String File_Name;

@Column(name="KEY")
private String key;

@Column(name="MET_TYPE")
private String met_type;

@Column(name="DSHARED")
private String dShared;

@Column(name="SER")
private String ser;

@Column(name="SERVI")
private int servi;

@Column(name="DESCRIPTION")
private String description;

public MyRecord(RecordId id, String File_Name, String key, String met_type,
String dShared, String ser, int servi, String description) {
this.id = id;
File_Name = File_Name;
this.key = key;
this.met_type = met_type;
this.dShared = dShared;
this.ser = ser;
this.servi = servi;
this.description = description;
}



public RecordId getId() {
return id;
}

public void setId(RecordId id) {
this.id = id;
}

public String File_Name() {
return File_Name;
}

public void setFile_Name(String File_Name) {
File_Name = File_Name;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getMet_type() {
return metric_type;
}

public void setMet_type(String met_type) {
this.metric_type = metric_type;
}

public String getDShared() {
return dShared;
}

public void setDShared(String dShared) {
this.dShared = dShared;
}

public String getService_cat_no() {
return service_cat_no;
}

public void setSer(String ser) {
this.ser = ser;
}

public int getServi() {
return servi;
}

public void setServi(int servi) {
this.servi= servi;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}




}

调用类:

@Controller
@RequestMapping("/history")
public class MyTable {

@SuppressWarnings("unchecked")
@GetMapping("/load")
public String loadData(Model theModel) {

//Get all records out of the table from Hibernate

Session newSession = Statics.getSessionFactory().openSession();
newSession.beginTransaction();


//load all data from the Historical table

String hqlQuery = "from MyRecord";

Session session = Statics.getSessionFactory().openSession();

Query<CrossReferenceRecord> query = session.createQuery(hqlQuery);

List<CrossReferenceRecord> crossReferenceList = query.getResultList();

theModel.addAttribute("crossReferenceList", crossReferenceList);

return "CrossReferenceView";


}

}

数据库架构:

int id, VarChar(20) File_Name, VarChar(20) key, VarChar(20) met_type, VarChar(20) dShared, VarChar(20) ser, int servi, VarChar(20) description

完整错误:

Hibernate: select crossrefer0_.FILE_NAME as FIL_BUI3_0_, ... java.sql.SQLException: Column not found: FIL_BUI3_0_

这似乎是一个奇怪的问题 - 我希望 Hibernate 不需要这种配置。

我们如何解决这个问题?

最佳答案

我不确定这是否是确切的问题,但您的查询是从 MyRecord 中进行选择,这是类的名称,而不是表的名称。我认为您想从 My_Table 进行查询。另外,在调用 createQuerygetResultList 时,是否要将这些结果保存到 MyRecord 类中?

关于java - Hibernate列别名用作实际列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60381246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com