SQLSyntaxErrorException - ORA-02287: sequence number not allowed here - java

In my java code, i am trying to perform insert operation but for some reasons i am getting SQLSyntaxErrorException . I am not getting the reason behind it though i tried. Below is my code.
Query nativeQuery = em.createNativeQuery(
"insert into TABLE_DIMENSION (DIMESNION_ID, DIMENSION_CODE, TABLE_CODE, TABLE_IND, CREATE_TIME, "
+ "CREATE_PLACE, CHG_DT, CHG_WARDANT) "
+ " (select TABLE_DIMENSION_SEQ.nextval, d.name,:dimensionCode, 0, :time, :place, :date, :wardant from table_master d where d.name in (:table_dimension_ids)) ");
nativeQuery.setParameter("date", new Date());
nativeQuery.setParameter("table_dimension_ids", tableDimensionList);
nativeQuery.setParameter("dimensionCode", dimensionCode);
nativeQuery.setParameter("wardant", wardant);
number = nativeQuery.executeUpdate();
Please guide.

You told your query that you will use 6 parameters but you set just 4 parameters, you are missing to put :place and :time in the parameters like the others, so you have to use :
nativeQuery.setParameter("place", place);
nativeQuery.setParameter("time", time);
before you execute your query, else you have to remove it from your query

You should skip ():
insert into TABLE_DIMENSION
(DIMESNION_ID, DIMENSION_CODE, TABLE_CODE, TABLE_IND
, CREATE_TIME, CREATE_PLACE, CHG_DT, CHG_WARDANT)
select TABLE_DIMENSION_SEQ.nextval, d.name,:dimensionCode, 0
, :time, :place, :date, :wardant
from table_master d
where d.name in (:table_dimension_ids)

Related

How to CAST twice an element in a row JPA

I've done a query with PostgreSQL that retrieves certain data as I expected: (It works)
SELECT distinct d.data
FROM myScheme.myTable d, myScheme.mySecondTable g, json_array_elements(d.data->>'categorys') e
where d.data->>'categorys' is not null
AND e::text::int in (g.id_product_category_magento)
AND e::text::int IN (21718, 17);
The problem is when I try to do the same query with Java using JPA. This is not worked script:
#Query(
nativeQuery = true,
value = "SELECT DISTINC d.data " +
"FROM myScheme.myTable d, myScheme.mySecondTable g, " +
"json_array_elements(d.data->>'categorys') e " +
"where d.id = :id AND d.data->>'categorys' is not null " +
"AND CAST(CAST(e as text) as Integer) in (g.id_product_category_magento) " +
"AND CAST(CAST(e as text) as Integer) IN (:categories)"
)
List<ServiceDefinition> yest(Set<Long> categories, #Param("id") Long id);
The stack trace throws the following error message:
No dialect for Mapping JDBC 1111
Things to keep in mind:
b.data ->> 'categorys' is an array of Strings that contains string values such as: ['123', '456', '789'] and should return each one of this values.
Pd: I'm not sure if this is the right way to cast a value twice: AND CAST(CAST(e as text) as Integer).
I found the solution to this:
I saved from casting each element twice by using json_array_elements_text instead of json_array_elements, the first one returns the values already as Strings. The second one was producing this error message: No dialect for mapping 1111 due to a bad cast syntax made by me. This thanks to the wonderful suggestion from #AdrianKlaver
Doing select d.data seemed not to work, because it was returning an unknown data type for Hibernate, instead I returned d.idbecause I was really expecting each categoryId as Long type.
Used this CAST(e as int4) instead of this as Integer
Removed an extra #Param (id) of my yest() method
The final query is as shown below:
#Query(
nativeQuery = true,
value = "SELECT distinct d.id FROM myScheme.myTable d, myScheme.mySecondTable g " +
",json_array_elements_text(d.data->>'categorys') e " +
"where d.data->>'categorys' is not null " +
"AND CAST(e as int4) in (g.id_product_category_magento) " +
"AND CAST(e as int4) in (:categories)"
)
List<Long> yest(List<Long> categories);
Hope this helps someone in the future. :)

error: The column index is out of range: 1, number of columns: 0

