Home > Back-end >  No property found for type error in springboot
No property found for type error in springboot

Time:04-10

I'm getting the following error when trying to create a one to many (and many to one) relationship:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'id' found for type 'UserRole'!

Here's the full story. I have the following at the moment in these classes:

LoginUser.java

@Entity
@Table(name = "table_users")
public class LoginUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id", nullable = false, unique = true, updatable = false)
    private int userId;

    @Column(name = "first_name", nullable = false)
    private String firstName;

    @Column(name = "last_name", nullable = false)
    private String lastName;

    @Column(name = "username", nullable = false, unique = true)
    private String username;

    @JsonIgnore
    @Column(name = "password", nullable = false)
    private String password;

    //@Column(name = "user_role", nullable = false)
    @ManyToOne
    @JoinColumn(name = "role_id", referencedColumnName = "role_id", nullable = false)
    private UserRole userRole;

    @Column(name = "is_active", nullable = false)
    private boolean isActive;

    public LoginUser() {}

    public LoginUser(
            String firstName,
            String lastName,
            String username,
            String password,
            UserRole userRole,
            boolean isActive
    ) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.username = username;
        this.password = password;
        this.userRole = userRole;
        this.isActive = isActive;
    }

... Getters and Setters ...

UserRole.java

@Entity
@Table(name = "lu_user_roles")
public class UserRole {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @PrimaryKeyJoinColumn
    @Column(name = "role_id")
    private int roleId;

    @Column(name = "role_title", nullable = false, unique = true, updatable = false)
    private String roleTitle;

    @OneToMany(mappedBy = "userRole")
    private List<LoginUser> loginUsers;

    public UserRole() {}

    public UserRole(String roleTitle) {
        this.roleTitle = roleTitle;
    }

... Getters and Setters ...

UserRolesRepository.java

public interface UserRolesRepository extends CrudRepository<UserRole, Integer> {
    UserRole findUserRoleById(@Param("role_id") Integer roleId);
}

StartDatabase.java

@Configuration
public class StartDatabase {
    private static final Logger LOG = LoggerFactory.getLogger(StartDatabase.class);

    @Autowired UserRolesRepository userRolesRepository;

    @Bean
    CommandLineRunner initUserRolesTable(UserRolesRepository repository) {
        return args -> {
            if(((List<UserRole>) repository.findAll()).size() == 0) {
                LOG.info("Adding "   repository.save(new UserRole("Developer")));
                LOG.info("Adding "   repository.save(new UserRole("Admin")));
                LOG.info("Adding "   repository.save(new UserRole("User")));
            }
        };
    }

    @Bean
    CommandLineRunner initUserTable(LoginUserRepository repository) {
        return args -> {
            if(((List<LoginUser>) repository.findAll()).size() == 0) {
                LOG.info("Preloading "   repository.save(
                        new LoginUser(
                                "Jamie",
                                "Edwards",
                                "jamie.edwards",
                                "password1",
                                userRolesRepository.findUserRoleById(1),
                                true
                        )
                ));
            }
        };
    }
}

The full errors I'm getting are:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRolesRepository' defined in com.jre.assetregister.database.repositories.loginuser.UserRolesRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.jre.assetregister.database.entities.loginuser.UserRole com.jre.assetregister.database.repositories.loginuser.UserRolesRepository.findUserRoleById(java.lang.Integer)! Reason: Failed to create query for method public abstract com.jre.assetregister.database.entities.loginuser.UserRole com.jre.assetregister.database.repositories.loginuser.UserRolesRepository.findUserRoleById(java.lang.Integer)! No property 'id' found for type 'UserRole'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.jre.assetregister.database.entities.loginuser.UserRole com.jre.assetregister.database.repositories.loginuser.UserRolesRepository.findUserRoleById(java.lang.Integer)! No property 'id' found for type 'UserRole'!


Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.jre.assetregister.database.entities.loginuser.UserRole com.jre.assetregister.database.repositories.loginuser.UserRolesRepository.findUserRoleById(java.lang.Integer)! Reason: Failed to create query for method public abstract com.jre.assetregister.database.entities.loginuser.UserRole com.jre.assetregister.database.repositories.loginuser.UserRolesRepository.findUserRoleById(java.lang.Integer)! No property 'id' found for type 'UserRole'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.jre.assetregister.database.entities.loginuser.UserRole com.jre.assetregister.database.repositories.loginuser.UserRolesRepository.findUserRoleById(java.lang.Integer)! No property 'id' found for type 'UserRole'!


Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract com.jre.assetregister.database.entities.loginuser.UserRole com.jre.assetregister.database.repositories.loginuser.UserRolesRepository.findUserRoleById(java.lang.Integer)! No property 'id' found for type 'UserRole'!


Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'id' found for type 'UserRole'!

I'm not entirely sure where or why it's trying to find a property of id when I clearly have set all types of UserRole.

CodePudding user response:

the problem is in your findUserRoleById because spring match the exact field name and you have to put roleId -> findUserRoleByRoleId

CodePudding user response:

In UserRoleRepository interface, as you mentioned the method name is findUserRoleById, according to JPA conventions this method will look for an id column in UserRole and try to match it with roleId you're passing as @Param. But, the UserRole class has no id property and hence you're facing the error.

You've got two options

  1. You can change the method name to

    findUserRolebyRoleId(@Param("role_id") Integer roleId)
    

    as that's the property name you defined in UserRole @Entity is roleId and not id.

  2. If you don't want to change the method name you can annotate the method with @Query annotation like this

    @Query("FROM UserRole ur WHERE ur.roleId = :role_id")
    public interface UserRolesRepository extends CrudRepository<UserRole, Integer> {
     UserRole findUserRoleById(@Param("role_id") Integer roleId);
    }
    
  • Related