【问题标题】:PSQLException: ERROR: relation doesn't existPSQLException:错误:关系不存在
【发布时间】:2022-03-21 06:28:27
【问题描述】:

我正在使用 Hibernate 和 PostgreSQL 数据库。 我有几个模式(每个模式中有很多表)和一个通用方法,它接受一个实体类并根据传递的实体返回所有条目:

public <T> List<T> getAll(Class<T> entityClass)

在找到实体的表名(例如“EMPLOYEE”)后,它会执行List list = criteria.list(); 创建此sql SELECT * FROM EMPLOYEE。但它会抛出PSQLException: ERROR: relation doesn't exist,因为它必须是SELECT * FROM MYSCHEMA.EMPLOYEE(必须附加模式)。如何处理?

这不是我的方法,因为我在持久层中使用 Spring Data JPA,它在识别模式方面没有问题,而且我不擅长原生休眠。我正在努力帮助我的同事。

方法如下:

 @Override
@Transactional
public <T> List<T> getAll(Class<T> entityClass, Map<String, Object> filter, String... fetchProfiles) {
    Session session;
    try {
        session = sessionFactory.getCurrentSession();

        if (fetchProfiles != null) {
            for (String fetchProfile : fetchProfiles) {
                session.enableFetchProfile(fetchProfile);
            }
        }
        Criteria criteria = session.createCriteria(entityClass);

        if (filter != null) {
            for (Map.Entry<String, Object> entry : filter.entrySet()) {
                if (entry.getValue() == null) {
                    criteria.add(Restrictions.isNull(entry.getKey()));
                } else {
                    if (entry.getValue() instanceof Collection) {
                        criteria.add(Restrictions.in(entry.getKey(), (Collection) entry.getValue()));
                    } else if (entry.getValue() instanceof BetweenValue) {
                        BetweenValue bv = (BetweenValue) entry.getValue();
                        if (bv.getBoundLeft() != null) {
                            criteria.add(Restrictions.ge(entry.getKey(), bv.getBoundLeft()));
                        }
                        if (bv.getBoundRight() != null) {
                            criteria.add(Restrictions.le(entry.getKey(), bv.getBoundRight()));
                        }
                    } else if (BaseEntity.class.isAssignableFrom(ReflectionUtils.findField(entityClass, entry.getKey()).getType()) && entry.getValue() instanceof Long) {
                        BaseEntity fieldEntity = null;
                        try {
                            fieldEntity = (BaseEntity) ReflectUtils.createFieldInstance(entityClass, entry.getKey());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        fieldEntity.setId((Long) entry.getValue());
                        criteria.add(Restrictions.eq(entry.getKey(), fieldEntity));
                    } else {
                        criteria.add(Restrictions.eq(entry.getKey(), entry.getValue()));
                    }
                }
            }
        }
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        List list = criteria.list();
        if (fetchProfiles != null) {
            for (String fetchProfile : fetchProfiles) {
                session.disableFetchProfile(fetchProfile);
            }
        }
        return list;
    }

【问题讨论】:

  • @Billy Frost 检查编辑
  • @Billy Frost 有没有办法告诉 Criteria 要查找的架构?
  • 你得到的确切错误是什么(检查你的 postgres 日志文件,它应该告诉你不存在的关系的名称)以及表的实际名称是什么(检查 psql/ pgadmin)。我的猜测是存在区分大小写的问题。
  • @eurotrash 错误是:PSQLException:错误:关系不存在。因为session.createCriteria(entityClass) 在创建 SQL 查询时不会附加架构名称。所以SELECT * FROM EMPLOYEE 抛出了上面的错误。 sql查询必须是这样的:SELECT * FROM MYSCHEMA.EMPLOYEE

标签: java postgresql hibernate hibernate-criteria


【解决方案1】:

对于 Postgres,如果你的表名不是小写,你可能会得到这个

【讨论】:

    猜你喜欢
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多