Declaration of entities in EclipseLink JPA native queries - eclipselink

Does EclipseLink support the declaration of entities in JPA native queries?
I am looking for something like this
sess.createSQLQuery("SELECT {cat.*}, {m.*} FROM CATS c, CATS m WHERE c.MOTHER_ID = m.ID")
.addEntity("cat", Cat.class)
.addEntity("mother", Cat.class)
The code example is using the Hibernate API and I wonder whether something similar is possible using the unwrapped Eclipse JPA query.

Related

Jpa query with Predicate

How select one column by predicate in jpa?
For exemple like this
#Query("SELECT SUM (u.sum) from User u")
Double findSums(Predicate predicate);
Spring Data JPA does not have direct support for this kind of query.
Therefore the solution is to fall back to plain JPA in a custom method for your repository.
In that custom method you'd use the Criteria API to create the SELECT SUM (u.sum) from User u apply the predicate to it and execute it using the EnterpriseManager.

Default class for Hibernate Single Table Inheritance Scheme

I have an application that maps views into an existing Hibernate schema that makes extensive use of single table inheritance with a discriminator-value in the CLASSNAME column. What makes my situation unusual is that I do not what to support all the subclasses (discriminator-value in the Hibernate mappings) in the data, just those of interest to the application.
The problem is that if the application encounters an unsupported discriminator-value, Hibernate throws a "org.hibernate.WrongClassException" because it cannot find a mapping with the required discriminator-value.
What I would like to do is to create subclass that Hibernate will use if the discriminator-value is unknown (instead of throwing an exception).
Is there a way to do this?
Thanks,
Ed
I found the answer here for those using annotation:
Mapping Hibernate entity for unknown DiscriminatorValue for InheritanceType.SINGLE_TABLE.
What this does not tell you is what to use in a mapping file. I checked the open source code and it appears that the discriminator-value="<not null discriminator>" will do the trick. Hibernate checks for this value (and <null discriminator>), and gives them special handling.

Hibernate and dynamic SQL

do you know anything about Hibernate and its ability to generate dynamic sql queries with HQL?
I you have any links I would appreciate posting, I can't find nothing about it in Hibernate`s documentation.
Best regards
Gabe
//edit
so maybe I will precise what I mean. I am wondering if some HQL code generates SQL queries which uses something like EXECUTE (for postgres)
Not sure what you're aiming for here? HQL in Hibernate will always generate SQL, and you can put your HQL together differently based on input. It will always generate new SQL for each permutation and run. You can make Hibernate precompile/cache queries but that's just a performance optimization and shouldn't be your first concern.
I would also consider looking into the Criteria API which lets you stay a lot more object oriented instead of working with tons of strings.
If you're talking about static queries using dynamic arguments, the syntax is
select f from Foo f where f.bar = ? and f.zim = ?
or, with named parameters
select f from Foo f where f.bar = :bar and f.zim = :zim
If you're talking about completely dynamically created queries based on a set of criteria, then the API to use is... the Criteria API.
Both are largely covered in the Hibernate reference documentation.

NHibernate 3 full-text searching with table-per-subclass setup

I'm using FluentNHibernate and I have just moved to NHibernate 3.0. I have also changed my database schema to a table-per-subclass configuration which I really like. We used full-text search before using MS SQL 2005's built in full-text search which worked fine because all of our content was of one class and one table. Now that our data is spread out among different subclasses/tables with different fields to be indexed on each class we will typically want one search against all subclasses. What is the best way to accomplish this and how would I query it?
We have been using Linq more lately but I would be OK with HQL.
I eventually solved this by chaining by HQL queries like this...
string selectCat = #"from Cat c where freetext((c.Name),:keyword)";
string selectDog = #"from Dog d where freetext((d.Name,d.OwnerName),:keyword)";
var animals = session.CreateQuery(selectCat).SetString("keyword", keyword).List<BaseAnimal>().Concat<BaseAnimal>(session.CreateQuery(selectDog).SetString("keyword", keyword).List<BaseAnimal>()).ToList<BaseAnimal();

Is the Entity Framework unable to use non-entity classes in a query?

I am trying to determine if I am doing something wrong, or if the Entity Framework just isn't meant to do this. I want to perform a LINQ style query to populate an object, including lists of child elements. In Linq2SQL this is valid, and is converted into an efficient SQL query whose results populate the new list.
class ReportItem
{
string Manager; /* ... */
List<string> Employees; /* ... */
}
var report = (from manager in entities.Managers
select new ReportItem()
{
Manager = manager.Name,
Employees = manager.Employees.Select(e => e.Name).ToList()
}).ToList();
With the Entity Framework the Employees line will result in a method cannot be translated into a store expression error - the Entity Framework does not seem to like constructing non-entity classes inside a LINQ statement, even though it does not involve any database side logic.
I understand that for change tracking entity classes are used, but is it not possible to read information into arbitrary classes? I don't want to construct entities and database views for every possible read scenario, and I don't want to send half the database across the pipe (entire Employee or Manager entities) just to read off something like the name property.
Is it possible to do this kind of 'lazy but efficient' query with the Entity Framework, or is the Entity Framework just not built with it in mind? I have already come across a number of issues converting a project from Linq2SQL to the Entity Framework, and this could really kill the deal.
LINQ to Entities only supports parameterless constructors and Initializers.
To be more specific, by design, LINQ to Entities requires the whole LINQ query expression to be translated to a server query. Only a few uncorrelated subexpressions (expressions in the query that do not depend on the results from the server) are evaluated on the client before the query is translated. Arbitrary method invocations that do not have a known translation, and parameterized initializers in your case, are not supported.
That said, what you are looking for could be easily accomplished by a Anonymous type Projection:
var report = (from manager in entities.Managers
select new
{
Manager = manager.Name,
Employees = manager.Employees.Select(e => e.Name)
})
.ToList();