Spring Generate JPA Data Queries by Field

I had a method in ProjectRepository that looked something like this:

findByUserID(int userID);

This will be a list of all projects that link to a specific user. Due to other requirements, I now need to refer to the user from the project, and not just the user ID, for example:

@ManyToOne
@JoinColumn(name = "userID", referencedColumnName = "userID")
private User user;

Which was usually:

int userID;

So, I replaced the method:

findByUser(User user);

But this requires an additional call to the database to get the User object for this user ID. Is there a way to use the first method for my new class? If I just save the first method, I get

org.springframework.data.mapping.PropertyReferenceException: No property ID found for type User! Traversed path: Project.user.

Is this possible without writing a custom request?

+4
source share
2

, User :

public class User {
    private int userId;

    public User(int userId) {
        this.userId = userId;
    }
    //getters setters ommitted
}

Project User :

public class Project {
    private User user;

    public Project() {
    }
    //rest ommitted
}

, , project.user.userId,

findByUserUserId(int userId);

, , ,

+3

@PrimaryKeyJoinColumn.

0

Source: https://habr.com/ru/post/1623893/


All Articles