org.postgresql.util.PSQLException: ошибка: столбец systementi0_.id не существует - Hibernate, PostgreSql - PullRequest
0 голосов
/ 21 октября 2019

Я получаю сообщение об ошибке "column" 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, но все еще появляется ошибка:

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.

Я видел похожий пост, похожий на мой, но решение было простым, просто добавьте имя схемы в@ Таблица.

Ответы [ 2 ]

0 голосов
/ 21 октября 2019

Это распространенная ошибка. Первая рекомендация: не используйте заглавные буквы в имени всех объектов базы данных: схем, таблиц или столбцов. Ваше имя столбца 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 
    // ... 

}
0 голосов
/ 21 октября 2019

возможно, вам следует использовать Integer, тип данных вместо int, и вы должны попытаться использовать имя таблицы напрямую. system_table

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...