Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 402 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Annotations are not allowed here

#1
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
Reply

#2
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
Reply

#3
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
Reply

#4
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.
Reply

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

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

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

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through