java - org.postgresql.util.PSQLException : Error: column systementi0_. id 不存在 - Hibernate、PostgreSql

标签 java spring postgresql hibernate

我收到错误“列“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;
    }

这是我的数据库类型和输出: Table output

Table types

我的模式名称在@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 中添加架构名称即可。

最佳答案

这是一个常见的错误。第一个建议:不要在所有数据库实体的名称中使用大写字母:模式、表或列。 您的 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 
    // ... 

}

关于java - org.postgresql.util.PSQLException : Error: column systementi0_. id 不存在 - Hibernate、PostgreSql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58477715/

相关文章:

java - session 作用域 bean 不是由 DispatcherServlet 创建的

java - 无法在 Springboot/Hibernate 中保存具有 OneToMany 关系的子对象

spring - 什么时候在 Spring 中触发 ContextRefreshedEvent?

java - 合并两个数组而不使用额外空间

java - 使用 XML 解析器实现作为 OSGi 服务

postgresql - 在数据库中插入数据时出现客户端编码错误(我在 Codeigniter 中使用 Postgre SQL)

postgresql - 克洛贾尔/后gresql : How do I see the exception when an error is in the db?

matlab - 来自 PostgreSQL 的 Octave 数据库包时间戳

java - 为什么 i+=l 会编译,其中 i 是 int 而 l 是 long?

java - 数据库 - 有 2 个表,需要另一个具有 ID 和另一个字段的表