Select specific columns from table with JPA in java - jpa

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

Related

jpa java JPQL get the list of entities Type

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)

Spring Data Jpa equality of two column

I want to learn if we could write a query that has a condition like
List<Entity> findbyField1EqualsField2();
This method should not take any parameter . It should fetch entities which has a field1 equals field2. It is just a simple sql :
select * from entity where field1=field2.
But I could not find any solution yet. Thanks.
Create an operation with a query as next:
#Query("select t from entity t where t.field1 like t.field2")
List<T> findByField1LikeFie‌ld2();
I don't think findByField1LikeFie‌​ld2()works.... you would need to do it passing a param findByField1Like(St‌​ring param) and for this maybe you would need to load the entity before to get the value of field2.

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...

Doubt regarding JPA namedquery

I am trying to execute a namedquery
#NamedQuery(name="getEmployeeDetails",query="select e.username,e.email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1")
Now when I execute this query in a EJB 3.0 Session Bean what is the object I should return.I tried returning Listits returning a Vector which creates a classcast exception.The employee table contains fields like password and other confidential details which I don't want to fetch.So I am not using select e from Employee e.
I am learning JPA can anyone help.
Below is the sample query which fetches only the required fields, but have to make such constructor for it.
Query : SELECT NEW package_name.Employee(e.username,e.email,e.image,e.firstname,e.lastname) FROM Employee e where e.empid=?1;
It will return Employee entity with selected fields & remaining will have default values.
Inspect the returned type by calling .getClass() on a returned object. I'd guess it's an array.
But this is not really a good way to use JPA. Select the whole entity and then just don't use what you don't need. It's not such a performance hit.