I have 2 tables in a one-to-many mapping, layoutFeature and layoutPadData.
Parent table layoutFeature has 2 columns: layoutId (Pkey) and colorHex.
Child table layoutPadData has 5 columns:
layoutId (Fkey), padId (Pkey), longitude, latitude, padName
Now I want to write a query which from database will fetch data like this projection:
public interface LayoutFeatureInfo {
String getColorHex();
List<LayoutPadDataInfo> getPadLevelDataList();
interface LayoutPadDataInfo {
double getLatitude();
double getLongitude();
String getPadName();
}
}
I wrote this query :
But this doesn't work since the query returns:
All rows of layoutPadData table but I want selective of them under padLabelDataList
As I had done join, I get multiple rows of colorHex as well but I what I want is single property of parent and list of child interface something like this will be response of api when I fetch from DB:
Can anyone help me with the query please.
My personal experience with projections and join fetching has been really bad in spring boot (vanilla JPQL).
One approach is the one explained here https://vladmihalcea.com/one-to-many-dto-projection-hibernate/
Another approach is to use a 3rd party lib like https://github.com/Blazebit/blaze-persistence
I've heard great things about it but I've never used it before.
For your second problem a simpler issue is query child entity based on parent key and return a list of the things you want (hibernate is not good to understand and "turn" the sql like result-set into a single entity).
Related
I have a database table on which a sorted query needs to be done.
To do the sorting a join on another table is requiered. The problem is that this other table does not exist in the database because we read the required data on the services startup from a CSV file and keep it as an in-memory list.
Is it possible to somehow inject this list as a kind of virtual database into Spring Data? So that it could use this list to make the required join and sorting.
As far as I know, the only other options I have would be to create a real database table from this in-memory list or load the whole table and do the sorting in the service itself.
You can add a special order by expression through e.g. Spring Data Specification, but that is going to be very ugly. In HQL it looks like this:
case rootAlias.attribute when 'value1' then 1 when 'value2' then 2 ... else null end
which will return some integer value by which you can sort ascending or descending, based on the mapping you have.
Even if you have lots of values, I would rather recommend you don't do a join at all, and instead try to make this attribute of your main table sortable, so that you don't need this mapping. You can maybe create a trigger that maintains a column based on the mapping, which can be used for sorting directly. If you do all your changes through JPA/Hibernate, you could also use a #PreUpdate/#PrePersist listener to handle the maintenance of this column.
I've got 2 classes
Reports - objectID, Title, Date & relationItem ( Relation type column linked up to Items)
Items - ObjectID, Title, Date etc
I want to query all the Items that are equal to a objectID in reports. Users create reports then add items to them. These items are found in the Items table.
I've looked at the https://parseplatform.github.io/docs/ios/guide/#relations but don't see anything for swift3.
I've tried a few things with little success. This snipplet below i did find, but not sure how to apply it to my classes.
var relation = currentUser.relationForKey("product")
relation.query()?.findObjectsInBackgroundWithBlock({
Would love somebody to direct me into the right direction! Thanks
Tried this code below too!
var query = PFQuery(className:"Items")
query.whereKey("relationItem ", equalTo: PFObject(withoutDataWithClassName:"Reports", objectId:"MZmMHtobwQ"))
Ok so i had to change the table slightly to get this to work to prevent a query within a query.
I've added a relation Type to the Items table instead of the Reports Table
Then i managed to retrieve all the Items based of that report ObjectId like this:
let query = PFQuery(className:"Items")
query.whereKey("reportRelation", equalTo: PFObject(withoutDataWithClassName:"Reports", objectId:"3lWMYwWNEj"))
This then worked. Note that reportRelation is the Relational Type Column.
Thanks
When you’re thinking about one-to-many relationships and whether to implement Pointers or Arrays, there are several factors to consider. First, how many objects are involved in this relationship? If the “many” side of the relationship could contain a very large number (greater than 100 or so) of objects, then you have to use Pointers. If the number of objects is small (fewer than 100 or so), then Arrays may be more convenient, especially if you typically need to get all of the related objects (the “many” in the “one-to-many relationship”) at the same time as the parent object.
http://parseplatform.github.io/docs/ios/guide/#relations
If you are working with one to many relation, use pointer or array. See the guide for examples and more explanation.
I need to support legacy client and compose ADO datasets from our Linq queries. The problem is how get specific column information (varchar length, decimal precision, etc) that cannot be obtained using reflection.
for example, I have table Customer with field Name varchar(80)
When I fetch data from linq to entities query:
var data = (from c in ctx.Customers select c.Name).ToList()
I cannot obtain maxSize for the column data[i].Name and adodataset raises an error.
I already have simple solution:
Code to extract column metadata from ObjectContext by property reference
Simple code that parses expression from Queryable and links output properties to edm columns.
But I have a lot of issues parsing complex queries that include multiple nested groupbys/unions/joins etc.
Does anybody know any other way (maybe using materialization shaper or similar)?
Thanks to EFProviderWrappers by Joseph Kowalski I have made similar provider and published it on codeplex
I'm a Seam newbie in an already established project, so a lot of code I use is borrowed and I'm not always fully sure how things work. My problem is that I am using a query object extended from EntityQuery to back a list page with search and sort capabilities that needs to search across a many-to-many relationship and a separate many-to-one relationship which must also be used to sort. Because the many-to-many relationship has to be joined in to allow for the search capability, the query returns duplicate records for each assignment. That's not a big deal because I just added "distinct" to the ejbql and that worked fine. However, when I try to order by the other many-to-one relationship, Oracle throws an error. It appears that Oracle will not accept an order by column that is not in the select clause when using the distinct keyword http://ora-01791.ora-code.com/, and http://oraclequirks.blogspot.com/2009/04/ora-01791-not-selected-expression.html.
Here are the relationships as they are defined in the entities: [Subject m:m JobFunction] (obviously through an assignment table [Subject o:m Subject_JobFunction m:o JobFunction]), and [Subject m:o Type]. Because I need to search Subject by JobFunction, it is joined in in the ejbql which requires the distinct keyword to only return distinct Subjects to the list page. When I try to order by the Type.name (through the many-to-one relationship), the resulting query makes Oracle angry and throws the "ORA-01791: not a SELECTed expression" error. SubjectQuery code:
#Override
public String getEjbql() {
return "select subject from Subject subject left outer join subject.jobFunctions as jobFunction";
}
#Override
#SuppressWarnings("rawtypes")
public List<ValueExpression> getRestrictions() {
ValueExpression[] RESTRICTIONS = {
createValueExpression("lower(subject.name) like #{subjectQuery.prepRestriction(subjectQuery.subject.name)}"),
createValueExpression("subject.active = #{subjectQuery.active}"),
createValueExpression("subject.type.name = #{subjectQuery.typeName}"),
createValueExpression("jobFunction.name = #{subjectQuery.jobFunctionName}")
};
return Arrays.asList(RESTRICTIONS);
}
When I set the query order when a user sorts by the Type name through the front end:
"#{subjectQuery.order=='UPPER(subject.type.name) asc'}"
I get the Oracle error. If I take the distinct out of the ejbql, the sort works fine, but I get duplicate Subject records. When I add the distinct keyword the list works fine without duplicate records, but the sort throws an error. Does anyone have any suggestions about how I can restructure the ejbql to return distinct records without the distinct keyword to make the sort happy, or how to do the sort without making Oracle angry that the sort column referenced in the query is not in the select clause? I have read several places that my answer might be in the the Hibernate Criteria API, but I have no idea how to leverage it in the context of an extended EntityQuery class with what I am trying to accomplish. Please Help!
If you are adding a DISTINCT, then something is broken.
"Because the many-to-many relationship has to be joined in to allow for the search capability, the query returns duplicate records for each assignment. "
Consider the case that a person can work on many projects and a project can have many persons. There is a uniqueness of a 'person/project'. If you want a list of people that work in either project A or B (or both) then you may get
FRED/PROJ_A
BILL/PROJ_A
FRED/PROJ_B
TOM/PROJ_B
BILL/PROJ_C
If you only show the names (not the projects), you can still order by project, but you will see
FRED
BILL
FRED
TOM
BILL
If you do a DISTINCT, you can no longer order by project, because you don't know whether the FRED is the one from PROJ_A or PROJ_B or whether BILL comes before TOM (based on PROJ_A) or after TOM (based on PROJ_C).
So remove the DISTINCT and always show the column on which you are ordering (because then you'll see why the duplicates aren't actually duplicates).
I'm not sure how the generated query(ies) is(are) different, but I found an answer. I wasn't aware of the fetch command for hibernate, which fixes the need for the distinct keyword (again, not sure exactly how, maybe by subquerying?). After changing the ejbql to:
#Override
public String getEjbql() {
return "select subject from Subject subject left join fetch subject.jobFunctions jobFunction";
}
the distinct is no longer needed and therefore, Oracle does not complain about the order by column not being in the select clause. The list works as expected and the sort column works! Yay!
Predictably, I found the answer here on stackoverflow. The question was not exactly the same, but the hql syntax worked for me: HQL order by within a collection
I have a huge schema that contains my application table, I need to return these tables only.
I've used the following:
ResultSet publicTables = jdbcConnection.getMetaData().getTables(null, schema, "USER_%", dataTypes);
The problem is, I've 6 tables started by the USER_ keyword and two other arbitrary table names, how can I add OR filter for these two tables?
That's not possible. You will need to call getTables() several times (one call for each search expression)