How to remake this code to work with oracle. Before that, it worked with Postgres and there were no errors. Now it gives sql grammatical error.
public List<MyOrder> myOrderListNew(Company company){
Criteria criteria = session.getCurrentSession().createCriteria(MyOrder.class);
criteria.add(Restrictions.eq("company.id", company.getId()));
criteria.add(Restrictions.eq("removeorder", false));
criteria.add(Restrictions.eq("status", "new"));
criteria.addOrder(Order.desc("id"));
List<MyOrder> myOrders = criteria.list();
return myOrders;
}
about the : criteria.add (Restrictions.eq ("company.id", company.getId ()));
"company.id", here company this property in the entity class MyOrder.
StackTrace
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69)
java.sql.SQLSyntaxErrorException: ORA-01747: invalid user.table.column, table.column, or column specification
oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:494)
oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1054)
oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:623)
oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:252)
Error : 1747, Position : 144, Sql = select this_.myOrder_id as myOrder_id1_6_0_, this_.myorder_company_id as myorder_company_id9_6_0_, this_.courier_id as courier_id10_6_0_, this_.date as date2_6_0_, this_.date_hms as date_hms3_6_0_, this_.hide as hide4_6_0_, this_.removeorder as removeorder5_6_0_, this_.selected as selected6_6_0_, this_.shops_id as shops_id11_6_0_, this_.status as status7_6_0_, this_.sum as sum8_6_0_ from myorder this_ where this_.myorder_company_id=:1 and this_.removeorder=:2 and this_.status=:3 order by this_.myOrder_id desc, OriginalSql = select this_.myOrder_id as myOrder_id1_6_0_, this_.myorder_company_id as myorder_company_id9_6_0_, this_.courier_id as courier_id10_6_0_, this_.date as date2_6_0_, this_.date_hms as date_hms3_6_0_, this_.hide as hide4_6_0_, this_.removeorder as removeorder5_6_0_, this_.selected as selected6_6_0_, this_.shops_id as shops_id11_6_0_, this_.status as status7_6_0_, this_.sum as sum8_6_0_ from myorder this_ where this_.myorder_company_id=? and this_.removeorder=? and this_.status=? order by this_.myOrder_id desc, Error Msg = ORA-01747: invalid user.table.column, table.column, or column specification
oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:498)
oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1054)
oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:623)
oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:252)
oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:612)
Formatted, query you use is this. I've marked what seems to be wrong: column names can't be DATE nor SUM as these are reserved words (for a datatype and a function).
SELECT this_.myOrder_id AS myOrder_id1_6_0_,
this_.myorder_company_id AS myorder_company_id9_6_0_,
this_.courier_id AS courier_id10_6_0_,
this_.date AS date2_6_0_, --> this
this_.date_hms AS date_hms3_6_0_,
this_.hide AS hide4_6_0_,
this_.removeorder AS removeorder5_6_0_,
this_.selected AS selected6_6_0_,
this_.shops_id AS shops_id11_6_0_,
this_.status AS status7_6_0_,
this_.SUM AS sum8_6_0_ --> this
FROM myorder this_
WHERE this_.myorder_company_id = :1
AND this_.removeorder = :2
AND this_.status = :3
ORDER BY this_.myOrder_id DESC
What to do? Depends on table description. If you managed to create such a table, then you must have enclosed column names into double quotes. If that's so, you'll have to do the same every time you work with those columns, e.g.
SQL> create table myorder
2 ("DATE" date,
3 "SUM" number
4 );
Table created.
SQL> -- this is what you are currently doing; see? The same ORA-01747 error
SQL> select this_.date,
2 this_.sum
3 from myorder this_;
select this_.date,
*
ERROR at line 1:
ORA-01747: invalid user.table.column, table.column, or column specification
SQL> -- this is what you should be doing
SQL> select this_."DATE",
2 this_."SUM"
3 from myorder this_;
no rows selected
SQL>
If possible, change column names to make your life easier.
I'm trying to get count of users who have only one 1GB_DATA prize between two dates in JPA with Query annotation, but I'm getting an error.
How can I do it with JPA?
My SQL query:
select count(client_id) from (select client_id, count(client_id) from AV_CUSTOMER_PRIZE where prize_type ='1GB_DATA' and prize_date>'08.11.2017' and prize_date<'01.12.2017' group by client_id having count(client_id)=1);
My method:
#Query("select count(client_id) from (select client_id, count(client_id) from AV_CUSTOMER_PRIZE where prize_type ='1GB_DATA' and prize_date> :sDate and prize_date< :eDate group by " +
"client_id having count(client_id)=1)")
int getCount(#Param("sDate") Date startDate, #Param("eDate") Date endDate);
Error:
QuerySyntaxException: unexpected token: ( near line 1, column 30 [select count(client_id) from (select client_id, count(client_id) from AV_CUSTOMER_PRIZE where prize_type ='1GB_DATA' and prize_date> :sDate and prize_date< :eDate group by client_id having count(client_id)=1)]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:288)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
You can either
1) add nativeQuery=true to the #Query annotation. You may have problems with the date params though.
or
2) use only the inner query and expect a List:
#Query("select clientId, count(clientId) from AV_CUSTOMER_PRIZE
where prizeType ='1GB_DATA' and prizeTate> :sDate and prizeDate< :eDate
group by clientId having count(clientId)=1")
List<Object[]> getCount(#Param("sDate") Date startDate, #Param("eDate") Date endDate);
then you can simply invoke .size() on the list returned.
Also remember to use entity field names instead of native db column names in that query
The bottom line is that you cannot use a select statement in a from clause in JPA.
Still new to hibernate custom queries.
My table is
ID TID R1 Position
1 1 1 2
2 1 1 3
I want a custom query to delete rows with TID 1 and R1 1
My current sql looks like
#Query(value = "delete from Table t where t.TID= :tid and t.R1 = :r1", nativeQuery = true)
void deleteByTIDAndR1(#Param("tid") Integer TID, #Param("r1") Integer r1);
It gives me the following error:
.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
My controller retrieves the right tid and r1 ID's.
Is it even possible to delete multiple rows at once? And where could my error be?
Edit
After I add #Modyfying to the query i get the error
TransactionRequiredException: Executing an update/delete query
And after i add #Transactional in combination with #Modifying i get
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't where t.tid= 1 and t.R1 = 99' at line 1
I think you lack the #Modifying annotation, indicating a query that modify the database:
#Modifying
#Query(value = "delete from Table where TID= :tid and R1 = :r1", nativeQuery = true)
void deleteByTIDAndR1(#Param("tid") Integer TID, #Param("r1") Integer r1);
And yes this method can delete zero, one or more rows.
i am trying to fetch the id of last column in descending order.
the query which returns last column is
select id from(select id from challan
order by id desc) where ROWNUM=1;
now i am trying to do same thing using hibernate.
public long getIdOnChallanTable() {
session = sessionFactory.openSession();
trans = session.beginTransaction();
Query<Object[]> query = session.createNativeQuery("select id
from(select id from challan order by id desc) where ROWNUM=1;");
Long value = 0L;
List<Object[]> list = query.getResultList();
for ( Object lst : list){
Object[] objects =(Object[]) lst;
value=(Long)(objects[0]);
}
return value;
}
and the error is:
2017-07-26 12:37:36 [http-nio-7080-exec-1] WARN :: SQL Error: 911, SQLState: 22019
2017-07-26 12:37:36 [http-nio-7080-exec-1] ERROR:: ORA-00911: invalid character
update error javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
You don't need the semicolon at the end of the query and please use proper whitespacing. In the FROM clause, you don't have the whitespace between the subquery and the FROM keyword.
Note: don't forget to commit/rollback the transaction at the end and handle the exceptions as well. I hope this was just a sketch to show us the problem and not a code from a real world application.
I have the following Spring Data Query:
#Query(value = "select * from person where person_id =?! and family_id not in (select related_person_id from relationships where related_family_id = ?1)", native query = true)
Person getPerson(String personId);
I am getting the error:
Caused by: java.sql.SQLException: Invalid column name
However, I know that all my column names for the two tables in my query are correct, what could ne causing this?
i don't know the structure of your data but your spring data query has many typos and errors, the standard query method should be:
#Query(value = "select * from person where person_id =?1 and family_id not in (select related_person_id from relationships where related_family_id = ?2)", nativeQuery = true);
Person findByPersonIdAndRelatedFamilyId(String personId, String relatedFamilyId);
also check your inner select query -I don't know the relation between family_id and related_person_id- but it should return a family_id column or an aliased column as family_id may be thats why you're receiving such error ..