Annotations are not allowed here

Viewed 56077

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

7 Answers

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

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();

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

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.

In my case, I was trying to use it in the constructor...

Related