Hibernate Criteria: relation “my_table” does not exist - java

I have an entity:
#Table(schema="my_schema",name="my_table")
public class MyTable
...
And i want to retrieve records from that table:
Criteria criteria = session.createCriteria(entityClass);
List list = criteria.list();
I get:
PSQLException: ERROR: relation “my_table” does not exist
I suspect that it's because of the missing schema name in front of my_table but how to add it, or maybe there's another reason?

Is your schema name begin with uppercase?
i have a similar case, my table name is users and schema is D2018.
i've got error like this :
org.postgresql.util.PSQLException: ERROR: relation "d2018.users" does not exist
it seems hibernate try to connect to d2018 schema not D2018, so i rename my schema to d2018 and now it's work flawless

Related

QuerySyntaxException when trying to validate a table of unclean records to a new table with clean data. Necessary to map entities?

I have an unclean UncleanRecord tbl with dirty and unvalidated data.
My plan is to go through all the records in the tbl, and then clean and validate it to add into my new CleanRecord tbl.
I am using Spring Batch with Hibernate. When I execute the job, it returned these error:
org.springframework.batch.core.step.AbstractStep - Encountered an error executing step step1 in job validate
org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: UNCLEANRECORD is not mapped [SELECT name, contact,address FROM UNCLEANRECORD WHERE date is null]
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: UNCLEANRECORD is not mapped
I have entity UncleanRecord, and CleanRecord.
With the error in console log, I suppose that I need to map UncleanRecord to CleanRecord? However, the data in Unclean is inconsistent--- there is also no unique identifiers in UncleanRecords... it's purely to store whatever data that is passed into the application. The rows in UncleanRecord would be checked against CleanRecord before being added into CleanRecord
Is it possible to achieve what I want to without Hibernate mapping?
I understand I can do the above with RowMapper<>, however, I am using Hibernate.
Looks like you are using a HQL/JPQL query which requires an entity named UNCLEANRECORD to exists but it doesn't. I don't know how spring-batch works, but it looks like you should mark the query as being a native query instead.

hibernate query involving foreign key

I am trying to write a hibernate query which selects a number of records from a table based on some criteria.
I have two relevant tables in my database
tbleventattendees which has the following fields - eventAttendeeRecord tblevent, tblmembers, and memberComments;
tblevent which has the following fields relevant field eventID which is the tblevent foreign key in tbleventattendees
What I am trying to do is write a HQL query which shows the attendees the attendees at specific event e.g. something like this SQL query select * from tbleventattendees where tblevent = 1
However, whilst this works as an SQL query in MySQL workbench when I try from tbleventattendees where tblevent = 1 in Netbeans run HQL Query I get the following error
org.hibernate.QueryException: Incorrect query syntax [ FROM
Society.Tbleventattendees as attendees where Tblevent =1
]
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:259)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:209)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
Caused by: java.lang.NullPointerException
at org.hibernate.sql.QuerySelect.appendTokens(QuerySelect.java:185)
at org.hibernate.sql.QuerySelect.setWhereTokens(QuerySelect.java:103)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.renderSQL(QueryTranslatorImpl.java:625)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:243)
... 9 more
I think this is because I am using the value in the database in the query rather than the object reference as when I remove the =1 criteria I get the following value in that column
Society.Tblevent#6a2437ae
Can someone please help, I have read through lots of posts on here but struggling to work out the answer
You can write somthing like this:
Query query = session.createQuery("from Tbleventattendees attendees where attendees.tblevent.eventID = :tbleventId ");
query.setParameter("tbleventId", "1");
List list = query.list();

Error executing a Query using JPA

