D
D
Denis Kuznetsov2019-05-29 19:56:52
Java
Denis Kuznetsov, 2019-05-29 19:56:52

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 
}

there are boxes themselves that have several (or not several) plants and sensors
@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 
}

own plants and sensors
@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() {
    }
//....
}

after I create a repo interface inheriting it from JpaRepo
and everything works fine tables are created (I haven’t done the UI itself yet under the boxes)
I also had a repo user in which I threw my methods in order to search for a user when trying to register by soap and name and in which case say that such a user exists
public interface UserRepo extends JpaRepository<User, Long> {

    User findByUsername(String username);

    User findByEmail(String email);
}

and at this stage everything is fine (there is a UI for registration and authentication)
after that I decided to add a method to the Repo for boxes that would allow me to find the box by user ID (or you can by name) because the userId field is more like a field by which boxes are mapped to the user (indicated in the anatomy)
public interface GrowBoxRepo extends JpaRepository<GrowBox, Long> {
     List<GrowBox> findByUserId(Long userId);
}

and here the whole thing falls down they say there is no property in the growbox for the UserID
here is the stacktrace
Error creating bean with name 'requestMappingHandlerAdapter' defined in class path
resource
: Invocation of init method failed; nested exception ....
Failed to create query for method public abstract java.util.List com.example.auth.repo.GrowBoxRepo.findByUserId(java.lang.Long)! No property userId found for type GrowBox!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property userId found for type GrowBox!
on the one hand, this is because there is no UserReady field in the box, but on the other hand, it is there, it is declared in the anatomy, and if you create another field with the same name, it will show that there are 2 identical fields in the sql table or even such
thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sviato_slav, 2019-05-30
@DennisKingsman

findByResponsibleUserId

O
Orkhan, 2019-05-29
Hasanly @azerphoenix

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!

you sort of get access to the User object, and then the userId. And according to your method, it turns out that you are looking for userId in the growBoxes table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question