当前位置: 首页 > 知识库问答 >
问题:

在springboot中找不到类型错误的属性

齐财
2023-03-14

在尝试创建一对多(以及多对一)关系时,我遇到以下错误:

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

以下是完整的故事。目前在这些课程中,我有以下几点:

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 ...

用户角色。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 ...

用户角色存储。JAVA

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

开始atabase.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
                        )
                ));
            }
        };
    }
}

我得到的全部错误是:

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'!

我不完全确定在哪里或为什么它试图找到一个属性的id时,我显然已经设置了所有类型的UserRole。

共有2个答案

轩辕煌
2023-03-14

在UserRoleRepository接口中,正如您所提到的,方法名为findUserRoleById,根据JPA惯例,此方法将在UserRole中查找id列,并尝试将其与作为@Param传递的roleId匹配。但是,UserRole类没有id属性,因此您将面临错误。

你有两个选择

>

  • 可以将方法名更改为

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

    因为您在UserRole@Entity中定义的属性名是roleId,而不是id。

    如果您不想更改方法名称,您可以使用@Query注释像这样注释方法

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

  • 柯国安
    2023-03-14

    问题在于findUserRoleById,因为spring与确切的字段名匹配,所以必须输入roleId-

     类似资料:
    • 我正在尝试创建一个简单的eureka服务和客户端程序,并在其上启用hystrix。但我在代码上发现了这个错误

    • 我在Spring网站上跟踪这个SpringBoot演示,学习如何创建一个接受上传的文件。我收到一条错误消息: 我试图通过向FileUploadController添加注释来修复它。 我已尝试将以下依赖项添加到build.gradle 注意:我不想将数据库连接到此演示项目。这个演示确认它不是在这个演示中尝试连接数据库,而是在生产环境中尝试连接数据库。 我尝试在Storage Service接口上方添

    • 当我调用服务时,它会给出以下错误:- 我已经基于相同的设计模式构建了REST API,它对我来说工作得很好。我没有得到的东西,为什么它说“没有财产日期找到类型现金流!”?实体中没有日期属性。我尝试删除数据库并重新启动,但没有成功。我错过了什么?

    • 学习Typescript并尝试构建一个简单的文本框,在提交文本后显示文本。 我能够很好地捕捉输入,现在试图将其报告给用户。 问题出在编译类型上。脚本给出了以下错误消息:类型“htmldevelment”上不存在属性“value”。 我只是想分配div messageList的值来打印数组(现在还不关心格式)。 我一直在网上搜索,我能找到的大部分都说一定是命名错误,我尝试将名称更改为1,但仍然没有乐

    • 有什么想法吗?(角2)