Answer the question
In order to leave comments, you need to log in
Why can't I add a method to the interface that inherits from JpsRepo?
Hello, I'm trying to add some logic to the interface for managing entities , the
logic was such that first the user registers, then on the personal page he has the opportunity to add a box and see all his boxes (initially he does not have them)
he also has the ability to edit or delete box, when editing the box, he has the ability to change its parameters or add sensors or plants there (initially they are not there)
I had a User who has roles (in my case, only one), and in theory there should be boxes
@Entity
@Table(name = "usr")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String email;
private boolean active;
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "responsibleUser", cascade = CascadeType.ALL)
private List<GrowBox> growBoxes;
public User() {
}
// getters, setters
}
@Entity
@Table(name = "growBoxes")
public class GrowBox {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
private Integer length;
private Integer width;
private Integer height;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "responsibleGrowBox", cascade = CascadeType.ALL)
private List<Plant> plants;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "activeGrowBox", cascade = CascadeType.ALL)
private List<Sensor> sensors;
@ManyToOne
@JoinColumn(name = "user_id")
private User responsibleUser;
public GrowBox() {
}
// Getters, setters
}
@Entity
@Table(name = "plants")
public class Plant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String plantName;
private Integer num;
private String status;
@ManyToOne
@JoinColumn(name = "gb_id")
private GrowBox responsibleGrowBox;
public Plant() {
}
//...
}
@Entity
@Table(name = "sensors")
public class Sensor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String sensorName;
private Integer value;
@ManyToOne
@JoinColumn(name = "agb_id")
private GrowBox activeGrowBox;
public Sensor() {
}
//....
}
public interface UserRepo extends JpaRepository<User, Long> {
User findByUsername(String username);
User findByEmail(String email);
}
public interface GrowBoxRepo extends JpaRepository<GrowBox, Long> {
List<GrowBox> findByUserId(Long userId);
}
Answer the question
In order to leave comments, you need to log in
List<GrowBox> findByUserId(Long userId);
the first thing you'll see is that you want to get a List. If my memory serves me, then I need to use List<GrowBox> findAllByUserUserId(Long userId)
if my memory serves me, then it should be something like this
, otherwise it turns out that you want to get one object by userId, but at the same time you want to get a List
or you can do the following:
in this case you need to pass the user to the method, not his id.
By the way, usually the IDE tells you how it should be...
No property userId found for type GrowBox!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property userId found for type GrowBox!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question