using list to map tables in HQL - hql

Im using HQL to create a query in a report application, my query looks like this:
from Identity i, Bundle b, IdentityBundles aib
WHERE b.name LIKE 'DYP%' AND i.bundleSummary LIKE 'DYP%'
AND i.id = aib.identityId AND b.id = aib.bundle
I have the next error when I tried to run this query:
IdentityBundles is not mapped [select count(*) from sailpoint.object.Identity i, sailpoint.object.Bundle b, IdentityBundles aib
Looks like there is not mapping to IdentityBundles
The only think that I noticed in the jar files from the application is this list name ( founded in the Identity.hbm.xml file ) with the attributes from the table IdentityBundles
list name="bundles" table="IdentityBundles"
cascade="persist,merge,save-update">
key column="identityId"
list-index column="idx"
many-to-many column="bundle" class="sailpoint.object.Bundle"
list
Can I use this list to map the table or do I need to create the class and the hbm.xml file ??

Hibernate works with mappings. You are trying to specify the joins and since those are already mapped in your domain objects you don't need to do this. Use the domain object properties instead and Hibernate will fetch them for you.
from Identity i
join i.bundles b
where b.name like ...
etc.

Related

How to do the mappings for joins with OR conditions using Hibernate

I am trying to do the mappings and trying to write a non-native HQL query by joining the 4 tables. The logic is written in stored procedure, but we want to migrate it to hibernate/JPA. I am unable to do proper mappings and write a query to create same logic
FROM
[dbo].[vwInstitutionUser]AS IU
INNER JOIN [dbo].[vwCommonCourse] AS C
ON C.CustomerID=CAST(IU.InstitutionID AS VARCHAR(10))
INNER JOIN [dbo].[vwCommonCourses] AS CM
ON C.CourseID = CM.CourseID
INNER JOIN dbo.vwProductMaster AS PM
ON CM.LearningActivityID = PM.ResourceID
OR CM.AssessmentID = PM.AssessmentID
WHERE
IU.UserID = #UserID
AND PM.IsCert = CASE WHEN #SubReportID='MACS' THEN PM.IsCert ELSE
#IsCert END
ORDER BY PM.IsCert, PM.ResourceName
Please let me know if anybody has same use case, Thanks!
I m not sure if I undrestand you correctly, but i would recommend you the following actions:
Each inner join can be mapped as a relation within JPA (#OneToMany,
#OneToOne, #ManyToOne,#ManyToMany)
Ordering can be implemented with Comparable interface on entity level
For the computed values i would suggest to use sql computed value or service layer within your business logic which will provide you those values (#Transient field)
You can also write NativeQuery for you model
Hope that i answered at least some of your questions :)

How to use #QueryInit to init path to attribute of embedded object in QueryDSL query?

According to the QueryDSL docs, (http://www.querydsl.com/static/querydsl/4.0.8/reference/html_single/#d0e2250) only the first two levels of paths are initialized in generated Q classes, so if an object A has a relationship inside a #Entity B defined by an #JoinColumn, you can't do things like b.a.id in a query to access the id for A from inside B. They have a #QueryInit annotation to use in those situations where we need to have deeper paths but the docs are very brief and I don't understand how its used. Right now Im getting an "invalid path" exception when I try to run the query inside my application. Has anyone use #QueryInit to fix a problem similar to mine ?
The query where clause (ie. in QueryDSL) is something like a.id.eq(b.a.id) where a is "embedded" inside b
The exception Im seeing is:
"org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'b.a.id' [select a from A a where a.id is not null and (a.id = b.a.id and a.amount <= sum(b.amount))]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'b.a.id' [select a from A a where a.id is not null and (a.id = b.a.id and a.amountCents <= sum(b.amountCents))]",
The query Im trying to "translate" into QueryDSL looks like this:
SELECT * FROM A a
WHERE a.amount <= (SELECT SUM(b.amount) FROM B b WHERE b.a_id=a.id);
The predicate ended up simply being:
a.amount.loe(
JPAExpressions.select(b.amount.sum()).from(b).where(a.id.eq(b.a.id)))
)

JPA repository queries by function name for not equals

I'm trying to write a query by function name available in Spring (JPA). I need something like this select * from table where col1=x AND col2=y AND col3 <> z. Would the following function name be equivalent?
findByCol1AndColb2ndCol3IsNot(....)
findByCol1AndCol2AndCol3Not -> you just need to remove the "Is" . The following page lists all the possible keywords:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

Partial select of left joined table with doctrine query builder

When querying one table using the doctrine query builder a partial select can be written like this:
$queryBuilder = $this->createQueryBuilder('person');
$queryBuilder->addSelect('partial person.{id, name}');
How can one write a partial select be written for a left joined table? I tried something like this, but can't figure out the correct syntax:
$queryBuilder = $this->createQueryBuilder('person');
$queryBuilder->join('person.address');
$queryBuilder->addSelect('partial person.{id, name} person.address.city'); // ???
My goal would be to select only parts of the Person and the Address object when executing the query to be more memory efficient.
Your syntax is off for your join operation. You have to give an alias when using join. From there, you can just use the same syntax to query your partial Address object:
// In a method of PersonRepository
$qb = $this->createQueryBuilder('person')
->select(['partial person.{id, name}', 'partial address.{id, city}'])
->join('person.address', 'address');
Notice that I added id to the fields retrieved for Address. If you don't, Doctrine will give you the following error:
Error: The partial field selection of class Path\To\Entity\Address must contain the identifier
As a side note, you said you wanted to write this select for a left joined table. If you want to perform a LEFT JOIN, you need to use leftJoin instead of join (the signature of both methods is the same).

Is this linq query efficient?

Is this linq query efficient?
var qry = ((from member in this.ObjectContext.TreeMembers.Where(m => m.UserId == userId && m.Birthdate == null)
select member.TreeMemberId).Except(from item in this.ObjectContext.FamilyEvents select item.TreeMemberId));
var mainQry = from mainMember in this.ObjectContext.TreeMembers
where qry.Contains(mainMember.TreeMemberId)
select mainMember;
Will this be translated into multiple sql calls or just one? Can it be optimised? Basically I have 2 tables, I want to select those records from table1 where datetime is null and that record should not exist in table2.
The easiest way to find out if the query will make multiple calls is to set the .Log property of the data context. I typically set it to write to a DebugOutputWriter. A good example for this kind of class can be found here.
For a general way of thinking about it however, if you use a property of your class that does not directly map to a database field in a where clause or a join clause, it will typically make multiple calls. From what you have provided, it looks like this is not the case for your scenario, but I can't absolutely certain and suggest using the method listed above.

Resources