Eclipse link how to query - jpa

I started to work with eclipse link and from what I read in the net the eclipse link is some kind of JPA implementation and you are not working directly with DB code when you are using eclipse link you are working with objects.
My question is what is this if not DB command?
Query q = em
.createQuery("SELECT p FROM Person p WHERE p.firstName = :firstName AND p.lastName = :lastName");

It's JPQL. You can find more here

Its JPA query equivalent to SELECT * FROM PERSON WHERE FIRSTNAME='Stefan' and LASTNAME='Strooves'; further you need to set value for parameters.
Query q = em
.createQuery("SELECT p FROM Person p WHERE p.firstName = :firstName AND
p.lastName = :lastName");
q.setParameter("firstName", "Stefan");
q.setParameter("lastName", "Strooves");
List<Person> resultList = q.getResultList();
Result list contains all the Person entities matched with query.

Related

OpenJPA String as date attribute

I have a Query to parse on JPA , some errors have happened . I want a right way to make this.
Original:
Query q = this.em.createQuery("SELECT e FROM Entity e WHERE e.codigo = :codigo ORDER BY e.data ASC");
My solution but doesn't work:
Query q = this.em.createQuery("SELECT e FROM Entity e WHERE e.codigo = :codigo ORDER BY to_date(e.data,'DD/MM/YYYY') ASC");
where e.data is a String like "01/01/2014"
Solved
I´d replaced it with a nativeQuery.
Then a had be able to use Oracle Date Function

JPA Criteria select all instances with max values in their groups

Is there a way to write with JPA 2 CriteriaBuilder the equivalent of the following query?
select * from season s1
where end = (
select max(end)
from season s2
where s1.contest_id=s2.contest_id
);
In JPQL this query is:
Select s1 from Season s1
where s1.end = (
select max(s2.end)
from Season s2
where s1.contest=s2.contest
)
This should work, with contest being either a basic Integer property, or a ManyToOne property pointing to another non-basic Entity.
EntityManger em; //to be injected or constructed
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Season> cq = cb.createQuery(Season.class);
Subquery<Date> sq = cq.subquery(Date.class);
Root<Season> s1 = cq.from(Season.class);
Root<Season> s2 = sq.from(Season.class);
sq.select(cb.greatest(s2.get(Season_.end)));
sq.where(cb.equal(s2.get(Season_.contest), s1.get(Season_.contest)));
cq.where(cb.equal(s1.get(Season_.end), sq));
List<Season> result = em.createQuery(cq).getResultList();

jpa query for arithmetic operation on columns

I am somewhat new to jpa. I have created a few simple queries. But now i have a problem with a simple query. I have an entity Payment with 3 columns(Id,Amount1,Amount2).
Now i want to find all the rows from Payment where Amount1+Amount2 is greater than somevalue.I tried something like:
Query query = getEntityManager().createQuery(
"Select p from Payment p WHERE p.Amount1 + p.Amount2 >' " + someamount +" ' ");
But it is not working. I tried using SUM also but that is only one column.
Can anyone help me with this.
Thanks.
Assuming your entity is properly defined, the following query should work just fine:
final BigDecimal threshold = BigDecimal.valueOf(1000);
TypedQuery<Payment> query = getEntityManager().createQuery(
"SELECT p FROM Payment p WHERE p.Amount1 + p.Amount2> :value",
Payment.class);
query.setParameter("value", threshold);
List<Payment> results = query.getResultList();

Linq: the linked objects are null, why?

I have several linked tables (entities). I'm trying to get the entities using the following linq:
ObjectQuery<Location> locations = context.Location;
ObjectQuery<ProductPrice> productPrice = context.ProductPrice;
ObjectQuery<Product> products = context.Product;
IQueryable<ProductPrice> res1 = from pp in productPrice
join loc in locations
on pp.Location equals loc
join prod in products
on pp.Product equals prod
where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
select pp;
This query returns 2 records, ProductPrice objects that have linked object Location and Product but they are null and I cannot understand why. If I try to fill them in the linq as below:
res =
from pp in productPrice
join loc in locations
on pp.Location equals loc
join prod in products
on pp.Product equals prod
where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
select new ProductPrice
{
ProductPriceId = pp.ProductPriceId,
Product = prod
};
I have the exception "The entity or complex type 'PBExplorerData.ProductPrice' cannot be constructed in a LINQ to Entities query"
Could someone please explain me what happens and what I need to do?
Thanks
The answer to your first question the Product and Location are null because you need to add an Include("") to your query.
IQueryable<ProductPrice> res1 = from pp in
productPrice.Include("Location").Include("Product")
join loc in locations
on pp.Location equals loc
join prod in products
on pp.Product equals prod
where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
select pp;
The second issue is EF is trying to push down your query and ProductPrice (is not an entity) so it can not. If you want to do this convert it to an anonymous type so just do
select new
{
ProductPriceId = pp.ProductPriceId,
Product = prod
};
And then do
res.ToList().ConvertAll(x=new ProductPrice () {
ProductPriceId = x.ProductPriceId ,
Product = x.Product
});
Or you could do it other ways, by selecting the entity you want, and just populating manual.

Can some one help me to conver it into LINQ i m using Entity Framework

select ForumCategories.ID , ForumCategories.Title , ForumCategories.DateCreated,
CO = ( select COUNT(*) from ForumSubCategories where ForumSubCategories.CategoryID_FK = ForumCategories.ID)
from ForumCategories
var q = from fc in Context.ForumCategories
select new
{
Id = fc.ID,
Title = fc.Title,
DateCreated = fc.DateCreated
CO = fc.ForumSubCategories.Count()
};
return q;
The "join" (subquery) is implicit; it's defined in the relationship between ForumCategories and ForumSubCategories in your model. Using this syntax, the call to Count() will be done on the DB server.