'Can't get products by userId from repository

I have 2 tables. One of them called 'products'

@Data
@Entity
@Table(name = "products")
@NoArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(length = 100)
@NotBlank(message = "Name must be written")
private String name;
@Column(length = 200)
@NotBlank(message = "Provide image (link in this case) of your product")
private String image;
@PositiveOrZero
private int amount;
@Column(length = 250)
@NotBlank(message = "description must be written")
@Size(min = 10, max = 250, message = "description is too long or empty")
private String description;
@PositiveOrZero
private float price;
@ManyToOne
@JoinColumn(name = "type_id")
private ProductType productType;


public Product(@NotBlank String name, String image, int amount, @NotBlank String description, 
@PositiveOrZero float price, ProductType productType) {
this.name = name;
this.image = image;
this.amount = amount;
this.description = description;
this.price = price;
this.productType = productType;
}
}

another table is 'users'

@Data
@Entity
@NoArgsConstructor
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(length = 50)
@Size(min = 2, max = 30, message = "enter appropriate amount of letters, min 2")
private String username;
@Column(length = 100)
@Email(message = "Enter a valid email")
@NotBlank(message = "email should have a value")
private String email;
@Column(length = 50)
@NotBlank(message = "password should have a value")
@Size(min = 6, message = "password should at least consist of 6 characters")
private String password;
private boolean enabled;
private String role;



public User(@Size(min = 2, max = 30, message = "enter appropriate amount of letters, min 2") 
String username,
            @Email(message = "Enter a valid email")
            @NotBlank(message = "email should have a value") String email,
            @NotBlank(message = "password should have a value")
            @Size(min = 6, message = "password should at least consist of 6 characters") 
String password, boolean enabled, String role) {
    this.username = username;
    this.email = email;
    this.password = password;
    this.enabled = enabled;
    this.role = role;
}
}

and also table that include both 'product_user' (many to many relationship)

it looks like this

@Data
@Entity
@Table(name = "product_user")
@AllArgsConstructor
@NoArgsConstructor
public class ProdAndUser{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;

@ManyToOne
@JoinColumn(name = "product_id")
Product product;

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

public ProdAndUser(Product product, User user) {
    this.product = product;
    this.user = user;
}

}

then I tried to get them from prodAndUser repository by UserId or by User as obj:

@Repository
public interface ProdAndUserRepository extends JpaRepository<ProdAndUser, Integer> {
List<ProdAndUser> getProdAndUsersByUserId(Integer id);
List<ProdAndUser> getAllByUser(User user);
}

my controller looks like this:

@ResponseBody
@GetMapping("/findByUsr/{user}")
public List<ProdAndUser> getByUser(@PathVariable User user){
    return prodAndUserRepository.getAllByUser(user);
}

error:

{
 "timestamp": "2022-02-12T05:52:53.165+00:00",
 "status": 404,
 "error": "Not Found",
 "message": "No message available",
 "path": "/Cart/findByUsr"
 }

I have tried to find them all by .findAll() and it worked fine. Also another tables work fine on their own



Solution 1:[1]

Look at the error it says (404) means something is not right with the path. The path on the error does not contain user_id.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafa