spring 这里不允许注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45526647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Annotations are not allowed here
提问by Andrey-2310
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:
我正在创建一个实现 JpaRepository 并与用户实体一起使用的 UserRepository 。我需要一种方法来更新用户名。我决定在@Query 的帮助下完成它。但它是由Intellij 标记的。这是我的存储库代码:
@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
我遇到了这样一个问题:“这里不允许使用注释”Intellij Idea 说,并用 @Query 注释标记了这一行。这让我很困惑
回答by Lukenzo
In my case, I accidentally typed ";" at the end of @Query. Might help someone else.
就我而言,我不小心输入了“;” 在@Query 的末尾。可能会帮助别人。
回答by Andrey-2310
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
对不起大家。在这一行的末尾写双引号对我来说太愚蠢了。但我实际上不明白为什么 Intellij 没有注意到这一点,而是开始用另一个错误标记这一行
回答by Cepr0
Using the @Queryannotation in this place is absolutely appropriate. But your query is not quite correct.
@Query在这个地方使用注解是绝对合适的。但是您的查询并不完全正确。
Query parameters can be named - ':name' or indexed - '?1'.
查询参数可以命名 - ':name' 或索引 - '?1'。
You should use update queries with 'where' clause otherwise you will update all records.
您应该使用带有“where”子句的更新查询,否则您将更新所有记录。
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:
请注意 - 虽然“名称”字段不是唯一的,但您将使用给定的“名称”更新所有用户。要仅更新一个用户,您必须在“where”子句中使用唯一字段或主键,例如:
@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:
顺便说一句,如果您需要更新具有特定名称模式的所有用户 - 使用“like”运算符:
@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
P.S. Annotation @Repositoryis not necessary
PS注解@Repository不是必须的
回答by Shahzeb Khan
Well, I got this issue with the following query after copying from SQLyog.
好吧,从 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 copyand pasteissue in my case. This could be helpful.
但是当我输入查询时,错误信息消失了。在我的情况下可能是copy和paste问题。这可能会有所帮助。

