I have issues injecting the database schema to my #ColumnTransformer, eg
#ColumnTransformer(
read = "(select trim(sct.CD) from {h-schema}SCT sct where sct.CD_TBL_ID = 4 and sct.CSN = KEY_VAL_TCSN)"
)
Does not resolve the {h-schema}, but instead it translates to
(select trim(sct.CD) from {h-kvlist0_.schema}VTKC028_SCT kvlist0_.sct where sct.CD_TBL_ID = 4 and sct.CSN = kvlist0_.KEY_VAL_TCSN)
I am trying to resolve crossreference of KEY_VAL_TCSN from SCT.CD table.
So it replaces the {h-schema} with {h-kvlist0_.schema} instead of the query schema.
Related
I am working on a spring boot application that queries a large set of data from the db and is need to be processed in my application.
I am using #SqlRessultSetMapping in the entity class but getting org.hibernate.MappingException: Unknown SqlResultSetMapping every time the query runs.
below is the example of how i am mapping the columns.
#SqlResultSetMapping(
name = "QueryResultMapping",
entities = #EntityResult(
entityClass = AwaitingCancellationModel.class,
fields = {
#FieldResult(name="Colum1_Key",column = "Colum1Value"),
#FieldResult(name="Colum2_Key",column = "Colum2_Value"),
#FieldResult(name="Colum3_Key",column = "Colum3_Value"),
#FieldResult(name="Colum4_Key",column = "Colum4_Value"),
#FieldResult(name="Colum5_Key",column="Colum5_Value"),
#FieldResult(name="Colum6_Key",column = "Colum6_Value"),
#FieldResult(name="Colum7_Key",column = "Colum7_Value")
}
)
)
and the query is being made as List<ClassModel> queryResult = entityManager.createNativeQuery(sqlQuery,"QueryResultMapping").getResultList();
i have a nested query as part of sqlQuery.
Any help is appreciated! Thanks!
I created dynamic query using jpa criteria but an extra pair of parentheses gets added to columns to be selected when I do userGroupSubquery.select(userGroupsRoot);
generated query
select (securitygr3_.group_name, securitygr3_.user_name) from gm.security_groupings securitygr3_ where 1=1 and securitygr3_.user_name='xxx' and (securitygr3_.group_name in ('XYZ'))
expected query:
select securitygr3_.group_name, securitygr3_.user_name from gm.security_groupings securitygr3_ where 1=1 and securitygr3_.user_name='xxx' and (securitygr3_.group_name in ('XYZ'))
Subquery<SecurityGroupings> userGroupSubquery = secUsersQuery.subquery(SecurityGroupings.class);
Root<SecurityGroupings> userGroupsRoot = userGroupSubquery.from(SecurityGroupings.class);
Path<SecurityGroupingsId> secGroupId = userGroupsRoot.get("id");
Path<SecurityUsers> secUsers = secGroupId.get("securityUsers_1");
Path<SecurityUsers> securityUsers = secGroupId.get("securityUsers");
Path<String> su_name = secUsers.get("name");
Path<String> name = securityUsers.get("name");
userGroupSubquery.select(userGroupsRoot);
userGroupSubquery.getCompoundSelectionItems();
//userGroupSubquery.where(criteriaBuilder.equal(pet.get(SecurityGroupingsId_.id), root.<String>get("name")));
Predicate restrictions3 = criteriaBuilder.conjunction();
restrictions3 = criteriaBuilder.and(restrictions3, criteriaBuilder.and(criteriaBuilder.equal(su_name, dto.getUserId().trim().toUpperCase())));
restrictions3 = criteriaBuilder.and(restrictions3, criteriaBuilder.and(name.in(userGroups)));
userGroupSubquery.where(restrictions3);
restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(userGroupSubquery));
}
secUsersQuery.where(restrictions);
Its just that I get an extra pair of parentheses at select (securitygr3_.group_name, securitygr3_.user_name) from
which gives me ora-00907 missing right parenthesis error. I am sure it is coming from userGroupSubquery.select(userGroupsRoot) but I am not sure why. Please help
I got the solution for the above. When the entity has a composite key, then in JPA criteria instead of doing
userGroupSubquery.select(userGroupsRoot);
we should do
userGroupSubquery.select(userGroupsRoot.get("id"));
where id is the composite id.
I have an insert JPA named query as below. When I try to execute it from my DAO using enity manger I am getting the below error. I am using Apache OpenJPA, I could not identify the root cause of this issue. I cannot use the persist method because of the complexity of my domain object.
Encountered "INSERT" at character 1, but expected: ["DELETE", "SELECT", "UPDATE"].
#NamedQuery(
name=IuaPersistenceConstants.QUERY_INSERT_AGREEMENT_ACKNOWLEDGEMENT,
query="INSERT INTO AgreementAcknowledgement agrackn VALUES agrackn.id.agrmntCntntUrl = :agrmntCntntUrl, agrackn.id.prvsndUserSqn = :prvsndUserSqn, agrackn.id.prvsnUserEffD = :userEffDate, " +
"agrackn.id.agrmntacknEffD = :agrmntacknEffD, agrackn.acknExpD = :acknExpD, agrackn.crtTs = :crtTs, agrackn.crtUidC = :crtUidC, agrackn.lstUpdtTs = :lstUpdtTs, agrackn.lstUpdtUidC = :lstUpdtUidC")
Query ackQuery =getEntityManager().createNamedQuery(IuaPersistenceConstants.QUERY_INSERT_AGREEMENT_ACKNOWLEDGEMENT);
ackQuery.setParameter("agrmntCntntUrl", agreementDO.getAgreementURL());
ackQuery.setParameter("agrmntacknEffD", agreementDO.getAgreementEffDate());
ackQuery.setParameter("acknExpD", agreementDO.getAgreementExpDate());
ackQuery.setParameter("prvsndUserSqn", userDO.getUserSequence());
ackQuery.setParameter("userEffDate", userDO.getUserEffDate());
ackQuery.setParameter("crtTs", new Date());
ackQuery.setParameter("crtUidC", "WS");
ackQuery.setParameter("lstUpdtTs", new Date());
ackQuery.setParameter("lstUpdtUidC", "WS");
ackQuery.executeUpdate();
OpenJPA doesn't seem to support JPQL "INSERT" queries. But then that is not surprising since JPA DOES NOT define INSERT queries. It defines SELECT, UPDATE, DELETE queries and that is all. See the JPA spec. This is not SQL.
Let's say I have two tables Task and Company. Company has columns id and name. Task has two columns customerId and providerId which link back to the id column for Company.
Using Querydsl how do I join on the Company table twice so I can get the name for each company specified by the customerId and providerId?
Code that maybe explains better what I'm trying:
Configuration configuration = new Configuration(templates);
JPASQLQuery query = new JPASQLQuery(this.entityManager, configuration);
QTask task = QTask.task;
QCompany customer = QCompany.company;
QCompany provider = QCompany.company;
JPASQLQuery sql = query.from(task).join(customer).on(customer.id.eq(task.customerId))
.join(provider).on(provider.id.eq(task.providerId));
return sql.list(task.id, customer.name.as("customerName"), provider.name.as("providerName"));
Which generates SQL:
select task.id, company.name as customerName, company.name as providerName from task join company on company.id = task.customerId
And I'd really like it to be:
select task.id, customer.name as customerName, provider.name as providerName from task join company as customer on customer.id = task.customerId join company as provider on provider.id = task.providerId
I couldn't figure out how to alias the table I was joining so I could distinguish between customer and provider names. I tried doing new QCompany("company as provider") but that didn't work. Anyone know how one can do this?
If you need to variables just do the following
QCompany customer = new QCompany("customer");
QCompany provider = new QCompany("provider");
Reassignment of the default variable QCompany.company doesn't help
I have a project already developed using java,jsp and JPA (open jpa).Now i want to add a new API to retrieve data from DB.Am not much familiar with JPA.Now i want to take a join of 3 tables Atbl , Btbl, and Ctble ,then check for some conditions and finally populate bean corresopnding to table Atble.I saw a a API as follows
String sql = "SELECT A.* FROM Atble A, Btbl B WHERE A.xyz = B.pqr
AND A.field1 = ? AND B.field2 = 'SubComponent' AND B.field3 = ? ";
Query q = em.createNativeQuery(sql, A.class);
q.setParameter(1,"aa");
q.setParameter(2, "aa");
q.setParameter(3, "cc");
List<A> a = (List<A>) q
.getResultList();
Does it populate bean for A directly?If not how can i populate bean for A
This will return a List, so should work fine.
You may also consider using JPQL instead of a native SQL query, but if you are more comfortable with SQL that is fine.