[英]org.postgresql.util.PSQLException: Error: column systementi0_.id does not exist - Hibernate, PostgreSql
[英]org.postgresql.util.PSQLException: ERROR: column column0_.pk does not exist Position: 8
我一直在寻找几个小时来解决这个问题,但我找不到或解决它。
我正在使用 Hibernate 对已经存在的表运行几个查询,但我总是遇到同样的错误:
org.postgresql.util.PSQLException: ERROR: column config0_.pk does not exist
Position: 8
我的数据库:
这是实体 Class:
package org.package;
// default package
// Generated 16/12/2021, 14:24:34 by Hibernate Tools 4.3.5.Final
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
/**
* Config generated by hbm2java
*/
@Entity
@Table(name = "config", schema = "schema")
public class Config implements java.io.Serializable {
private long pk;
private Request request;
private String key;
private String value;
private String description;
private String createdBy;
private Date createdDate;
private String modifiedBy;
private Date modifiedDate;
private Set<ConfigInf> configInfs = new HashSet<ConfigInf>(0);
public Config() {
}
public Config(long pk, String key, String value, String createdBy, Date createdDate) {
this.pk = pk;
this.key = key;
this.value = value;
this.createdBy = createdBy;
this.createdDate = createdDate;
}
public Config(long pk, Request request, String key, String value, String description, String createdBy,
Date createdDate, String modifiedBy, Date modifiedDate, Set<ConfigInf> configInfs) {
this.pk = pk;
this.request = request;
this.key = key;
this.value = value;
this.description = description;
this.createdBy = createdBy;
this.createdDate = createdDate;
this.modifiedBy = modifiedBy;
this.modifiedDate = modifiedDate;
this.configInfs = configInfs;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PK", unique = true, nullable = false)
public long getPk() {
return this.pk;
}
public void setPk(long pk) {
this.pk = pk;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REQUEST_PK")
public Request getRequest() {
return this.request;
}
public void setRequest(Request request) {
this.request = request;
}
@Column(name = "KEY", nullable = false, length = 256)
public String getKey() {
return this.key;
}
public void setKey(String key) {
this.key = key;
}
@Column(name = "VALUE", nullable = false, length = 1024)
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
@Column(name = "DESCRIPTION", length = 1024)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "CREATED_BY", nullable = false, length = 256)
public String getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED_DATE", nullable = false, length = 29)
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Column(name = "MODIFIED_BY", length = 256)
public String getModifiedBy() {
return this.modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFIED_DATE", length = 29)
public Date getModifiedDate() {
return this.modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "config")
public Set<ConfigInf> getConfigInfs() {
return this.configInfs;
}
public void setConfigInfs(Set<ConfigInf> configInfs) {
this.configInfs = configInfs;
}
}
我的仓库:
我想避免使用本机查询,所以像这样使用它,如图所示。 我也知道我可以使用 findAll 或仅使用" FROM Config"
,但没有任何效果,并且都解决了相同的初始问题。
我想指出Config
是一个 Object,即 Entity。
package org.package;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import pt.link.synchronizer.shared.domain.Config;
import java.util.List;
@Repository
public interface ConfigRepository extends JpaRepository<Config, Long> {
@Query("SELECT c FROM Config c")
List<Config> getAllConfigs();
}
然后通过 Autowired 调用存储库,并在服务中使用,如下所示。
//element -> convertConfigToDto(element)
List<ConfigDTO> configs = configRepository.getAllConfigs().stream().map(element -> convertConfigToDto(element)).collect(Collectors.toList());
这是我的 application.properties:
也许我在 application.properties 中有太多内容,但是在尝试解决问题时,我发现缺少一些重要的东西。
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.jpa.database=POSTGRESQL
spring.datasource.platform= postgres
spring.datasource.url=jdbc:postgresql://server:port/schema
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=org.postgresql.Driver
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.default_schema:schema
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jmx.default-domain: umsspring.jpa.show-sql=true
spring.h2.console.enabled=true
调试时,我可以看到并确定问题来自存储库中的查询调用。
我想指出,使用 Native Query = true,这个问题就消失了,一切正常。 (例如:使用SELECT * FROM Config
是不是数据库有问题? 还是我缺少一些配置?
谢谢!
我会说"PK"
和"pk"
是两个不同的东西……至少从 PostgreSQL 的角度来看。 如果您在 DB 中使用大写字母,则应该以这种方式调用它。 如果你有小写字母 - 它可以通过小写/大写来调用。 除了使用"
引号进行显式调用外。将数据库中的列以小写字母表示,它应该可以工作。 在这里您可以看到一些解决方案,您可以使用 - 以防无法更改数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.