Phạm Quốc Bảo
Phạm Quốc Bảo

Reputation: 894

JpaRepository for Java Spring : No property findbyUser found for type User

I have a User class entity:

@Entity
@Table(name = "USERS")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@json-id")
public class User extends AbstractEntity {

  @Id
  @SequenceGenerator(name = "USERS_SEQ", sequenceName = "S_USER_ACCOUNT")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USERS_SEQ")
  @Column(name = "USER_ID", precision = 22, scale = 0)
  private Long userId;

  @Column(name = "ID_TYPE")
  private String idType;

  @Column(name = "COUNTRY_CODE", length = 3, nullable = false)
  @NotBlank
  private String countryCode;
  }
}

and class UserRepository extend from JpaRepository

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

  Page<User> findbyUser(User users, Pageable pageable);


}

when build and complie source, i have a error message:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findbyUser found for type User!

I want to search User with any field in entity. Could you please give your thoughts?

Upvotes: 0

Views: 993

Answers (3)

Phạm Quốc Bảo
Phạm Quốc Bảo

Reputation: 894

Thanks all. I just researched the solution for this answer. It works well.

Example<User> example = Example.of(user); 
Page<User> pageResult = usersRepository.findAll(example, pageable);

Upvotes: 1

eparvan
eparvan

Reputation: 1729

You can use @Query annotation to search by any field, e.g.:

    @Query("SELECT user FROM User user WHERE (:userId IS NULL OR user.userId = :userId) " +
            "AND (:idType IS NULL OR user.idType = :idType) " +
            "AND (:countryCode IS NULL OR user.countryCode = :countryCode) ")
    Page<User> search(@Param("userId") Long userId, @Param("idType") String idType,
                                        @Param("countryCode") String countryCode, Pageable pageable);

Search by countryCode:

    userRepository.search(null, null, "US", new PageRequest(page, pageSize));

Search by type and countryCode:

    userRepository.search(null, "someType", "US", new PageRequest(page, pageSize));

Search by id:

    userRepository.search(1L, null, null, new PageRequest(page, pageSize));

Upvotes: 0

Alexandru Hodis
Alexandru Hodis

Reputation: 372

I would suggest you to make 3 different methods:

Page<User> findAllByUserId(Long userId, Pageable pageable);

Page<User> findAllByIdType(String idType, Pageable pageable);

Page<User> findAllByCountryCode(String countryCode, Pageable pageable);

In @Service, get the content from all pages and merge them into one Page<User>

Note that those methods could return a list of users and your @Controller can return a Page<User>.(this could make the the build up for the @Controller response lighter and easier)

Upvotes: 0

Related Questions