3

I'm working on a spring boot application and i'm using JPA and hibernate.
I'm facing a problem with the mapping relation between two entities User and Evaluation.
the main idea is to evaluate a user via another one (evaluaters)

User.java

public class User implements Serializable {

    private static final long serialVersionUID = 1209816669845063949L;
    @Id
    @GeneratedValue
    private int idUser;
    private String login;
    private String password;
    @ManyToMany(mappedBy = "listUsers")
    private List<ProjectGroup> listProjectGroup;

    @OneToOne(mappedBy = "EvaluatedUser")
    private Evaluation evaluation1;
    @OneToOne(mappedBy = "Evaluater")
    private Evaluation evaluation;
}

Evaluation.java

public class Evaluation {

    @Id
    @GeneratedValue
    private int idEvaluation;
    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "evaluateduser_fk")
    private User EvaluatedUser;
    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "evaluater_fk")
    private User Evaluater;
    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "project_fk")
    private Project project;

    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "notes_fk")
    private Notes notes;
    }

EvaluationController.java
I'm using this method to return all the evaluations

@GetMapping(path = "all-evaluation", produces = {"application/json" })
    public ResponseEntity<List<Evaluation>> getAllEvaluations() {
        List<Evaluation> list = evaluationService.findAll();
        return new ResponseEntity<List<Evaluation>>(list, HttpStatus.OK);
    }

The problem is, i can't evaluate the same user twice, i think the problem is with my relation OneToOne
This is the exception i'm getting while returning the json :

There was an unexpected error (type=Internal Server Error, status=500). More than one row with the given identifier was found: 1, for class: com.talan.entity.Evaluation; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.talan.entity.Evaluation

Any help would be apreciated

famas23
  • 2,072
  • 4
  • 17
  • 53
petrucci
  • 191
  • 1
  • 3
  • 15
  • please take a look at https://stackoverflow.com/questions/24210478/hibernate-more-than-one-row-with-the-given-identifier-was-found-error – Atul Aug 11 '17 at 15:46
  • Could you explain how user is related to evaluation, in a business "language"? – O.Badr Aug 11 '17 at 20:09
  • it's an ERP platfrom, so the logged in user will have to choose between a group of workers to evaluate them. my work is to keep a trace of the evaluater which is the logged in user and the evaluated user which is the worker. – petrucci Aug 15 '17 at 11:26

1 Answers1

0

This error occurs when multiple rows are saved but hibernate expects only a single row. In your case, as you wrote correctly, OneToOne is not the right relation to chose because with that, there could only ever be one Evaluation for each Evaluater/EvaluatedUser

You want every User to be able to be EvaluatedUser and Evaluater multiple times. Every Evaluation has just one User as EvaluatedUser and one User as Evaluater. Thus, you have to change your User entity to

@OneToMany(mappedBy = "EvaluatedUser")
private Set<Evaluation> evaluationsEvaluated;
@OneToMany(mappedBy = "Evaluater")
private Set<Evaluation> evaluationsEvaluater;

and your Evaluation entity to

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "evaluateduser_fk")
private User EvaluatedUser;
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "evaluater_fk")
private User Evaluater;
Andreas
  • 344
  • 4
  • 14