【问题标题】:findById(ID) in CrudRepository - attempting to use incompatible return typeCrudRepository 中的 findById(ID) - 尝试使用不兼容的返回类型
【发布时间】:2020-06-08 01:11:12
【问题描述】:

我已经定义了消息实体

@Entity
public class Message {
    @Id
    @GeneratedValue()
    private long id;

    @OneToOne
    @NotNull
    private User user;

    @NotEmpty(message = "text is required")
    private String text;

    @NotEmpty(message = "title is required")
    private String title;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

和 CrudRepository

public interface MessageRepository extends CrudRepository<Message, Long> {


    @PostAuthorize("hasPermission(returnObject, 'message')")
    Message findById(Long id);

    Iterable<Message> findByUserId(Long id);

    @PostAuthorize("hasPermission(returnObject, 'privateMessage')")
    Message findOne(Long id);

}

我收到以下错误 Error

当我使用时

 @PostAuthorize("hasPermission(returnObject, 'message')")
  Optional<Message> findById(Long id);

还是不行。 我明白了,“消息消息 = messageRepository.findById(id);”无法转换,我应该将变量“消息”类型更改为“可选”

当我这样做时,我收到以下错误:

Error creating bean with name 'defaultController': Unsatisfied dependency expressed through field 'messageRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.chapter5.data.Message com.example.chapter5.data.MessageRepository.findOne(java.lang.Long)! No property findOne found for type Message!

有人可以提出替代解决方案吗? 谢谢

【问题讨论】:

  • 删除findOne 方法或添加@Query 以添加查询。那根本行不通。
  • findById(Long id);Message findOne(Long id); 的方法有何不同?你对这两种方法有什么期望?对我来说似乎相同。(通过其 ID 查找消息)

标签: java spring-security spring-data


【解决方案1】:

Spring 数据会将您的方法名称转换为实际的数据库查询。但这不会神奇地发生。 spring当然有魔力,但是你必须遵循语法。

在您的第一种方法中,Message findById(Long id); Spring 知道您需要 Message 对象,并且您需要通过 message_id 找到它。这将其转换为类似于以下内容。

select msg from Message msg where msg.id = ?1;

?1 将被您的方法参数替换。

如果您不希望 Spring 为您生成查询,那么您必须使用 @Query 并提供您自己的自定义查询。(在这种情况下方法名称无关紧要。)

您可以按照here 所述实现此目的。

现在,回到你的错误, Spring 无法理解您的方法Message findOne(Long id);。 因为它期待语法findBy

您可以从spring documentations 中找到有效的方法名称。

我不明白的是,Message findById(Long id);Message findOne(Long id); 方法有何不同。我想他们都试图从 messageId 获取消息。

【讨论】:

  • 您好阿伦,谢谢您的回答。回答您的问题:这有助于尽可能简单地展示两种不同的权利。因此,通过findById 找到的消息可以由当前用户和管理员查看,而通过findOne 找到的消息只能由当前用户查看。
  • 那么在这种情况下,我建议在服务层而不是在 DAO 层上获得这些授权。使用弹簧数据时,您真的不能乱用方法名称。希望有帮助
  • 即使在 DAO 层也归结为这一点。管理员和当前用户都可以访问getMessageById。例如:管理员和当前用户可以访问X,当前用户也可以访问X。这归结为管理员和当前用户都可以访问X
猜你喜欢
  • 2016-11-24
  • 2019-10-15
  • 2019-01-15
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
相关资源
最近更新 更多