I'm trying to solve the problem of doing an insert into a Postgresql table
I looked at this similar question but it did not solve my problem
ERROR : The column index is out of range: 1, number of columns: 0
here is the part of code getting the error:
String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES($1,$2,$3,$4)";
PreparedStatement prepareStatement = connection.prepareStatement(query);
prepareStatement.setInt(1, nbStar);
prepareStatement.setString(2, body);
prepareStatement.setString(3, author);
prepareStatement.setInt(4, productId);
boolean executed = prepareStatement.execute();
i tried several times to change the index number but still the same error
and here is the schema of the table:
table schema
can anyone give me an advice ?
thanks.
In the sql query, you want to insert the values for 5 fields (id, nbstar, body, author, product_id) but there are only 4 values VALUES($1,$2,$3,$4).
Update following your edited question, just modify your query as follows:
VALUES($1,$2,$3,$4)
to
VALUES(?,?,?,?)
My problem was that the question mark had single quotes around it and I copy pasted the query from straight sql so I just replaced the string with a ?. For example
Select * from datatable where id = '?'
and I had to change it to
Select * from datatable where id = ?
For me, I had added a comment which included a question mark.
Silly me!
I got that same error because I had the last \n missing in following query, I hope this helps somebody.
" order by mp.id desc\n" +
" limit 1\n" +
" ) as mge_mp\n" +
" )\n" +
"\n" +
"-- #pageable\n",
My last line was
"-- #pageable",
Incase you get this error, for my own issue, I was passing a field ending with a character next to a variable e.g.:
select * from my_schema."my_table" mt
where
mt.field_2 = variable_2
and mt.field_1 = 'variable_1'
[[and cast(mt.date as DATE) = {{date_picked}}]]
-> This failed with an error of:
The column index is out of range: 1, number of columns: 0
Changed to:
select * from my_schema."my_table" mt
where
mt.field_1 = 'variable_1'
and mt.field_2 = variable_2
[[and cast(mt.date as DATE) = {{date_picked}}]]
Please note the variable ending with a quote character has been moved.
-> This worked for me.
For me the issue was having a semicolon at the end of the query string. Something like:
String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES(?,?,?,?);";
Notice the ; appended to the end of the query string. The fix is, well, to remove it:
String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES(?,?,?,?)";

Number of characters in sql query result

I am using Spring 3.1.1 with Hibernate 4 and a MSSQL database. Finally, I have been able to query my database with joins in my table that returns the correct answers. Although, it seems that it does not return the entire strings of my messages, but cuts at 29/30 digits. Here is my query:
SQLQuery sql = this.getCurrentSession().createSQLQuery(
"SELECT event.id as eventid, CAST(event_type.type_name AS varchar) as eventtype, event.check_date, event.event_date, event.status, CAST(event_message.message AS varchar) as eventmessage " +
"FROM event_log event " +
"LEFT JOIN event_type " +
"ON (event.event_type_id = event_type.id) " +
"LEFT JOIN event_message " +
"ON (event.event_message_id = event_message.id) " +
"WHERE event.event_type_id = " + jobId +
"ORDER BY eventid");
The result can be:
4506 Database 2014-01-15 14:14:15.02 2014-01-15 14:14:15.02 false Database-connection cannot be
Where the columns are id, task_name, check_date, event_date, status and the message-string at the end. .
The result goes to a ArrayList<Object[]> where I read row[0] etc. to get the entities in the row. The string message gets cut after 29 digits, which is disturbing. Why does it cut the string and how can I fix this? In my database the string exists in it's full and is entitled 400 characters max.
I know this is probably not the best way to query my database, but it will do for my application since it works.
You are using varchar without a length. Never do this!
Replace:
CAST(event_type.type_name AS varchar)
With something like:
CAST(event_type.type_name AS varchar(255))
In some contexts, the default length is 32. In some it is 1. In general, though, always specify the length.
EDIT:
Actually, you probably don't even need the cast. Why not just have event_type.type_name in the select list?

how to use 'group by' on hql?

I have a native query I need to change to HQL.
The original query is:
SELECT COUNT(DISTINCT `table`.`id`) FROM `database`.`table` WHERE
(`table`.`date`" " BETWEEN '" + year + "-01-01 00:00:00' AND '" + year
+ "-12-31 23:59:59') AND `table`.`box` NOT LIKE '' GROUP BY MONTH(`table`.`date`)";
I tried something like:
StringBuilder hql = new StringBuilder();
hql.append(" select count(distinct table.id)");
hql.append(" from Table table");
hql.append(" where table.date between '?-01-01 00:00:00' and '?-12-31 23:59:59'");
hql.append(" and table.box not like ''");
hql.append("group by month (table.date)");
query.setParameter(1, Integer.toString(year));
query.setParameter(2, Integer.toString(year));
Where year is a int passed to the method as argument.
The generated query is:
Hibernate: select count(distinct table0_.id) as col_0_0_ from table table0_ where (table0_.date between '2013-01-01 00:00:00' and '2013-12-31 23:59:59') and (table0_.box not like '') group by month(table0_.date)
My problem is: using the native query, I get one value and using the hql I get another for month 2 (February). For month 1 (January) results are the same.
What am I missing here?
Thanks in advance,
gtludwig
They seem to be the same query without the schema qualification to me. Aren't you running them in different instance? Like, one in 'database' and the other pointing to anoher base with similar data loaded?

Delete query in mysql

I want to delete rows from multiple tables. I know the select query, but I need an exact delete query for all the columns that are defined below:
SELECT j.application_id,
j.first_name,
j.last_name,
c.description,
l.description,
p.description, " +
"j.email,
j.date_applied,
j.application_status,
j.dob, j.current_city, j.phone_mobile, j.phone_residence " +
"FROM jobapp.job_applications j,
jobapp.countries c,
jobapp.countries l ,
jobapp.job_locations p " +
"WHERE j.preferred_location = p.id
AND j.current_country = l.id
AND j.nationality = c.id "; //+
//"and application_status like ? and first_name like ? and last_name like ? and email like ?";
The query works fine using MySQL, but I need an delete query for the exact columns where the rows are get deleted...
delete statements are structured like:
DELETE FROM table WHERE condition.
you should replace condition with the primary key from you query above (if it is present) and reference each table one at a time.

Categories

Resources