代码之家  ›  专栏  ›  技术社区  ›  TheCrunchyPotato

JPA查询找不到正确的构造函数

  •  0
  • TheCrunchyPotato  · 技术社区  · 3 年前

    带@Query注释

    @Query("SELECT new com.project.model.user(u.id, u.username, u.profile.last_online, u.profile.about) FROM User u WHERE id = :id)
    

    和构造函数

    User(long id, long username, Profile profile) {
       this.id = id;
       this.username = username;
       this.profile = profile;
    }
    

    它仍然无法真正找到构造函数,并且说没有“匹配的构造函数”,有没有更好的方法来创建构造函数?

    1 回复  |  直到 3 年前
        1
  •  2
  •   Ausgefuchster    3 年前

    查询中预期的构造函数具有 (long, String, Timestamp, String) . 你的构造器需要 (long, long, Profile) .

    public User(long id, String username, Timestamp lastOnline, String about) {
        this.id = id;
        this.username = username;
        this.profile = new Profile(lastOnline, about); // create new Profile with lastOnline & about
    }
    

    这应该行得通。

    JpaRepository#findById(Long id)