Update query not working propertly using namedquery in jpa - java

I'm trying to update a simple table using jpa. I have written a jpa namedquery for it
#NamedQuery(name="updatedetails",query="update myentity set enddesc=?, startdesc=? Where id=?")
My code is as follows
em.createNamedQuery("updatedetails",myentity.class).serParameter(1, "abcd").serParameter(2,"gjvg").serParameter(3, "gghjj").executeUpdate();
myentity is my class name
It throws me the following error
Encountered "=" at character but expected ["."] While parsing the query
Is there anything wrong with the query

I believe that JPA named queries should use actual names are parameter placeholders, rather than ?, the latter which is used in prepared statements. So something like this should work:
#NamedQuery(name="updatedetails",query="update myentity set enddesc = :enddesc, startdesc = :startdesc Where id = :id")
List<myentity> results = em.createNamedQuery("updatedetails", myentity.class)
.setParameter("enddesc", "abcd")
.setParameter("startdesc", "gjvg")
.setParameter("id", "gghjj")
.getResultList();
As side note, you should probably make your class names begin with uppercase letters, i.e. call it MyEntity, rather than what you currently have.

em.createNamedQuery("updatedetails",myentity.class).serParameter(1, "abcd").serParameter(2,"gjvg").serParameter(3, "gghjj").executeUpdate();
Instead of serParameter use setParameter, should work.

write this code in model class
#NamedQuery(name = "new_ticket_bat.UpdateflagAutres", query = "UPDATE new_ticket_bat t set t.status_AUTRE='0' WHERE t.id= :id")
<br>
write this code in you service class
static void updateflagAutres(String id) {
DataBaseTools dbTools = new DataBaseTools("databaseOv");
try {
Query q=dbTools.em.createNamedQuery("new_ticket_bat.UpdateflagAutres");
q.setParameter("id", id);
q.executeUpdate();
dbTools.em.getTransaction().commit();
dbTools.em.close();
} catch (Exception ex) {
ex.printStackTrace();
Tools.traiterException(Tools.getStackTrace(ex));
}
}

Related

Named parameter not bound criteria native query [duplicate]

I want to execute a simple native query, but it does not work:
#Autowired
private EntityManager em;
Query q = em.createNativeQuery("SELECT count(*) FROM mytable where username = :username");
em.setProperty("username", "test");
(int) q.getSingleResult();
Why am I getting this exception?
org.hibernate.QueryException: Not all named parameters have been set: [username]
Named parameters are not supported by JPA in native queries, only for JPQL. You must use positional parameters.
Named parameters follow the rules for identifiers defined in Section 4.4.1. The use of named parameters applies to the Java Persistence query language, and is not defined for native queries. Only positional parameter binding may be portably used for native queries.
So, use this
Query q = em.createNativeQuery("SELECT count(*) FROM mytable where username = ?1");
q.setParameter(1, "test");
While JPA specification doesn't support named parameters in native queries, some JPA implementations (like Hibernate) may support it
Native SQL queries support positional as well as named parameters
However, this couples your application to specific JPA implementation, and thus makes it unportable.
After many tries I found that you should use createNativeQuery And you can send parameters using # replacement
In my example
String UPDATE_lOGIN_TABLE_QUERY = "UPDATE OMFX.USER_LOGIN SET LOGOUT_TIME = SYSDATE WHERE LOGIN_ID = #loginId AND USER_ID = #userId";
Query query = em.createNativeQuery(logQuery);
query.setParameter("userId", logDataDto.getUserId());
query.setParameter("loginId", logDataDto.getLoginId());
query.executeUpdate();
You are calling setProperty instead of setParameter. Change your code to
Query q = em.createNativeQuery("SELECT count(*) FROM mytable where username = :username");
em.setParameter("username", "test");
(int) q.getSingleResult();
and it should work.
I use EclipseLink. This JPA allows the following way for the native queries:
Query q = em.createNativeQuery("SELECT * FROM mytable where username = ?username");
q.setParameter("username", "test");
q.getResultList();
Use set Parameter from query.
Query q = (Query) em.createNativeQuery("SELECT count(*) FROM mytable where username = ?1");
q.setParameter(1, "test");
This was a bug fixed in version 4.3.11
https://hibernate.atlassian.net/browse/HHH-2851
EDIT:
Best way to execute a native query is still to use NamedParameterJdbcTemplate
It allows you need to retrieve a result that is not a managed entity ; you can use a RowMapper and even a Map of named parameters!
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
#Autowired
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
final List<Long> resultList = namedParameterJdbcTemplate.query(query,
mapOfNamedParamters,
new RowMapper<Long>() {
#Override
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong(1);
}
});

From Hibernate Version 5.3.0 return "ordinal parameter not bound 1"

Am upgrading Hibernate from 5.1.2.Final to 5.4.13. Am facing issue in below code,
#Entity
#NamedNativeQuery(name = "getStudentDetails", resultClass = StudentEntity.class, query = "{call getStudentDetails(?)}")
public class StudentEntity {
private Long id;
private String name;
}
and my DAO class like below,
public List<StudentEntity> getStudentDetails(){
List<StudentEntity> result = null;
try{
Query query = em.createNamedQuery("getStudentDetails");
result = query.getResultList();
}catch(Exception e){
}
return result;
}
create or replace procedure getStudentDetails(p_return_cur OUT SYS_REFCURSOR) is Store procedure with only output parameter.
am not set outparameter in java code. Till Hibernate 5.2.* don't have this issue. When update to 5.3.* it return "ordinal parameter not bound 1".
Positional Parameters are not Supported since 5.3
Support for legacy-style query parameter ('?') declarations in HQL/JPQL queries has been removed. This feature has been deprecated since Hibernate 4.1 and finally removed in 5.3 version.
Therefore, the following query declaration is not valid:
Query<Product> query = OBDal.getInstance().getSession()
.createQuery("from Product as p where p.name = ? and p.stocked = ?", Product.class);
query.setParameter(0, "Ale Beer");
query.setParameter(1, true);
To make the previous query work fine it must use named parameters:
Query<Product> query = OBDal.getInstance().getSession()
.createQuery("from Product as p where p.name = :name and p.stocked = :isStocked", Product.class);
query.setParameter("name", "Ale Beer");
query.setParameter("isStocked", true);
Code sample is taken from http://wiki.openbravo.com/wiki/Hibernate_5.3_Migration_Guide

Query not giving solution in DAO class

when i run my query in database visualizer its working perfectly, but i think there are some issues in syntax when i convert it in my DAO class method.
I want to get whole data against the name provided
In Visualizer:
SELECT first_name,last_name,nic,phone,email FROM x_hr_user where (first_name = 'Irum');
Now in Dao
public List<XHrUser> findXHrUserByNameInTable()
{
String name ="Irum";
Query query = em.createQuery("SELECT xHrNewUserObj.firstName,xHrNewUserObj.lastName, xHrNewUserObj.nic, xHrNewUserObj.phone, xHrNewUserObj.emil FROM XHrUser xHrNewUserObj where (xHrNewUserObj.firstName) = (name)");
List<XHrUser> list = query.getResultList();
return list;
}
Instead of showing single row, it displays whole data Table
Thank you
Your current query is not valid JPQL. It appears that you intended to insert the raw name string into your query, which could be done via a native query, but certainly is not desirable. Instead, use a named parameter in your JPQL query and then bind name to it.
String name = "Irum";
Query query = em.createQuery("SELECT x FROM XHrUser WHERE x.firstName = :name")
.setParameter("name", name);
List<XhrUser> list = query.getResultList();
You have to write query as below. where : is used for variable
Query query = em.createQuery("SELECT xHrNewUserObj.firstName,xHrNewUserObj.lastName, xHrNewUserObj.nic, xHrNewUserObj.phone, xHrNewUserObj.emil FROM XHrUser xHrNewUserObj where (xHrNewUserObj.firstName) = :name");

Querying by inherited property

i am trying to do my first steps in Java, and i am blocked with the next problem: i have this hieararchy class "User -> Traveller".
#Entity
#Inheritance(strategy=InheritanceType.JOINED)
public class User {
// Some properties and accessors.
}
#Entity
#PrimaryKeyJoinColumn(name="user_id")
public class Traveller extends User{
// Some properties and accessors.
}
So, i need search a traveller by your username which is a User class property. I try this in my TravellerDao:
public Traveller findByUsername(String username){
EntityManager em = PersistenceHelper.getEm();
TypedQuery<Traveller> query = em.createQuery("FROM Traveller WHERE username = ?1", Traveller.class);
query.setParameter(1, username);
return query.getSingleResult();
}
But, a javax.persitence.NoResultException is catched. Where is the problem ? In the "where" clausule ? In the join ?. Any ideas ?
Notes:
1) The hierarchy works fine with "find" method. And the database is generated correctly.
2) I have an entry in the Traveller table that links up to one in User table.
3) In the database exists an user with the username that i am using to test.
4) The method get correctly the "username" parameter.
EDIT: Now works. The problem was the execution order of test cases. The query works fine.
I think your query is incorrect, I believe you are forced to name an alias for every entity you want to interact with. Have you tried this?
"SELECT t FROM Traveller t WHERE t.username = ?1", Traveller.class
Also you should have to cast the result when you call query.getSingleResult(). Because you pass in Traveller.class into the createQuery call which is paramaterized by Class<T> resultClass
Edit: Added a 1 after the ? as shown here. http://www.objectdb.com/java/jpa/query/parameter#Ordinal_Parameters_index_
public Country getCountryByName(EntityManager em, String name) {
TypedQuery<Country> query = em.createQuery(
"SELECT c FROM Country c WHERE c.name = ?1", Country.class);
return query.setParameter(1, name).getSingleResult();
}

IN-clause in HQL or Java Persistence Query Language

I have the following parametrised JPA, or Hibernate, query:
SELECT entity FROM Entity entity WHERE name IN (?)
I want to pass the parameter as an ArrayList<String>, is this possible? Hibernate current tells me, that
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
Is this possible at all?
ANSWER: Collections as parameters only work with named parameters like ":name", not with JDBC style parameters like "?".
Are you using Hibernate's Query object, or JPA? For JPA, it should work fine:
String jpql = "from A where name in (:names)";
Query q = em.createQuery(jpql);
q.setParameter("names", l);
For Hibernate's, you'll need to use the setParameterList:
String hql = "from A where name in (:names)";
Query q = s.createQuery(hql);
q.setParameterList("names", l);
in HQL you can use query parameter and set Collection with setParameterList method.
Query q = session.createQuery("SELECT entity FROM Entity entity WHERE name IN (:names)");
q.setParameterList("names", names);
Leaving out the parenthesis and simply calling 'setParameter' now works with at least Hibernate.
String jpql = "from A where name in :names";
Query q = em.createQuery(jpql);
q.setParameter("names", l);
Using pure JPA with Hibernate 5.0.2.Final as the actual provider the following seems to work with positional parameters as well:
Entity.java:
#Entity
#NamedQueries({
#NamedQuery(name = "byAttributes", query = "select e from Entity e where e.attribute in (?1)") })
public class Entity {
#Column(name = "attribute")
private String attribute;
}
Dao.java:
public class Dao {
public List<Entity> findByAttributes(Set<String> attributes) {
Query query = em.createNamedQuery("byAttributes");
query.setParameter(1, attributes);
List<Entity> entities = query.getResultList();
return entities;
}
}
query.setParameterList("name", new String[] { "Ron", "Som", "Roxi"}); fixed my issue

Categories

Resources