Answer the question
In order to leave comments, you need to log in
Do you get a 500 error when trying to retrieve data from a linked table?
Essence of the question:
There is an application in which users can save recipes. There are two tables:
1 - recipes
2 - users
Recipes have a user_id column by which it is associated with the user who saved this recipe. It is necessary to make sure that each user sees only those recipes that he added himself and does not see the recipes of other users.
Here is my code, it throws an error
type=Internal Server Error, status=500
SQLGrammarException: could not extract ResultSet
What is wrong in my code to implement what I intended?
My controller:
@GetMapping("/recipes/user/{id}")
public String recipesView(@AuthenticationPrincipal User user, @PathVariable(value = "id") long id, Model model) {
long userId = user.getId();
Iterable<Reciepe> recipes = reciepeRepo.getRecipesForUser(userId);
model.addAttribute("reciepes", recipes);
model.addAttribute("user", user.getUsername());
return "recipes";
}
public interface ReciepeRepo extends JpaRepository<Reciepe, Long> {
@Modifying
@Transactional
@Query(value = "select r from reciepes_model as r where r.user_id=:user_id", nativeQuery = true)
Iterable<Reciepe> getRecipesForUser(@Param("user_id") long id);
}
public interface UserRepo extends JpaRepository<User, Long> {
User findByUsername(String name);
}
@Entity
@Table(name = "reciepes_model")
public class Reciepe {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String reciepe;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
private String info;
private int weight, calories;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getReciepe() {
return reciepe;
}
public void setReciepe(String reciepe) {
this.reciepe = reciepe;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
this.calories = calories;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Reciepe() {
}
public Reciepe(String name, String reciepe, int weight, int calories, String info, User user) {
this.name = name;
this.reciepe = reciepe;
this.weight = weight;
this.calories = calories;
this.info = info;
this.user = user;
}
}
@Entity
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "roles")
private Set<Role> roles = new HashSet<>();
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
private Collection<? extends GrantedAuthority> getRoles() {
return roles;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return getRoles();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
public Long getId() {
return id;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question