Spring boot JPA nativeQuery Incorrect syntax near #P0 - java

I am trying to run the following query from a spring boot app using Hibernate, and the problem is that in the query I need to pass the path variable from the method.
#Query(
value = "INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK ?, SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);",
nativeQuery = true)
Integer insertImage(String path);
the error I am getting is
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '#P0'.
If I write the path manually it will work
#Query(
value = "INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK 'C:\\pic\\NORMAL2-IM-1257-0001.jpeg', SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);",
nativeQuery = true)
Integer insertImage(String path);

'#P0' is your RowCountToDisplay parameter ... Maybe try to place parenthesis around the argument
INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK (?), SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);"
, nativeQuery = true)

Related

Why does JPA give me a syntax error at or near?

I am learning spring-boot and I have a connection to a database, I am trying to perform the following query.
#Query(value = "select f.entity_id as 'NIT' from renting.fine f where f.companyparent = :companyParent", nativeQuery = true)
But it generates the following error about the value that starts with a single quote, I can't get it to recognize the value
org.postgresql.util.PSQLException: ERROR: syntax error at or near "'NIT'"
Remove single quotes around 'NIT'. In order to define aliases for column you don't require quotes at all.
#Query(value = "select f.entity_id as NIT from renting.fine f where f.companyparent = :companyParent", nativeQuery = true)
This should work.

Filter JSON column from MySQL table in spring boot

Below is my query to filter the json column in MySQL table ,
public interface MpaCustomizationRepository extends
JpaRepository<MpaCustomization, Serializable> {
#Query(nativeQuery = true, value = "select * from MPA_CUSTOMIZATION where json_contains(domain_based_properties, '{\"id\" : ?1}')")
MpaCustomization findByDomainBaseId(String domainId);
}
above method throws below SQL error,
but if I hard code the variable and run the query like below its works fine,
#Query(nativeQuery = true, value = "select * from MPA_CUSTOMIZATION where json_contains(domain_based_properties, '{\"id\" : 2}')")
MpaCustomization findByDomainBaseId(String domainId);
How to fix this issue?
Use $.id for values when using native query, as SQL accepts like it only.
Other way, you may use :id and bind parameter {\"id\":\"" + <value> + "\"} to the query.
Maybe the following will work.
#Query(nativeQuery = true, value = "select * from MPA_CUSTOMIZATION where json_contains(domain_based_properties, ?1 , '$.id'")
MpaCustomization findByDomainBaseId(String domainId);
Source

native query unable to set parameter while creating partition

I have the following query to create a partition, it's working with hardcoded values but is unable to replace parameters dynamically.
#Modifying
#Query(value = "CREATE TABLE :name PARTITION OF sample_table\n"
+ "FOR VALUES FROM (a) TO (b)", nativeQuery = true)
void createPartition(#Param("name") String name, #Param("a") long a, #Param("b") long b);
This statement I want to generate which is working fine on postgres console:
CREATE TABLE sample_table_p1 PARTITION OF sample_table
FOR VALUES FROM ('1020000') TO ('2000000');
error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
Thanks in advance :)

Bind parameter to a native query annotation JPA

I have the following piece of code:
#Query(value = "select * from james_mail where encode(header_bytes, 'escape') like '%Message-ID: :messageId%'", nativeQuery = true)
List<JamesMail> findByMessageIdFromHeader(#Param(value = "messageId") String messageId);
The expected behavior is to execute the query with bind parameter (e.g. messageID)
Actual behavior is that the query is executed as it is, without biding
What is the solution for actually biding, or there is an workaround for my problem?
Technology stack:
Spring Boot, JPA, Hibernate
here you are trying to encode header_bytes which is contained table james_mail right
and you want to find rows which contains value matching to messageId and contains Message-ID
#Query(value = "select * from james_mail where encode(header_bytes, 'escape') like '%Message-ID%' and encode(header_bytes, 'escape') like %:messageId%", nativeQuery = true)
List<JamesMail> findByMessageIdFromHeader(#Param(value = "messageId") String messageId);
Try to correct your query in this way:
#Query(value = "select * from james_mail where encode(header_bytes, 'escape') like '%Message-ID: ' || :messageId || '%'", nativeQuery = true)
List<JamesMail> findByMessageIdFromHeader(#Param(value = "messageId") String messageId);

JpaRepository does not read string parameter in a native query

I am facing a problem with my JpaRepository in a spring boot application
I want to perform a simple update query on my database, but it turns out that the native query is quite annoying, please help
public interface ImageRepository extends JpaRepository<Image, Integer> {
#Modifying
#Transactional
#Query(value = "UPDATE image SET path =(0?), status = (1?) WHERE Id = (2?)", nativeQuery = true)
void update(String path ,String status,int Id);
}
the code above returns the following error message
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '#P0'.
I have tried to change SQL dialect to
org.hibernate.dialect.SQLServer2008Dialect
org.hibernate.dialect.SQLServer2012Dialect
respectively and non of them worked.
I tried also to write the query in a different way which does not give me an error but it does not update the fields. it can detect the integer value from the method but it will set the string values to an emply value:
#Query(value = "UPDATE image SET physical_path =(0), status = (1) WHERE Id = (2)", nativeQuery = true)
If anyone has faced the same issue please support
Frame the query like this :
#Query(value = "UPDATE image i SET path =:path, status = :status WHERE i.Id = :Id", nativeQuery = true)
void update(#Param("path") String path , #Param("status") String status, #Param("Id") int Id);
For positional parameters :
#Query(value = "UPDATE image i SET path = ?1, status = ?2 WHERE i.Id = ?3", nativeQuery = true)
void update(String path , String status, int Id);
From the Spring Data JPA - Reference you can see that the parameters (in the way you want to use them) are defined like -> ?1, ?2 etc..
Also, please keep in mind that the JPQL syntax is slightly different than plain sql.
#Modifying
#Query("update Image i set i.path = ?1 where i.status = ?2 where i.id = ?3")
void update(String path, String status, int id);

Categories

Resources