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.
Related
I am trying to generate Native Query like this so that I can run them separately.
Expected Query:
select test.TEST_KEY
from TEST_TABLE test
where test.TEST_CODE = 'TEST_01' and test.TEST_ACCOUNT_NUMBER = '001' and test.POSTED_UTC_DATE between timestamp '2020-06-19 23:59:59' and timestamp '2020-06-19 23:59:59'
Query I am getting
select testTable.testKey
from testTable
where testTable.testCode = 'TEST_01' and testTable.testAccountNumber = '0000124001' and testTable.postedUtcDate between timestamp '2020-06-19 23:59:59' and timestamp '2020-06-19 23:59:59'
Code
public String getTestResults(DataDto dataDto) {
SQLTemplates templates= OracleTemplates.builder().printSchema().build();
Configuration configuration=new Configuration(templates);
configuration.setUseLiterals(true);
PathBuilder<?> entityPath = new PathBuilder<>(getEntityClass(), getEntityName());
SQLQuery<Object> sqlQuery= (SQLQuery<Object>) new SQLQuery(configuration)
.select(entityPath.getString(getColumnMap().get("TEST_KEY")))
.from(entityPath)
.where(buildCondition(dataDto).build());
sqlQuery.setUseLiterals(true);
String query=sqlQuery.getSQL().getSQL();
return query;
}
I referred to this post and query DSL document however no help so far.
How to get fully materialized query from querydsl
http://www.querydsl.com/static/querydsl/3.3.1/reference/html/ch02s03.html
There was a bug in constant rendering in old versions of QueryDSL. Please upgrade to a more recent version.
See also: https://github.com/querydsl/querydsl/issues/2017
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.
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 am getting the following error on the execution of the below hibernate transaction : expecting DOT, found '=' near line 1, column 32 [update t_credential set status = :status , assigned_engine = :engine where id = :id] .
Also, t_credential is a table and not an object. Does hibernate allow to use this way or does it compulsorily have to be an object?
for(Credential credential: accountList){
Query query = ssn.createQuery("update t_credential set status =:status , assigned_engine = :engine where id = :id");
query.setParameter("status", status);
query.setParameter("engine", assignedTo);
query.setParameter("id", String.valueOf(credential.getId()));
int result = query.executeUpate();
}
Look at the HQL example here: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html#batch-direct. It seems that you need to refer to an object: "update t_credential c", and then update it's fields, like this: "set c.status = :status" and so on.
If you want to use Hibernate with SQL Query (and not HQL query), you may have to use a different function.
ssn.createSQLQuery(" ... ");
I never used this function, so I hope it's a good answer for you.
Max
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.