PSQLException: ERROR: relation doesn't exist
Clash Royale CLAN TAG#URR8PPP
PSQLException: ERROR: relation doesn't exist
I'm using Hibernate and PostgreSQL database. I have several schemas (and many tables in each of them) and a generic method which takes an entity class and returns all entries depending on passed entity:
public <T> List<T> getAll(Class<T> entityClass)
public <T> List<T> getAll(Class<T> entityClass)
After finding the table name of entity ("EMPLOYEE" for example), it does List list = criteria.list();
which creates this sql SELECT * FROM EMPLOYEE
. But it throws PSQLException: ERROR: relation doesn't exist
because it must be SELECT * FROM MYSCHEMA.EMPLOYEE
(schema must be appended). How to handle it?
List list = criteria.list();
SELECT * FROM EMPLOYEE
PSQLException: ERROR: relation doesn't exist
SELECT * FROM MYSCHEMA.EMPLOYEE
This is not my method, as i'm using Spring Data JPA in persistence layer and it has no problem with identifying a schema and i'm not good in native hibernate. I'm trying to help my colleague.
And here's the method:
@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 Is there a way to tell Criteria which schema to look for?
– Mukhamedali Zhadigerov
Aug 8 at 14:26
What's the exact error you're getting (check your postgres log file, it should tell you the name of the relation that doesn't exist) and what's the actual name of the table (check psql/pgadmin). My guess is there's a case sensitivity issue.
– eurotrash
Aug 8 at 15:23
@eurotrash The error is: PSQLException: ERROR: relation doesn't exist. Because
session.createCriteria(entityClass)
doesn't append schema name while creating SQL query. So SELECT * FROM EMPLOYEE
throws the error above. Sql query must be as such: SELECT * FROM MYSCHEMA.EMPLOYEE
– Mukhamedali Zhadigerov
Aug 9 at 5:08
session.createCriteria(entityClass)
SELECT * FROM EMPLOYEE
SELECT * FROM MYSCHEMA.EMPLOYEE
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
@Billy Frost check to edit
– Mukhamedali Zhadigerov
Aug 8 at 14:13