Joing entities in spring boot with saving output in a hashmap - java

so i wanted to join to entites from database MYSQL ( Client and Facture) in english (User and invoice), the idea is to create statistics , how many Facture (Invoice) are in a Categorie_client From Client(user)
I created the SQL request but i always get Bad SQL syntax
So here are the screenshots of all what we need :
Facture entity
Client entity
In between CLient and facture entity
The sql syntax
The map implementation
the error i get :
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.example.demo.utils.ClientFacture(F.ID_FACTURE, C.ID_CLIENT, COUNT(*)) FROM F...' at line 1
If i turn the nativeQuery = False or i remove it i get A error about #Bean implementation.
Hibernate version : 5.4.32
Every help is welcome it's for Uni project
Thanks in advance.

Related

"Invalid Column Name" thrown In Java Spring Batch, but not in Oracle Sql Developer

Using Java Spring Batch, I am generating a file based off of a query sent to a Oracle database:
SELECT
REPLACE(CLIENT.FirstName, chr(13), ' ')
FROM client_table CLIENT;
This query works fine when I run it in Oracle SQL Developer when I spool the result, but doesn't work when I try to utilize it to generate a file in Java Spring batch. It throws the error:
Message=Encountered an error executing step preparePrimaryIpData in job extract-primary-ip-job
org.springframework.jdbc.BadSqlGrammarException: Attempt to process next row failed; bad SQL grammar [
SELECT
REPLACE(CLIENT.FirstName, chr(13), ' ')
FROM client_table CLIENT
]; nested exception is java.sql.SQLException: Invalid column name
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:237)
Why is this working fine in Oracle Sql Developer but not when I try to utilize it in Java Spring Batch?
Also, the alias is necessary, because my actual query has a lot more joins, I just wanted to simplify it as an example.
you need to check the spelling of columns in your query as well as where you will be getting the result set. I was facing the same problem and the problem was in spelling in one of the "rs.getString("COLUMN_NAME")".
It is not the sql that has failed but you are misspelling the column name somewhere in your code.
Try some thing like :
SELECT REPLACE(CLIENT.FirstName, chr(13), ' ') columnNameX FROM client_table CLIENT

Why is Hibernate generating this SQL query?

I'm using Hibernate and a MySql server. I use multiple databases as "namespaces" (e.g. users, transactions, logging etc.).
So, I configued Hibernate to NOT connect to a particular database :
url = jdbc:mysql://127.0.0.1/
The databases where tables are located are defined in the hbm files through the catalog attribute :
<class name="com.myApp.entities.User" table="user" schema="" catalog="users"> ...
When I want to load some data, everything works fine and Hibernate seems to generate the expected SQL queries (by using the catalog prefix in the table names) e.g. :
select id from users.user
However, when I try to add a new record, Hibernate don't use the from [catalog].[table_name] syntax anymore. So I get a MySQL error 'No database selected'.
select max(id) from user
Hibernate is trying the get the future id to create a new record, but it doesn't specify in which database is located the table, it should be :
select max(id) from users.user
Why is Hibernate generating this invalid query ? Have someone ever experienced this problem ?
You need to specify the schema for the generator. See this question on SO for a more detailed answer.

Hibernate multiple native SQL statements

I want to run a native SQL from a file using Hibernate. The SQL can contain several statements creating the database structure (i.e. tables, constraints but no insert/update/delete statements).
Example, very simple query is below (which contains the following two SQL statements)
CREATE DATABASE test;
CREATE TABLE test.testtbl( id int(5));
I am using MySQL db, and when I run the above query I am gettng syntax error returned. When I run them one by one, its ok.
Caused by: 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
'CREATE TABLE test.testtbl( id int(5))' at line 1
The code to run the query is below (above statement is assigned to 'sql' variable):
session = sf.openSession();
session.beginTransaction();
Query qry = session.createSQLQuery(sql);
qry.executeUpdate();
session.getTransaction().commit();
Any help would be appreciated.
As others have explained
You must run these queries one by one.
The hibernate code gets translated into running one update statement on JDBC.
But you provided two update statements.
In addition,
I personally prefer to have the code that creates tables outside of the Java application, in some DB scripts.
The parameters of the method createSQLQuery is t-sql code;
t-sql code to ensure that in the mysql interface analyzer correctly.
You can try changed the sql :'CREATE TABLE testtbl(id int(5));'
by the way you can use JDBC Connection api (Don't recommend to do so)
Such as:
java.sql.Connection conn=session.connection();

Pagination is not working when calling MySQL Stored Procedure using Hibernate

I have a MySQL Stored Procedure click here for stored procedure and calling the Procedure using Hibernate
Hibernate Code:
int ps=5;
SQLQuery query=session2.createSQLQuery("CALL AbsentReportproc(:_fromdate,:_todate)");
query.setParameter("_fromdate", fromdate);
query.setParameter("_todate", todate);
query.setFirstResult(ps*(pno-1));
query.setMaxResults(ps);
List<Object[]> empList=query.list();
when I execute above code I'm displayed with the following Error Message:
org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: 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 'limit 5' at line 1
Note:If i remove the below statement from the code,I'm displayed with all records in a single jsp Page
query.setMaxResults(ps);
Could any one give me the solution what was the Problem?
thanks...
It is clearly a jdbc driver error that you are using;
Also try this with setFetchSize , and if this doesn't work also then,
I would suggest not to get the details out of the query in chunks if you want to display them all and there is not much data and you should rather store all the data without calling this method query.setMaxResults(ps); into a collection.
And when you want to display that data in pages, then get subList in case you are using List, to break the data at the application level and then display it on your view that is jsp in this case.

Db2 connection problem with java

I am having problem with DB2. I just installed the db2 as a db2admin and with a password. When i try to connect to database it is success full and while running any simple select query it give me following error:-
DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.LOGIN, DRIVER=3.57.82
I have a database named onp and a table in it called 'login' in which there is one table called 'login' with two fields username and password.
Query that i am running
Select * from login; gives me error
DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.LOGIN, DRIVER=3.57.82
Select * from system.login; gives me error:- (//system is schema name)
DB2 SQL Error: SQLCODE=-551, SQLSTATE=42501, SQLERRMC=DB2ADMIN;SELECT;SYSTEM.LOGIN, DRIVER=3.57.82
I have tried all the resources on the net and exhausted completely. Please help me
I don't know a lot about DB2, but looking up the error codes...
The first error is because you didn't specify a schema, so it couldn't find the login table.
SQLCODE -204 Object not defined to DB2
DB2 apparently requires you to specify the schema name or it looks in the schema with the same name as your login user.
You must use SET SCHEMA or fully qualify the table name.
The second error is because you don't have the privileges to perform that select:
SQLCODE -551, Error: DOES NOT HAVE
THE PRIVILEGE TO PERFORM OPERATION ON
OBJECT
I'm not sure why the db2admin user wouldn't be able to select from this table...
Resources:
List of DB2 SQLCODEs
SQL CODE 551 occurred because the connecting user does not have privileges to perform operations.
Go to Control Center - Go to User Group and Object and select DB2ADMIN(assume this user is the one use to connect to DB2)
Check all the check box as the following
Grant Schema access to the user
Grant Tables access to the user
I had the same problem and i resolved it by adding Schema in my entity :
#Entity
#Table(name="MyTable", schema="MySchemaName")
public class MyClass implements Serializable {
...
}
You can also resolve the issue as:
Just give the proper authority to the user by which you are connection to DB2.

Categories