簡體   English   中英

org.postgresql.util.PSQLException:錯誤:列 systementi0_.id 不存在 - Hibernate、PostgreSql

[英]org.postgresql.util.PSQLException: Error: column systementi0_.id does not exist - Hibernate, PostgreSql

我收到錯誤“列“systementi0_.Id 不存在”。我正在使用 PostgreSql 和 Hibernate。我的實體有代碼:

@Entity
@Table(name = "system_table", schema = "blue_schema")
public class SystemEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", unique = true)
    private int id;

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

    @Column(name = "sys_description")
    private String systemDescription;

    @Column(name = "tech_description")
    private String technologyDescritpion;

    @Column(name = "owner_name")
    private String owner;

    public SystemEntity() {
        // TODO Auto-generated constructor stub
    }

    public SystemEntity(int id, String name, String systemDescription, String technologyDescritpion, String owner) {
        super();
        this.id = id;
        this.name = name;
        this.systemDescription = systemDescription;
        this.technologyDescritpion = technologyDescritpion;
        this.owner = owner;
    }

    public SystemEntity(String name, String systemDescription, String technologyDescritpion, String owner) {
        super();
        this.name = name;
        this.systemDescription = systemDescription;
        this.technologyDescritpion = technologyDescritpion;
        this.owner = owner;
    }

在開始時,我試圖從數據庫中獲取該表中的所有數據。

    @Override
    public List<SystemEntity> getAllSystems() {

        Session currentSession = sessionFactory.getCurrentSession();

        Query<SystemEntity> theQuery = currentSession.createQuery("from SystemEntity", SystemEntity.class);

        List<SystemEntity> listOfSystems = theQuery.getResultList();

        return listOfSystems;
    }

那是我的數據庫類型和 output:表 output

表類型

我的模式名稱在 @Table 注釋中得到了澄清,但仍然出現錯誤:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/ContractManager] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
org.postgresql.util.PSQLException: Error: columnsystementi0_.id does not exist.

我看到了類似的帖子,但解決方案很簡單,只需在 @Table 中添加模式名稱。

也許你應該使用Integer ,數據類型而不是int ,你應該嘗試直接使用表名。 system_table

這是一個常見的錯誤。 第一個建議:不要在所有數據庫實體的名稱中使用大寫:模式、表或列。 您的id列名稱具有“ Id ”。 試試“ id ”。 第二:我推薦你在postresql中使用序列生成器:

@Entity
@Table(name = "system_table", schema = "blue_schema")
@SequenceGenerator(schema = "blue_schema", name = "system_table_seq", allocationSize = 1, sequenceName = "system_table_seq")
public class SystemEntity {

    // other code below 
    // ... 

}

最后一個建議:不要在 JPA 實體中使用原始類型。 嘗試:

@Entity
@Table(name = "system_table", schema = "blue_schema")
@SequenceGenerator(schema = "blue_schema", name = "system_table_seq", allocationSize = 1, sequenceName = "system_table_seq")
public class SystemEntity {

    @Id
    @GeneratedValue(generator = "system_table_seq", strategy = GenerationType.SEQUENCE)
    private Integer id;

    // other code below 
    // ... 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM