In order to insert a record into the database, I was wondering to use SQL insert in order to entityManager.persist, as I would like to provide directly the foreign keys.
So I was using a Named Query.
@NamedQuery(name="insertQuery",
query="INSERT INTO person (person_id, name) VALUES (:person_id, :name)")
But at compilation time , I got this error:
Error in named query: insertQuery: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: VALUES
In fact, the solution is to use a Native Query.
So I needed to change the @NamedQuery into @NamedNativeQuery.
As I should setParameters, I should use the createNamedParameter. Otherwise, I will have the error :
could not locate named parameter.
So my query should look like this.
Query query = entityManager.createNamedQuery("insertQuery")
query.setParameter("person_id", person.getId());
So I was using a Named Query.
@NamedQuery(name="insertQuery",
query="INSERT INTO person (person_id, name) VALUES (:person_id, :name)")
But at compilation time , I got this error:
Error in named query: insertQuery: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: VALUES
In fact, the solution is to use a Native Query.
So I needed to change the @NamedQuery into @NamedNativeQuery.
As I should setParameters, I should use the createNamedParameter. Otherwise, I will have the error :
could not locate named parameter.
So my query should look like this.
Query query = entityManager.createNamedQuery("insertQuery")
query.setParameter("person_id", person.getId());
This comment has been removed by a blog administrator.
ReplyDelete