Hibernate - How to get single value from another table without primary/Foreign key relationship? - spring

I have 2 tables(Employee & Department).
Employee(ID, employee name, dept id, employee address) --> ID primary key
Department(ID, department name, address) --> ID primary key
Note: Consider no primary key / foreign key relationship between these 2 tables
Now based on dept id in Employee, i need to fetch its corresponding dept name from Department & display it in Employee.
Employee(ID, employee name, dept id(Display dept name instead of dept id, query department table to get dept name through passing dept id), employee address)
What are all possible ways to achieve this?
Regards
Raj

I see at least three possible solutions:
Create a "normal" SQL query for this, joins can be perfectly done without a PK/FK-relationship.
Perform two SQL queries: One query for fetching the employee, then extract the ID and then a second query to fetch the department by ID.
Change your JPA entities to include the relationship as you would normally do having a #OneToMany relationship so you can load the employee with its department as usual. Same as 1 applies, joins (whether done by you or by the JPA provider) don't need a PK/FK-relationship.
Whilst 1 can be done with plain SQL, 2 can be done using the entity manager without the need to write "plain" SQL. 3 would be the easiest solution, obviously.

You can #Query in repository and write your JPA query using JOIN.
Sample Query:
#Query( value = "select new map(employee.name as empName, employee.id as id, department.id as depId, department.name as depName) from Employee employee INNER JOIN Department department ON employee.depId = department.id where employee.id = :id" )
public Map<String,Object> getEmployeeById(long id);

Related

NIFI - How to insert distinct data from the flow and refer to that data ID in other places

I'm trying to learn NIFI so this is all new to me, I used to work with Talend and I have hard time translating to NIFI. So the main idea: For example is I have two tables in Postgresql
Table CITY :
ID (auto generated), city_name
Table PERSON :
ID (auto generated), first_name, last_name, city_id
and I have a CSV file :
first_name, last_name, city_name
Can you please explain how I can insert in tow tables from one flowfile and refer in the table PERSON to the ID of the city not the name from the table CITY.
Thank you
you could use LookupRecord to enrich each record with city id and split input in two files: matched/unmatched.
for matched you have to execute simple insert into PERSON table - because city id was found.
for unmatched you have to generate insert/upsert into CITY table and then route all those records to lookup record again.
or you could insert everything as is into temp table with structure that matches your CSV.
and then execute 2 simple sql statements:
populate missing cities from temp table
insert into person from temp table with lookup city id on the level of SQL server

GraphQL Mutation Insert using Inner Query

How can I perform the following SQL Insert Query as a GraphQL Mutation Insert?
INSERT INTO User (id, name, user_type_id)
VALUES
(1, "Name", (SELECT id FROM UserType WHERE user_type="Guest"))
Provided that there is a One to Many relation from UserType table (id column) to User table (user_type_id column).
You should checkout an ORM that work with your technology.
like 'sequelize' for example.

Oracle query I have to fetch the records based on department(admin, user, emp) and respective role(1,2,3,4,5) from employee table

There are two tables 1. employess (column emp_name, emp_dept, emp_address ) 2. department (dept, role)
I have to fetch the records from employee table
based on department(admin, user, emp) and respective role(1,2,3,4,5) from employee and department table.
If employee is admin then records with role 1 and 3 should be fetched if user then only records with role 5.
Please help me to write the query.
Following is the query which I tried:
select emp_name, emp_dept,
(select role from department d where d.dept= e.emp_dept) role, emp_address
from employee e
where role IN (case emp_dept
when 'admin'
then('1','3')
when 'user'
then ('5')
when 'emp'
then('4')
end
)
Could be good to start with a Users table , where you will store all users independently if they are admin or employee . After that try to write a draft of E/R model , to make us understand what you need .

Joining 3 tables in Oracle

I need to join three tables in Oracle; I have code but I am not sure if it is totally right.
FROM DISTRICT D1
JOIN EMPLOYEE E1 ON D1.DISTRICT_ID = E1.DISTRICT_ID
JOIN TOTAL_PAB T1 ON E1.EMP_ID = T1.EMP_ID
I need to join the table DISTRICT, EMPLOYEE, AND TOTAL_PAB.
Where do the primary and foreign keys go in this join table statement?
Primary key for EMPLOYEE is EMP_ID and FK is DISTRICT_ID.
Primary key for DISTRICT is DISTRICT_ID and FK is SUPERINTENDENT_ID.
Primary key for TOTAL_PAB is PAB_ID and FK is EMP_ID.
The query seems legit, but you should give us some more informations for a better answer.
Generally, it's not important if a field is key or not: the important thing is that ONLY fields in "ON" part of the join statement will be used for matching rows.

how to write parameter string for include for linq query?

If table Employee has foreign key for table person, department,
when get the entity of employee, I want to both associated entities loaded too.
how to write the linq query? like below?
var employee = this.Context.Employee.Include("Person, Department");
It seems not working.
var employee = this.Context.Employee.Include("Person").Include("Department");

Resources