Delete query in mysql - java

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.

Related

How to retrieve data and count of data at same time in hql?

I want to retrieve count and list of data in one query only which I want to write on JPA repository. I wrote it using a constructor and executed using entity manager, but it didn't work. It gave me a QuerySyntaxException. Here is my query:
String hql = "select new core.abc(select count(*) from abc as m where m.Id in :Ids and m.Type = :Type,"
+ "select max(m.modificationTime) from abc as m where m.Id in :Ids and m.Type = :Type )";
How can I write such kind of query in JPA repository?
I just figure out your use case senario is that you want to get all records from table as well as total-count of records and max value of a specific column , you named that column as modificationTime. So onething will be happen in this case, if you want to intract with table with single query, Than you will get useless data for both column named as max and count.
Try This for JPA,
#Query(value="SELECT cb from abc cd where cd.Id in (?1) and cd.Type=?2 , (SELECT MAX(m.modificationTime) as maxModificationTime , COUNT(*) as count FROM abc m where m.Id in (?3) and m.Type=?4) as m",nativeQuery=true)

HQL not working "in" with multiple values

I have hibernate which goes something like this.
String queryString = "from MFunction as mFunction where mFunction.functionKey in "
+ "((select mRole.enterprise from MRole as mRole where mRole.roleKey=:role), "
+ "(select mRole2.project from MRole as mRole2 where mRole2.roleKey=:role ), "
+ "(select mRole3.technology from MRole as mRole3 where mRole3.roleKey=:role ))";
But the hibernate query takes in only the first value.
Hibernate: select mfunction0_.FunctionKey as Function1_73_, mfunction0_.`Add` as Add2_73_, mfunction0_.`Audit` as Audit3_73_, mfunction0_.ClientKey as ClientKey73_, mfunction0_.CreatedBy as CreatedBy73_, mfunction0_.CreatedTs as CreatedTs73_, mfunction0_.`Delete` as Delete7_73_, mfunction0_.`Edit` as Edit8_73_, mfunction0_.`Financial` as Financial9_73_, mfunction0_.FunctionName as Functio10_73_, mfunction0_.`General` as General11_73_, mfunction0_.Level as Level73_, mfunction0_.LevelKey as LevelKey73_, mfunction0_.LogicalDeleteTms as Logical14_73_, mfunction0_.UpdatedBy as UpdatedBy73_, mfunction0_.UpdatedTs as UpdatedTs73_, mfunction0_.`View` as View17_73_ from appanalytixdb.M_Function mfunction0_ where mfunction0_.FunctionKey in (select mrole1_.Enterprise from appanalytixdb.M_Role mrole1_ where mrole1_.RoleKey=?)
Hibernate version: 4.1.8.Final
Your problem is that you created a list of lists, and you should create a single list of functionkeys.
I see only 2 options:
Change one 'in' to 3 statements connected with or.
String queryString = "from MFunction as mFunction where mFunction.functionKey in (select mRole.enterprise from MRole as mRole where mRole.roleKey=:role) or mFunction.functionKey in (select mRole2.project from MRole as mRole2 where mRole2.roleKey=:role ) or mFunction.functionKey in (select mRole3.technology from MRole as mRole3 where mRole3.roleKey=:role )";
Best solution would be to preselect all projects,technologies,enterprise and union them together (to avoid duplicates). But hibernate does not yet support union, according to this post Hibernate Union alternatives.
If you have not so many entries in your tables, I would create a view, and used my query on it. Views can be quite cheap and easy to use in this scenarios.

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?

JPA avoiding multiple subquery

I need a little help setting up my query. I don't want to have to make multiple selects to form basically the same sub-query if I can avoid it. In a nut shell, I have Objects called TimeSlot that are used to track several details. Those TimeSlot's are items that are paid on. When its time for the TimeSlot to submit for a reimbursement they are used to create a PayableTimeSlot. Before the TimeSlot can be paid I need to make sure it has not been paid already.
As it sits the following is my query:
#NamedQuery(
name = "TimeSlot.by.person.academy.id.by.contract.date",
query = "select distinct ts
from TimeSlot ts
join ts.invitedInstructors ii
join ts.academyClass ac
join ac.academy a
where ii.person.id = ?
and a.id = ?
and ts.schedule.startDateTime BETWEEN ? AND ?
and ts.id not in (select e.id from PayableTimeslot pts join pts.event e)
and ? not in (select e.claimant from PayableTimeslot pts join pts.event e)")
As you can see I am already selecting an element from the PayableTimeSot for the first not in. Is there a way to expand the sub-query into:
(select e.id, e.claimant from PayableTimeslot pts join pts.event e) I am just not sure how to check for multiple items not in the sub-query. By all means if there is a better attack of the problem than the way I am doing it let me know.
Unless you all think the multiple selects wont be a big deal... There will be on average 30-50 entry's a week into the table with each entry being copied (for an audit trail) upwards of 7-9 times.
Okay so after some thinking this is what I came up with. I was indeed trying to answer the problem in the wrong way... I was doing two sub querys when all I needed was a where on the first thus combining the two.
#NamedQuery(
name = "TimeSlot.by.person.academy.id.by.contract.date",
query = "select distinct ts "
+ "from TimeSlot ts "
+ "join ts.invitedInstructors ii "
+ "join ts.academyClass ac "
+ "join ac.academy a "
+ "where ii.person.id = ? "
+ "and a.id = ? "
+ "and ts.schedule.startDateTime BETWEEN ? AND ? "
+ "and ts.id not in (select e.id from PayableTimeslot pts join pts.event e where pts.claimant = ?)")

sql query to retrieve sum of contact records that match to one user with mysql and java

I have two table user and contact, each user can have multiple contacts
I want to draw a graph (camembert) with JFreeChart(java library) to present the number of contacts that has each user
I tested :
select u.name, sum(c) from user u, contact c
but I have
unknown column c in field list
here are the structures of tables :
utilisateur = idutilisateur, identifiant, motdepasse, nom, prenom
contact = idcontact, ............, idutilisateur
How can I achieve that,
and also, please give me links to tutorials for learning to make queries like that because always I have this problem and I find only basic tutorials
thank you in advance
You need to reference the column of the table that you're trying to count, not just the table itself.
if you try:
select u.name, count(c.ID)
from user u, contact c
You can count all records that have a value in the ID field. If this is a primary key, then all records should have this field. Note the change from sum to count.
This won't fully solve your problem. You also have to let sql know that the two tables are related by joining:
JOIN contact
ON user.ID=contact.userID
Put this after the from user u instead of , contact c
For a good start to learning sql, try here: http://www.w3schools.com/sql/default.asp
String sql1 = "SELECT u.nom, COUNT(c.idcontact) ContactsCount FROM utilisateur u LEFT " + "JOIN contact c ON u.idutilisateur = c.idutilisateur " + "GROUP BY u.nom";
sql = "SELECT u.nom + ' ' + u.prenom dd, COUNT(c.idcontact) FROM utilisateur u " + "LEFT OUTER JOIN contact c ON u.idutilisateur = c.idutilisateur GROUP BY u.nom + ' ' + u.prenom";

Categories

Resources