0Day Forums
Annotations are not allowed here - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: FrameWork (https://zeroday.vip/Forum-FrameWork)
+---- Forum: Spring (https://zeroday.vip/Forum-Spring)
+---- Thread: Annotations are not allowed here (/Thread-Annotations-are-not-allowed-here)



Annotations are not allowed here - chanhrkmacys - 08-02-2023

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


RE: Annotations are not allowed here - gey860 - 08-02-2023

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


RE: Annotations are not allowed here - saltworks229234 - 08-02-2023

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](

[To see links please register here]

)

[JPQL Language Reference](

[To see links please register here]

)

[SQL Tutorial](

[To see links please register here]

)

P.S. Annotation `@Repository` is not necessary



RE: Annotations are not allowed here - rowney708 - 08-02-2023

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.


RE: Annotations are not allowed here - anoperineal726810 - 08-02-2023

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


RE: Annotations are not allowed here - tetanical31264 - 08-02-2023

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


RE: Annotations are not allowed here - willaminaoqgfjo - 08-02-2023

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


RE: Annotations are not allowed here - frazzling778705 - 08-02-2023

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