Oracle query Java EE, Entity Manager - oracle

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.

Related

Querying a ManytoOne Relationship using #Query in Spring Data

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

Creating a linq query which results in only 1 query execution

I have the following two tables:
*tableA
*tableB
TableA has a 1 to many relationship with tableB and therefore TableB has a column with a foreign key for tableA
TableA will be used to create a c# class called classA which has a bool property indicating wether it has related records in TableB called HasRecords
I could create it like this:
TableA.Select(s => new ClassA {
Hasrecords = s.TableB.Any()
}
Will this create 1 query or multiple ones because of the any function? Is there another way to do this in line without triggering multiple query executions or do I have to resort to a table valued function?

Partial Entity Binding for Android Room

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();

Inserting data from table (SELECT) with REF - Oracle Database

I'm trying to insert all data from Pacient table to Pacient_OR table (Object-Relational). Is there a simple way to do that (one script), if Pacient table has column with Pojistovna_ID (foreign key) and in Pacient_OR table there is REF to the Pojistovna_OR. Both Pojistovna and Pojistovna_OR are populated with the same data, but one is relational, second is based on object type.
I tried this (and more ofc):
INSERT INTO pacient_or
(pacient_or.id,
pacient_or.jmeno,
pacient_or.prijmeni,
pacient_or.datum_narozeni,
pacient_or.rodne_cislo,
pacient_or.telefon,
pacient_or.krevni_skupina,
pacient_or.rodinna_anamneza,
pacient_or.adresa,
pacient_or.pojistovna)
SELECT pacient.id,
pacient.jmeno,
pacient.prijmeni,
pacient.datum_narozeni,
pacient.rodne_cislo,
pacient.telefon,
pacient.krevni_skupina,
pacient.rodinna_anamneza,
Adresa_typ(pacient.ulice, pacient.mesto, pacient.psc),
(SELECT Ref(poj)
FROM pacient pac,
pojistovna_or poj
WHERE pac.pojistovna_id = poj.id)
FROM pacient;
This code throws error:
single-row subquery returns more than one row
Do not use pacient pac in the subquery. Link it to the pacient in your main from-clause. And even better do not use a subquery for this.

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