iamjoshua
iamjoshua

Reputation: 1269

How to Search for a [FieldName] in Spring Data JPA

I'm a newbie to Spring framework. I'm stuck at the basic CRUD functionalities. What I was trying to do is search for a username and then determine whether if the password entered is correct. For now I'm trying to at least just find for specific username in my Entities. How do I do that in Spring JPA?. I could only search through using ID's, but not FieldNames. I wanted to implement it like this in Service. List findCredentialsByUsername(String username);

And call this Service in my controller to which I would search for a username using Postman/browser url.

The method in the Controller would then have something like this credentialsRepository.findCredentialsByUsername(username); That is not using Queries, just the basic JPA methods.

And whenever that is found,it would return true or at least give me details with that username.

I have the following Entity.

@Entity
public class Credentials {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;

@Column
String username;
@Column
String password;
@Column
String firstname;
@Column
String lastname;

public Long getId() {
    return id;
}

//ToString , Getters and Setters
//Needed for returning the changes on POSTMAN
@Override
public String toString() {
    return "Credentials{" +
            "username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", firstname='" + firstname + '\'' +
            ", lastname='" + lastname + '\'' +
            '}';
}

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

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

}

My Controllers, Services and Repository is quite a mess.I can't quite get the hang of building blocks of this thing Spring data JPA?. I've been crawling the web for days for this basic building blocks for JPA using CRUD. :/

PS:

The password is unencrypted on DB. For a basic Spring JPA, I just wanted to do a basic Search / CRUD if possible. No Web UI is created, I'm just doing the testing of my CRUD functionalities via Postman

Upvotes: 0

Views: 5931

Answers (1)

Djamel Korei
Djamel Korei

Reputation: 774

You are looking for query methods just use the keyword findBy fallowed by the field name

Example : findByUsername(String username)

Query methods section for more information

So you just have to create an interface that extends JpaRepository or CrudRepository :

@Repository
public interface CredentialsRepository extends JpaRepository<Credentials, Long> {

  Optional<Credentials> findByUsername(String username);

}

Then your controller :

@RestController
public class CredentialsController {

  @Autowired
  CredentialsRepository credentialsRepository;

  @GetMapping("/check/{username}")
  public String check(@PathVariable("username") String username) {

   Optional<Credentials> credentials = CredentialsRepository.findByUsername(username);

   return credentials.isPresent() ? "exist" : "doesn't exist"; 

  }

}

Upvotes: 4

Related Questions