JPA createQuery() - jpa

When using JPA createQuery(), I found that we can use both class name and entity name to get the DB data.
This is by class name
em.createQuery("from com.model.Stuff s",
Stuff.class).getResultList();
And this is from entity name
em.createQuery("from Stuff s",
Stuff.class).getResultList();
This is in orm.xml
<entity class="com.model.Stuff" name="Stuff">
No matter which one i use, JPA can get the specific class that i am using from the orm.xml or from the annotation that i put in class.
So why i have to put Stuff.class in the parameter?
Because it can only put at most two parameters in createQuery(), What if i have to select two Class to do some join?
i cannot do this
em.createQuery("from Stuff s, Thing t where s.id = t.stuff_id", Stuff.class, Thing.class).getResultList();
Is
em.createQuery("from Stuff s",
Stuff.class).getResultList();
equal
em.createQuery("select s from Stuff s",
Stuff.class).getResultList();
Thanks to give me some help.

Stuff.class goes into the parameter to determine what type of query result object. A query can be infinitely complicated with dozens of nested types, but it must return one type of object in the end.
Then you must create a third Java type that will be result of join. Think about it, what type SHOULD the result be after a join?
As far as I know, yes

Related

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.

Is it possible to have a JPA entity that doesn't have a table itself, but is an aggregate of other entities?

Is it possible to have an entity such as
#Entity
class Aggreate {
B b;
C c;
D d;
}
where aggreate itself is not stored in a seperate table, but B C D are? Aggregate will be queryable and will cascade operations to its parts. I will use such a class to manage B, C, D without exposing their methods to everyone.
Maybe you could consider using construction of an aggregate result class as described in Result Classes (Constructor Expressions)
JPA supports wrapping JPQL query results with instances of custom result classes. This is mainly useful for queries with multiple SELECT expressions, where custom result objects can provide an object oriented alternative to representing results as Object[] elements.
Any class with a compatible constructor can be used as a result class. It could be a JPA managed class (e.g. an entity class) but it could also be a lightweight 'transfer' class that is only used for collecting and processing query results.
It sounds like those are not entities per se, to be sure a knowledge of the domain is needed.
If they aren't needed as entities maybe the #Embeddable can help.

Understanding Hibernate query

I'm new to Hibernate and I encountered a query today:
select new SomeClassDTO(r.id, r.name, r.description, u.id) from ClassA as u
inner join u.data as r where u.email !=?1 and r.name not like '%Blah%
Can you please explain how this query works?
This query takes several fields from a ClassA entity with it's associated data Entity(s) and passes those field values into the constructor for class SomeClassDTO.
As the name of the created object implies, this is a way to take data from multiple associated database-mapped Entities, and construct a Data Transfer Object (DTO) to pass to your presentation layer.
You are essentially creating a 'view' of your database Entities and constructing new objects to hold the records of the result set of that view.

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.

How do you determine subtype of an entity using Inheritance with Entity Framework 4?

I am just starting to use the Entity Framework 4 for the first time ever. So far I am liking it but I am a bit confused on how to correctly do inheritance.
I am doing a model-first approach, and I have my Person entity with two subtype entities, Employee and Client. EF is correctly using the table per type approach, however I can't seem to figure out how to determine what type of a Person a specific object is.
For example, if I do something like
var people = from p in entities.Person select p;
return people.ToList<Person>();
In my list that I form from this, all I care about is the Id field so i don't want to actually query all the subtype tables (this is a webpage list with links, so all I need is the name and the Id, all in the Persons table).
However, I want to form different lists using this one query, one for each type of person (so one list for Clients and another for Employees).
The issue is if I have a Person entity, I can't see any way to determine if that entity is a Client or an Employee without querying the Client or Employee tables directly. How can I easily determine the subtype of an entity without performing a bunch of additional database queries?
Use .OfType<Client>() in your query to get just the clients. See OfType.
e.g. entities.Person.OfType<Client>() ...
Use is to test if a Person object is a specific sub-class, e.g. if (p is Employee) ...
BTW why isn't it entities.People? Did you not select the pluralization option?