I have Employees table which has many-to-one relationship to Company table ( similar to my situation). I am currently using Spring Data Repositories.
Now I am a bit confused on how to get employees matching join-date and Employer Id . As it is a many to one relation, Employees Entity has a Employer Entity with the mapping.
I thought of using #Query Annotation with a query like below:
#Query(Select emp from Employee emp where emp.joindate= ?1 and emp.employer.id =?2)
Employees methodname ( arg1, arg2)
Any Corrections or Suggestions ? . I don’t see good documentation on #Query. Please post if u have gone thru one
Related
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);
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.
Would like to confirm is it possible to bind an entity bean to partial columns of a table?
Example:
Table "A" has column id, col1, col2, col3, col4, col5, ... , col10
But I only need id, col1, col2, so I create a entity bean with fields id, col1, col2 and do binding for these fields only? I tried doing this but got:
Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
Appreciate if anyone can verify is it possible to do the above binding using Room Persistence Library.
(Note: Why are there columns in my table which are not used in the mobile app. These tables schema are an exact copy of some tables in server side, hence some fields are used at server side webapp)
Returning subsets of columns
Most of the time, you need to get only a
few fields of an entity. For example, your UI might display just a
user's first name and last name, rather than every detail about the
user. By fetching only the columns that appear in your app's UI, you
save valuable resources, and your query completes more quickly.
https://developer.android.com/topic/libraries/architecture/room.html
You can define a model which does not have an #Entity annotation and use it at your select query.
// No #Entity annotation here !!!
public class NameTuple {
#ColumnInfo(name="first_name")
public String firstName;
#ColumnInfo(name="last_name")
public String lastName;
}
It isnt necessary to write another DAO (as the new class isnt an entity) - you can use the new Class as the return datatype from within any existing DAO.
// inside any existing DAO
#Query("SELECT first_name, last_name FROM user")
public List<NameTuple> loadFullName();
If I have an Oracle database, with structure, SCHEMA/TABLE1 SCHEMA/TABLE2 and I want to create a query to select all rows from Table1, would it be,
"Select x from SCHEMA.TABLE1 x"
If you have an entity such as:
#Entity
#Table(schema="SCHEMA", name="TABLE1")
public class Table1Class {
...
}
Then, the following basic JPQL Query would select all the Table1Class entities:
Select x from Table1Class x.
Summing up, you don't need to (not that you can, either) specify schema and table name on JPQL queries, just the class name the table is mapped to.
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");