I need one help.
I have a table - ACCOUNT which stores USER_ID and ACCOUNT_DETAIL. There are multiple records for a single USER_ID with different ACCOUNT_DETAIL.
I want to store this records in a Map where USER_ID will be the key and List of Account will be the value. I get the list of USER_ID
from USER table. Then for each of those user id I need to get account details.
I can populate that map by implementing below algorithm\logic :
Step 1 : Fetch userIds from USER table and store it in a list - userList
Step 2:
for (User user : userList) {
// Make a DB call to ACCOUNT table for user.id
// Put those details in the map - map.put(user.id, accountList)
}
But here the issue is, if USER table is having 1K records I have to make 1K db calls from that loop which will end up with a performance issue.
Could you please tell me if there is any better approach through which I can achieve this using hibernate ?
Use the HQL join query(ACCOUNT and USER) to get all the accounts and construct the map by iterating the accounts.
select acc from Account acc, User usr where acc.userId = usr.id
Related
I'm trying to build an application which uses a database, that allows user to log in as an admin or employee.
Admin has few options:
add new employee,
list all employees and
add TODO Items for selected employee
Employee
display TODO items when logged in.
The first problem I've encountered is:
Should I create just one table for all records(employees & admins) and specify their name, username, password, status (admin or employee) or create separate tables for login credentials(username, password) and user data(like name, last name etc) to keep it separated?
Second problem is
I can't figure out how to display list of items for a user that has successfully logged in. How to access the data of the user that is logged in?
The first problem I've encountered is:
Should I
1=> create just one table for all records(employees & admins) and specify their name, username, password, status (admin or employee) or
2=> create separate tables for login credentials(username, password) and user data(like name, last name etc) to keep it separated?
Its better to make all user data in one data-set (Table)
No need to separate login credentials in a separate table in your case.
Second problem is I can't figure out how to display list of items for
a user that has successfully logged in.
How to access the data of the user that is logged in?
Something like that
Each Item in ITEM table should has a field called say USER_ID
And ONLY managers can update and set this field value
Then your query will be
SELECT *
FROM ITEM
WHERE USER_ID = 'logged_user_id'
One approach will be to
Create a profile or user table for thr user (employee or admin) and have userid(primary key), userloginname,userfirstname,userlastname,password(encrypted),email,role(ADMIN for admin or EMPLOYEE for employees),hintquestion etc.,.
2.Then create a table for todo tasks with columns like taskid,taskname,taskdescription etc., along with userid column(foriegn key) which will link this table to the primary key of profile table. (This answers your question on how to validate and fetch employee record).After login store user or profile object in session for the duration of the session).
3.When the user logs in validate the credentials against the username(the one user uses for login) and password stored in the profile table and fetch that record.
4.When adding employer, add the user to the profile table.
5.When adding todo tasks, add the todo items in the todo table using the userid fetched from the profile table.
6.When listing the todo items, fetch the record for thr logged in user and using the userid, fetch the todo items for the userid. (This answers your question on how to fetch the todo items)
7.When deleting the employee, delete the record from profile table and using the userid delete thr todo items from the todo items table.
Hope this helps.
I have two tables Companies and Users, where each company has many users, and I want to do a query on the users table where the column IS_VERIFED equals false. In my User entitie I set the XMLTransient property on the getCompany method to avoid an infinite loop of the user getting the company and the company getting the user.
I preform my query to get all the users that are not verifed, which return a List<User>. But I also want to return the company that each User belong to.
I can loop through the users array like this
List<User> users = entityManager.createNamedQuery("User.getPendingUsers").getResultList();
for (User user : users) {
logger.debug(user.getCompany().getCompanyId() + "");
logger.debug(user.getCompany().getCompanyName() + "");
}
And I see the company ID and name printed out, but now I want to return that list of users with the companies referenced how can I do this?
Thanks
I have a Spring new web application(Java), and after having the user login (using username and password), I want to make the user able to add a new user, till then all is good. Each user have a primary Key iduser which autoincrimented (AI), and a foreign key idprofile. The table Profile contain informations about the user, and it's idprofile is the primary key (which have to be the same as the ipdrofile in User table).
As you may know, MySQL don't let you make more than one AI per table (MySQL 5 workbench as platform), and when my code add's an user, it has to add a profile in the same time, but with three fields that are unknown, which are iduser, idprofile (user table) and idprofile (profile table). As the ipdrofile can't be an AI, how can I manage to put the right key on it when saving the User and Profile (Adding) ?
You insert the profile first, returning the new profile id, then you insert the user, using the returned profile id, optionally returning the new user id if you need it.
You'll need to use these two methods to get the auto-generated key returned:
Connection.prepareStatement(String, String[])
Statement.getGeneratedKeys()
Lets say I have a table like :
CREATE TABLE USER (
userid ascii,
books set<text>
PRIMARY KEY (userid)
);
and index :
create index on USER (books);
I want to query on books by using sql context. What I am doing is :
CassandraSQLContext cassandraContext = new CassandraSQLContext(sparkContext);
SchemaRDD userTable = cassandraContext.sql("SELECT * FROM keyspace.user");
userTable.registerTempTable("usertable");
Following query does not work for Cassandra :
SchemaRDD userTable = cassandraContext.sql("SELECT * FROM keyspace.user where books CONTAINS 'book1' and books CONTAINS 'book2'");
It returns users with only 'book1'. I've tried similar queries like books CONTAINS ('book1', 'book2') but none of them worked.
What I can do on registered table is :
SchemaRDD users = cassandraContext.sql("SELECT * FROM usertable where userid='some_user_id'");
What I want to do is query by books like :
SchemaRDD users = cassandraContext.sql("SELECT * FROM usertable where books IN ('book1', 'book2')");
or similar queries.
but it doesnt work. It returns 0 records. I tried to register index table named as user_books_idx but it didnt work either. Am I able to query on indexed collections? How can i do it?
It seems the CONTAINS clause only works with one value per statement, so you may not be able to 'and' two different CONTAINS clauses together at one shot.
So I would create one RDD using CONTAINS 'book1', then I'd create another RDD using CONTAINS 'book2', and then I'd do a join of the two RDD's on the userid field. That should give you a resulting RDD of the users with both book1 and book2 in their books set.
my question is how to insert values into DB in sql. i have a USER who can have multiple emails. how can i insert multiple emails into one object? i dont want to add a completely new user object into a new row. i just want to update and append new email into email field of an existing user in db.
i did this:
JPA.em().createQuery
("insert into User (email) select (email) from User where USERNAME=? VALUES (?)")
.setParameter(1, username).setParameter(2, email).executeUpdate();
but it is not working, thanks for help !!
Get the user from the database, concatenate its existing email with the new value, and save the user.
JPA uses entities and generates SQL queries for you. You tyically use queries only to get entities from the database. And those queries are JPQL queries, not SQL queries.
And it looks like your schema is not normalized correctly. One User entity should have many Email entities (OneToMany associations), rather than stuffing all the emails in a single CLOB field of the user. This is how you could search and get individual emails from the database, and get users without all their emails if you don't need them.
I don't know JPA but query should be
JPA.em().createQuery
("insert into User (email) select (email) from User where USERNAME=? VALUES (?)")
.setParameter(1, username).setParameter(2, email).executeUpdate();
should be
JPA.em().createQuery("UPDATE User
SET email =?
WHERE USERNAME=? ")
.setParameter(1,email )
.setParameter(2,username)
.executeUpdate();