Home > Mobile >  Spring Repository Update - No property found for type
Spring Repository Update - No property found for type

Time:01-25

I have searched on StackOverflow a solution for this on the other discussions about this same error, but couldn't find an answer for this specific scenario.

Here is what's happening.

I have the following Spring Repository.

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

       @Modifying
       @Query("update User u set u.userDesc = :userDesc where u.userName = :userName")
       public int updateUser(String userDesc, String userName);
}

Getting this error.

Could not create query for public abstract int com.xyz.UserRepository.updateUser(java.lang.String,java.lang.String); Reason: Failed to create query for method public abstract int com.xyz.UserRepository.updateUser(java.lang.String,java.lang.String)! No property 'updateUser' found for type 'User';

Why?

CodePudding user response:

Probably not related, since the exception is from JPA and not JDBC, but you are using a class named User as your data class name and table name as I can see from the query. Depending on what underlying database you are using, you could be running into conflicts relating to restricted keywords of that database.

For example, in newer versions of H2 the USER keyword is restricted. https://www.h2database.com/html/advanced.html

On another note, if you run a clean SpringBoot project with the following:

import jakarta.persistence.*;

@Entity
public class UserTest {
    
    public UserTest(){}

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", nullable = false)
    public Long id;

    public String userDesc;

    public String userName;

}

import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
@Transactional
public interface UserRepository extends CrudRepository< UserTest, Long> {

    @Modifying
    @Query("update UserTest u set u.userDesc = :userDesc where u.userName = :userName")
    public int updateUser(String userDesc, String userName);
}

It does run and the update method works just fine, so the issue must be somewhere else in your code, or in your imports which you are not showing.

  • Related