jpa java JPQL get the list of entities Type - jpa

I would like to retrieve the list of requested entities from a JPQL request
and if I detect a specific one add a filtering criteria
sample
String jpql = "select u.fname, u.lname from **Users** u JOIN u.group g where g.name = :groupName";
here the request will work on entity "Users" and entity "Group"
so I would like to retrieve the list of entityType (Users / Group) or Type User.class, UserGroup.class
and to do a specific filtering in case I found a sensitive table
(not all Group are public and sometimes the personnel that create the query forget to apply the filtering)

Related

Select specific columns from table with JPA in java

I want to write a Query for getting specific columns.using
entitymanager.createQuery("SELECT u.name FROM Department u").getResultList();
however it returns object is not an instance of declaring class.
What is the correct way to get specific column from table in jpa.as a provider i am using Hibernate
It returns list of objects (it would be hard for JPA to guess what types are you returning). But luckily you can give it a hint with TypedQuery like this
TypedQuery<String> query = em.createQuery("SELECT u.name FROM Department u",
String.class);
List<String> departmentNames = query.getResultList();

Laravel Eloquent Query Builder (with JOIN)

Tables
VENDOR
id name approved
PRODUCT
id name price instock fky_prod_vendor_id
Relationship
VENDOR(hasMany products()) <- (one-to-many) -> (hasOne vendor())PRODUCT
Query
How can I get all the products in-stock of a approved vendor using Eloquent given that the relationships are defined in Model?
My SQL is as following, but I need to use Eloquent relationship to achive the following.
select product.id
from product, vendor
where product.fky_prod_vendor_id = vendor.id
and vendor.approved = 'y'
and product.instock > 0
Thanks
K
As relationships are present, we can achieve this using Querying Relationship Existence method whereHas as rightly pointed out by #svrnm
PRODUCT::where('instock','>',0)
->whereHas('vendor', function ($query) { // Using Eloquent Query Existence, first parameter is name of relationship method, inside function is where clause on related model
$query->where('approved','y');
})->get();
That's magic of Laravel
Thanks
K

How i can achieve relational data using Hibernate Search?

I am using JPARepository, I chose Hibernate Search for implementing search functionality.
Here is link : http://hibernate.org/search/documentation/getting-started/
Using an EntityManager (JPA) to rebuild an index
FullTextEntityManager ftem = Search.getFullTextEntityManager(entityManager);
ftem.createIndexer().startAndWait();
Suppose I have Two Entity classes Company.java & Employee.java.
Employees(e_id) saved under Company(c_id) and relation stored in another table "company_employee".
I want to implement search on only those employees(not all employees of other companies) which are associated with company account.
for eg: select e_id from company_employee where c_id = ?
Now assume i getting 3 employees of company and want to fetch the all record of these 3 employees and not others.
You fetch company and not employee like this :
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Company.class).get();
In your lucene query :
luceneQuery = queryBuilder.keyword().wildcard().onFields("employees.employeeName").matching("m*").createQuery()
Employees is the field annotatted with #IndexedEmbedded.
public class Company {
...
#IndexedEmbedded
private List<Employee> employees;
...
}
You will get a company with only its employees and not all the employees. (I don't know if you have to use eager fetch or not)
Is that what you are looking for ?

Limiting size of jpa association query

The following query gets all the employees in the department:
List<Employee> employees = em.find(Department.class,departmentid).getEmployeeList();
However, i am looking to get only a limited number of results from the above query and not complete resultset. Is it possible via the above query?
I am aware of em.createQuery() alternative however would like to use the existing one-to-many association in the entity and not write a new query to get limited results.
Any help or ideas would be great.
Use JPQL,
Select e from Department d join d.employees e where d.id = :id
And set the maxResults on the query

JQgrid / Entity Framework Issue with entities that have a relationship

I have an entity that has a relationship with another entity. I am able to search on columns that are in the main entity, and include columns from the relationship entity. But I need to be able to filter the list (search) on columns that are not in the relationship entity.
for example
the Invoice Entity contains a customerId property, and is related to the Customer Entity which contains the customerName property
I need to be able to search / filter the grid by customerName.
I am new to entity framework, please help.
thanks
Carl
Your relation is 1->1. In these cases I usually return a custom class to the grid that has all the columns I need, including joins with other tables.
So basically what you need is to create a custom linq query with your resultset.
The mais query should follow this example:
var q = from i in ctx.Invoices
join c in ctx.Customers on i.CustomerID equals c.CustomerID
select new{InvoiceID=i.InvoiceID, InvoiceDate=i.Date, CustomerName=c.Name};
Now, assuming we receive a CustomerName variable with the string to filter by c.Name we could do:
if(!string.IsNullOrEmpty(CustomerName))
{
q = q.where(c => c.Name.ToLower().Contains(CustomerName.ToLower()));
}
Notice that I'm performing a ToLower() operation and a Contains, this will beahave as a LIKE ingnoring case sensitivity and searching for the string anywhere in the Customer Name.
At the end you'll return the q.ToList() serialized for the jqGrid...