I have a simple Spring MVC Data project setup where I'm trying to select a list of users that don't already exist within an Admin table. Here's my repository method
SELECT u FROM User u WHERE u.id NOT IN (SELECT a.id FROM Admin a WHERE a.id = :id)
List<User>findAvailableUsers(#Param("id") Long id)
This works well, the more users I add to the Admin Table, the less I have available in my <select> tab
When it comes to editing an existing user in the Admin table, my <select> tag should contain all available Users to add as well as the currently mapped User, however because of the NOT IN clause this single user is omitted from the query results. Can I modify this query to include all available Users to add including the User that currently exists tag?
example: User 2 and User 4 have been added to the Admin table. When I edit the record for User 2 in that Admin table, I should still see User 2 as an available option, but it is omitted
Something like this?
SELECT u FROM User u WHERE u.id NOT IN (SELECT a.id FROM Admin a WHERE a.id = :id) OR u.id = :idToGet
Related
I have two oracle table:users and logs,as follow,
users:
---------
id number,
name varchar2(50)
logs:
-----
user_id number,
visit_date date
I need to find out the first 10 users who never appears in the logs table (means they never visit the page),I can write a sql:
select *
from users u
where not exists (
select 1 from logs o
where o.user_id = u.id)
and rownum<=10
But now the problem is these two table are not in the same schema,they could not be accessed for each other,so I can not join them.
Because there are two microservices,one microservice should query its own schema
If I fetch 10 records from users,maybe they all never visit the page,I need to fetch 10 more,then after that still 5 users did not visit that page,I need to fetch 5 more,and so on.So I am not sure how many record I should fetch at the first time.Is it a good idea to do so?And how to write the code?
If I fetch all the record of the 2 tables,then do join using Java code,I'm worried about the amount of data being too large.Or should I just make these two tables accessible to each other?
So,how to do paging with 2 table but not join them?
But now the problem is these two table are not in the same schema,they could not be accessed for each other,so I can not join them.
Create a user that can see both schemas and log in to that user and access the tables using schema_name.table_name:
SELECT *
FROM schema1.users u
WHERE NOT EXISTS (
SELECT 1
FROM schema2.logs l
WHERE l.user_id = u.id
)
ORDER BY u.something
FETCH FIRST 10 ROWS ONLY
or
SELECT *
FROM (
SELECT *
FROM schema1.users u
WHERE NOT EXISTS (
SELECT 1
FROM schema2.logs l
WHERE l.user_id = u.id
)
ORDER BY u.something
)
WHERE ROWNUM <= 10
Alternatively, create a third schema and GRANT the SELECT privilege on the USERS and LOGS tables in the first two schemas and then create a view in that third schema and a new micro-service and use that.
I have three tables here.The user is able to apply the training available.when he/she selects the training then it is placed at TRAINUSER table according to his/her userID. I want to SELECT the user when he/she logs in and to show data from TRAINUSER table which he/she has not applied the training. Like user_id 1 has applied 2 and 3 training but has not applied 1 and 4 training. I want user_id 1 to show with training 1 and 4 using hibernate query.
Some of this will depend on what your domain classes look like, you could use executeQuery like so:
Training.executeQuery( "from Training tr where tr.id not in ( select t.id from TrainUser tu join tu.training t join tu.user u where u.username = :uname )", [uname: 'ADMIN'] )
Assuming the following domains, only relevant fields included:
class User {
String username
}
class TrainUser {
static hasMany = [training: Training, user: User]
}
class Training {
String name
}
Hi I'm trying to select records from one table which doesn't have records in connected many-to-many table with specific values.
I will explain on sample tables:
documentation:
id_documentation
irrelevant_data
user:
id_user
irrelevant_data
documentation_user:
id_documentation
id_user
role
What I want to achieve is to select every single documentation which doesn't have user in specific role. Any ideas?
The main problem is that I'm using java's CriteriaBuilder to create query so using subqueries is impossible (I think).
You can add restrictions on your left join using: createAlias(java.lang.String, java.lang.String, int, org.hibernate.criterion.Criterion) method, see API.
Check this answer for an example on how to use the left join with a criteria.
Main problem does not exist - Criteria API do have SubQuery. Query itself selects instances of User and uses not in construct to limit results based to subquery. Subquery selects all users that are connected to document with specific role via DocumentationUser.
Try something like this (code not tested):
CriteriaQuery<Documentation> cq = cb.createQuery(Documentation.class);
Root<Documentation> u = cq.from(Documentation.class);
Subquery<Integer> sq = cq.subquery(Integer.class);
Root<User> su = sq.from(User.class);
sq.select(su.get("id_user"));
Join<User, DocumentationUser> du = su.join("documentationUserCollection");
sq.where(cb.equals(du.get("role"), "mySpecificRole"));
cq.where(cb.not(cb.in(u.get("id_user")).value(sq)));
See also this useful answer on SO.
I hava a java app, with 2 objects: User.java and Review.java.
Each User can have many Reviews.
Review object has a User object on it (eg: review.getUser())
I need an hql query that will get all Users that have no Reviews. How do I do this?
Try
from User u where u.reviews is empty
assuming your User class has a collection of reviews, of course...
from User u where not exists (from Review r where r.user = u)
I do not know your tables and the columns in these tables, but anyway you should have a query like the following:
select *
from User
where not exists (select Review where Review.userId = User.id )
I'm using the Java spring framework for security. My pre-existing table layout differs from spring's expected, but I'm allowed to specify a custom authorities-by-username query on the jdbc-user-service. The problem is that this query expects only a single parameter (?) in the SQL statement. Basically, if the user exists in the users table at all, they should get 'ROLE_USER'. If they exist in the auth table as 'S', they should get 'ROLE_USER', 'ROLE_LICENSEE' and 'ROLE_SYSADMIN'. If they exist in the auth table as 'L', they should get both 'ROLE_LICENSEE', and 'ROLE_USER'.
SELECT U.email AS 'username', 'ROLE_USER' AS 'authority' FROM users U WHERE U.email=**?**
UNION
SELECT U.email AS 'username', 'ROLE_LICENSEE' AS 'authority'
FROM users U, auth_sys A
WHERE U.user_id=A.user_id AND A.auth_type IN ('L', 'S') AND U.email=**?**
UNION
SELECT U.email AS 'username', 'ROLE_ADMIN' AS 'authority'
FROM users U, auth_sys A
WHERE U.user_id=A.user_id AND A.auth_type='S' AND U.email=**?**;
My question is how can I reduce this from 3 (?)'s down to 1?
Since you are already using Spring Framework, you can use the Spring Framework to make your JDBC calls as well. Spring Framework has a NamedParameterJdbcTemplate that allows you to name your JDBC parameters. So, you can create a named parameter called :emailAddress for example, and use the same parameter three times in the SQL, but pass it in only once into the template.
It sounds like you won't be able to do that; since it sounds like you want a separate row for each user if they're in multiple roles.
If you can get by with only one row returned per user; and can assume that any Admin is also a licensee (evident by your where clause), you should be able to use a case statement and a left join.
Something like:
SELECT u.Email,
CASE WHEN a.user_id is null then 'Role_User'
WHEN a.auth_type = 'S' then 'Role_Admin'
WHEN a.auth_type = 'L' then 'Role_Licensee' end as 'authority'
FROM users u LEFT JOIN auth_sys a on u.user_id = a.user_id
WHERE u.email = **?**
Granted; i'm not completely familiar with the table structure, so it might require a little tweaking; but that should be enough to get you started
As far as I know is not possible. You have to use three query with union.