0

I have the following JPA models:

Issue

@Entity
public class Issue {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String title;
private String text;

@ManyToOne
@JoinColumn(name="user_id")
private User user;

public Issue() {}

public Issue(String title, String text) {
    this.title = title;
    this.text  = text;
}

public long getId() {
    return id;
}

public String getTitle() {
    return title;
}

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

public String getText() {
    return text;
}

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

public User getUser() {
    return user;
}

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

@Override
public String toString() {
    return "Issue [id=" + id + ", title=" + title + ", text=" + text + ", user=" + user + "]";
}
}

User

@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String username;
private String firstname;
private String lastname;

public User() {}

public User(String username) {
    this.username = username;
}

public String getUsername() {
    return username;
}

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

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;
}

@Override
public String toString() {
    return "User [id=" + id + ", username=" + username + ", firstname=" + firstname + ", lastname=" + lastname
            + "]";
}
}

And an Issue repository that extends PagingAndSortingRepository and contains the method List<Issue> findByUser(User user); See below:

public interface IssueRepository extends PagingAndSortingRepository<Issue,Long> {
    List<Issue> findByUser(User user);
}

I'm trying to find a way to navigate these relationships with HTTP calls, namely how do I call findByUser(User user) and get all the issues for that user?

Using the following call I can execute that particular query:

GET http://localhost:8080/issues/search/findByUser

But I'm unclear what I should be providing as the User? Do I send the id as a query param? Do I construct an object and send that as a query param? Am I just modeling this the wrong way?

I'd like to get back a JSON list containing all the Issues for this particular User.

Thanks in advance for any help or guidance.

jerney
  • 2,187
  • 1
  • 19
  • 31

3 Answers3

1

Changing the repository to this solved the issue. The key is to do the lookup based on a field of the User, not the User itself.

public interface IssueRepository extends PagingAndSortingRepository<Issue,Long> {
    List<Issue> findByUserUsername(@Param("username") String username);
}

GET http://localhost:8080/issues/search/findByUserUsername?username=jerney

This returns a list of issues.

jerney
  • 2,187
  • 1
  • 19
  • 31
0

put another read only column like @Column(name = "user_id", insertable = false, updatable = false) private Long userId; in Issue entity and use findByUserId(Long userId) repo method to find it and pass userId parameter(i.e path varible) to controller to do this using http calls.

0

You can use a simple findOne(Long userId) if you need only one record as its probably faster than query by string field

Zeromus
  • 4,472
  • 8
  • 32
  • 40