Andrey-2310
Andrey-2310

Reputation: 571

Annotations are not allowed here

I'm creating a UserRepository which implements JpaRepository and works with User entity. I need a method to update username. I decided to do it with the help of @Query. But it's marked by Intellij. Here is my code for repository:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

@Modifying
@Query(value = "update User user set user.name= %?username"))
void updatingUser(@Param(value = "username") String username);
}

And for Entity:

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Long id;

@NotNull
@Column(name = "user_name")
private String username;
}

And I faced with such a problem: "Annotations are not allowed here" says Intellij Idea and marks the line with @Query annotation. And it made me confused

Upvotes: 36

Views: 76689

Answers (6)

David Liu
David Liu

Reputation: 211

In my case, I accidently typed an ';' at the end of the @Query sentence.

@Query("SELECT a FROM Author a")
@QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value= "" + Integer.MIN_VALUE));
Stream<Author> streamAll();

After I removed the ';' from the @QueryHints, then it worked perfectly.

@Query("SELECT a FROM Author a")
@QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value= "" + Integer.MIN_VALUE))
Stream<Author> streamAll();

Upvotes: 4

Ananay Gupta
Ananay Gupta

Reputation: 385

Sorry this was 4 years too late, but you have an extra ')' at the end of your @Query line

Upvotes: 2

Lukenzo
Lukenzo

Reputation: 1497

In my case, I accidentally typed ";" at the end of @Query. Might help someone else.

Upvotes: 137

Shahzeb Khan
Shahzeb Khan

Reputation: 3642

Well, I got this issue with the following query after copying from SQLyog.

SELECT  id FROM (SELECT * FROM departments ORDER BY id) dpt_sorted, (SELECT @pv := '2') initialisation WHERE   FIND_IN_SET(parent_dept, @pv) AND LENGTH(@pv := CONCAT(@pv, ',', id));

But when i typed the query, the error message disappeared. May be copy and paste issue in my case. This could be helpful.

Upvotes: 0

Cepr0
Cepr0

Reputation: 30329

Using the @Query annotation in this place is absolutely appropriate. But your query is not quite correct.

Query parameters can be named - ':name' or indexed - '?1'.

You should use update queries with 'where' clause otherwise you will update all records.

You code should be like this:

@Modifying
@Query("update User u set u.name = ?1 where u.name = ?2")
void updateByName(String value, String name);

Or like this:

@Modifying
@Query("update User u set u.name = :value where u.name = :name")
void updateByName(@Param("value") String value, @Param("name") String name);

Pay attention - while the 'name' field is not unique you will update ALL users with given 'name'. To update only one user you must use unique field or primary key in the 'where' clause, for example:

@Modifying
@Query("update User u set u.name = ?1 where u.id = ?2")
void updateById(String value, Long id);

By the way if you need to update all user which have specific name pattern - use 'like' operator:

@Modifying
@Query("update User u set u.name = ?1 where u.name like '%'||?2||'%'")
void updateByNameLike(String value, String name);

Useful info:

Spring Data JPA - Reference Documentation

JPQL Language Reference

SQL Tutorial

P.S. Annotation @Repository is not necessary

Upvotes: 0

Andrey-2310
Andrey-2310

Reputation: 571

Sorry, guys. It was so stupid for me to write double quotes in the end of this line. But I actually don't understand why Intellij didn't notice that but started to mark this line with another mistake

Upvotes: 15

Related Questions