![](/img/trans.png)
[英]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.