#(Hibernate) More than one row with the given identifier was found

5 messages · Page 1 of 1 (latest)

safe vapor
#

Hello world! Can you pls help to solve this issue:
"An unexpected error occurred: More than one row with the given identifier was found: 9, for class: ..*.model.Trainee"

When I create any models like trainee or trainer, I'm alwaying getting number 9

  • I already checked my DB, there's no duplicate IDs
  • In project I'm not manyally assigning any ID
  • All entities have @Id and @GeneratedValue
  • I tryed using GenerationType.SEQUENCE instead of GenerationType.IDENTITY cause someone said that postgres doesn't support identity.
  • I also added some sout for debug, but I didn't see any of them in my terminal

here I will provide you some of my code and thank you in advance:

I have CLI app so after entering data it will come directly to my facade class:

@Transactional
public void saveTrainee(TraineeRequest request) {
    var user = toUser(request);
    userService.create(user);

    user = userService.findByUsername(user.getUsername())
            .orElseThrow(() -> new ResourceNotFoundException("User not found"));

    var trainee = toTrainee(request, user);
    traineeService.create(trainee);
}

UserServiceImpl:

@Override
public void create(User user) {
    var username = generateUsername(
            user.getFirstName(),
            user.getLastName(),
            findAll().stream().map(User::getUsername).toList());
    user.setUsername(username);
    user.setPassword(generateRandomPassword());

    userDAO.save(user);
    System.out.println("User's id: " + user.getId());
}

TraineeServiceImpl:

 @Override
public void create(Trainee trainee) {
    traineeDAO.save(trainee);
    System.out.println("Trainee's id: " + trainee.getId());
}

AbstractDAO<T> class which handles basicly all services:

@Override
public void save(T entity) {
    session.persist(entity);  // I tried using merge too, no diff
    session.flush();
    log.info("{} has been saved", entityClass.getSimpleName());
}
noble coralBOT
#

This post has been reserved for your question.

Hey @safe vapor! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

hybrid flicker
#

are there maybe multiple rows with the same username?

#

or are you trying to insert a user with an existing username