JPA Join Column with where condition OR filter an entities with any declarative way - select

I know there is an option in Hibernate to filter entites based on an #Filter annotation!
Is there an option for a JPA 2.0 version of this (i am interested in mainly non-programmatic way -> declarative e.g. persistance.xml option or annotation option)
The goal:
In our application we want to filter all entites whose attribute Active is False or 0.
And do not love to write 30 or more specific selects for this!
I hope it has a more clear way to achieve this!
The perfect functionality would be #Filter and #FilterJoinTable, referenced this answer:
Filter list contained in entity returned by jpa/hibernate query

After some investigation i may say there is no option in JPA to get a filter function with Annotation.
The closest ones to this is:
Criteria API (http://www.objectdb.com/java/jpa/query/criteria)
Manual written query (entityManager createQuery() or createNativeQuery()) for getting entites. (http://www.objectdb.com/java/jpa/query/execute)
However I think it is a missing and very needed functionality :(

Related

Entity Framework query based on string stored in configuration file

i would like to know if you have any idea how i can achieve this, considering a query stored as string in the configuration file.
I tried to use SqlQuery applied to the DBSet, but the problem is that SqlQuery requires me to select all properties of the required entities in my query. If i don't consider any column, it will complain because is not able to map the query to the entities.
I don't want to select all properties of the entities i want to query.
Thanks
If you are using EF then why not use Database.ExecuteSqlCommand()? It's in the System.Data.Entity namespace.
For example:
int result = db.Database.ExecuteSqlCommand("Non SELECT SQL etc...");
Well, I ended up implementing a mechanism using reflection that basically receives a group of fields to select, and constructs dynamic objects with those fields, so when applied the query with the joins between the entities, will only bring the fields I am looking for.
So, considering Entity1, Entity2, Entity3 with the following relationship
<b>Entity1</b>{
<br/> Entity1Name, <br/> List<*Entity2*> Entity2Items, <br/> etc..
<br/>}
and
<b>Entity2</b> { <br/> Entity2Name, <br/> List<*Entity3*> Entity3Items <br/>}
I can store e.g. the following query in the configuration file, and retrieve the information:
"Entity1.Entity1Name", <br/>
"Entity1.Entity2Items.Entity2Name", <br/>
"Entity1.Entity2Items.Entity3Items.Entity3Name"
Anyway, I was just trying to see if there would be any solution out-of-the-box that would require minimal code changes.
Thank you.

JPA: Map a group of table columns extracted by native query to entity

I know that it is possible using #SqlResultSetMapping but I want to select not whole entity from the database but some fields and then map i to my entity using one of the constructor which accept that fields. Is that possible to map result with #EntityResult for only a few #FieldResult? I was trying to do that and all the time I get error which said that there is not specify mapping for some fields which exist in that entity.
The disadvantage of #SqlResultSetMapping is that you have to select all the columns.
The alternate way of doing this manually iterate over the DB result and populate your objects.
Well, if you are using JPA 1.0 your only option (not considering the manual mapping, of course), is to use #SqlResultSetMapping and map the whole table columns. With JPA 2.1 you can add a javax.persistence.ConstructorResult (see docs here) to map only the needed columns.

How to Achieve filters while mapping entity to table?

As it is mentioned in this document
http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Filters
How to acheive filtering data?This is nothing to do with inheritance.I want to restrict my object mapping to only subset of rows based on some condition.
There is no standard JPA support for this.
If you are using EclipseLink, you can either use #AdditionalCriteria, or #Multitenent,
see,
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_additionalcriteria.htm#additionalcriteria
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_multitenant.htm#BABIEJGD
You could also use a view in the database.

can I perform a query inside of JPA entity to get back a single column

I have a dumb question. It would be great if this could be done, but I am not holding my breath.
I need a single column from a table linked to my JPA entity to be a collection in said JPA entity. Is there any way, that I can just get back that column alone that is related to that entity, instead of having to get back an entire table (which could be very costly?)
Can I perform a query inside that JPA entity that will be performed and loaded eagerly into a collection?
I am trying to avoid having to make several calls to the database by just executing a couple of queries.
What are your thoughts on this?
#ElementCollection(fetch=FetchType.EAGER)
#CollectionTable(name="QUICK_LAUNCH_DISTLIST",joinColumns=#JoinColumn(name="QUICK_LAUNCH_ID"))
#Column(name="LIST_ID")
private List<Long> distListIDs;
The ElementCollection attribute is what I was looking for. It seems to work pretty well in addition to that.
Thanks for the help and inspiration guys.
Suppose a Category has many products:
select product.name from Category c inner join c.products product where ...
If that's not what you want, please show an example in your question.

Can't insert entries of a many-to-many relationship in Entity Framework in a specific order

I have a many-to-many relationship between 2 entities in Entity Framework, like here . So, Employees and Projects. At one point, I would like to insert some Projects to a Employees entity in a specific order. By conserving the order I would like to know which was the first preference of the Employees for a Projects entity. The thing is that although I order the Student.Projectslist in the way I like before the insert, when selecting Employees.Projects.FirstOrDefault(), the entities are ordered after the ProjectsId and I don't get the first element I inserted. How can I conserve the order I want?
O course, I could make a new field PreferredProjects and save the other Projects in a random order, since only the preferred one is important for me. But this is not an option, being given the context of the current project's software design.
Thank you in advance...
It sounds like you simply want to have sorted child collection results when you do a query, rather than take full control of the insert order.
You can achieve that using the techniques described in Tip 1 of my tips series.
Hope this helps.
Alex
Program Manager Entity Framework Team.
Unfortunately, there is no easy solution. I have the same problem. The only solution is to save after adding each child item (project). This is the only way to save the order without using a new field column to sort input.
Try Employees.Projects.OrderBy(x => x).FirstOrDefault()