I'm trying to sort a resultset using the SQL statement Order by using JPA, on a datetime column data type with this string, on a Mysql database:
Query query = em.createQuery("SELECT e FROM Events e Order by e.EventDateTime;");
Using the createQuery method java returns the error:
SEVERE: Local Exception Stack:
Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):
org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [Events.findByGameId: SELECT e FROM Events e WHERE e.gameId =
:gameId ORDER BY e.EventDateTime DESC], line 1, column 59: unknown state or association field [EventDateTime] of class [com.jogogestao.entity.Events].
at org.eclipse.persistence.exceptions.JPQLException.unknownAttribute(JPQLException.java:457)
at org.eclipse.persistence.internal.jpa.parsing.DotNode.validate(DotNode.java:88)
at org.eclipse.persistence.internal.jpa.parsing.OrderByItemNode.validate(OrderByItemNode.java:52)
at org.eclipse.persistence.internal.jpa.parsing.OrderByNode.validate(OrderByNode.java:61)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:210)
I tried sorting by the integer type primary and all runs ok...but this is not what I want of course.
Using createNativeQuery the statement runs ok...
Query query = em.createNativeQuery("SELECT * FROM Events Order by EventDateTime;");
The only problem is that the return object is not an Events type object (from the entity) and I can't convert to this type.
Maybe the problem is that JPA does not support sorting on datetime fields?
How can I get around this?
I'm using Netbeans 7.0.1, Glassfish 3.1.1, MySql 5.5.19 Community Server (GPL) and mysql-connector-java-5.1.15-bin.jar.
Thanks!
SELECT e FROM Event e ORDER BY e.eventDateTime
you don't have * - you have to specify the entity you select
don't put a semicolon at the end
use all-lower-case keywords, capital-case entity names, and lower-camel-case property names
name the entity in singular, not plural (Event vs Events)
The message is clear: you don't have an EventDateTime mapped property (or field, if fields are mapped directly) in the entity Events. If you respect the Java conventions, the field/property should be named eventDateTime, and not EventDateTime.

Why does my update query that works in SQL Server 2005 Management Studio not work as a Prepared Query in Java?

I have a Table [Name Table] and an associated table workSchedule. I'm using Hibernate 3.6.7.Final to generate my query. The result is:
update Personnel.dbo.[Name Table] set workSchedule=? where [Name IRC]=?
Which throws an Exception:
11-22#10:30:41 WARN [] JDBCExceptionReporter - SQL Error: 8624, SQLState: S0001
11-22#10:30:41 ERROR [] JDBCExceptionReporter - Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services.
[Name Table].workSchedule is a foreign key defined thus:
ALTER TABLE [Name Table] ADD workSchedule VARCHAR(34)
FOREIGN KEY REFERENCES workSchedule(id);
workSchedule.id is defined like this:
CREATE TABLE workSchedule
( /* format = letter-days-lunch EX: A-5-1 */
id AS CASE lunch
WHEN 1 THEN scheduleLetter+'-'+CONVERT(VARCHAR, scheduleDays)+'-'+'1'
ELSE scheduleLetter+'-'+CONVERT(VARCHAR, scheduleDays)+'-'+'5'
END PERSISTED NOT NULL,
/* rest of table follows */
);
If I copy paste the above update query into SSMS and plug values directly in for the ?s it works.
UPDATE:
I just tried changing out the PRIMARY KEY of the table WorkSchedule for an INT IDENTITY column. I left renamed id to shift and otherwise left it as column on the table. I also updated both POJO's and .hbm.xml files. The update query still fails, with the same exception.
I'm listing this as an answer because it's what I ended up doing to solve my problem.
I completely removed the calculated field on WorkSchedule.
I implemented the calcualations as a getter in the the POJO instead.
Re-tooled workschedule to use an IDENTITY for the PK
and re-linked [Name Table] to the new IDENTITY field instead. now it all works.

Syntax error in JPA query

When I execute the following code
return entityManager
.createQuery("select a from Article where a.slug = ?1", Article.class)
.setParameter(1, slug)
.getSingleResult();
I get the following exception
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select a from Article where a.slug = '?1'], line 1, column 22: syntax error at [where].
Internal Exception: MismatchedTokenException(77!=78)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1328)
I'm using JPA 2 with EclipseLink 2.0.2.
What is wrong with my query?
... From Article a ... (missing alias)
Well, the answer has alredy given.. But what I dont like about JPQL, you have to put an identifier after Entity name, but it is uncessary if your from clause does have only one Entity. Most of time, I also forget to put that unnecessarily required identifier.
I wish I would write the above query as below;
select * from Article where slug = ?1

Categories

Resources