0
votes

I have a database with 3 entities: User, Profile and UserProfile. When I update the user, I have an error.

public class User implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  @SequenceGenerator(name = "seqUser", sequenceName = "seqUser")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqUser")
  private Integer id;

  private String email;

  @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  private List<UserProfile> userProfiles = new ArrayList<UserProfile>();

}

And

 public class Profile implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      @SequenceGenerator(name="seqProfile", sequenceName ="seqProfile")
      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqProfile")
      private Integer id;

      private String codeProfile;

      private String libelleProfile;

      @OneToMany(mappedBy="profile", cascade = CascadeType.ALL,  fetch = FetchType.LAZY)
      @JsonIgnore
      private List<UserProfile> userProfiles = new ArrayList<UserProfile>();
    }

and

 public class UserProfile implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      @SequenceGenerator(name = "seqUserProfile", sequenceName = "seqUserProfile")
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqUserProfile")
      private Integer id;

      @ManyToOne
      @JoinColumn(name = "idUser")
      @JsonIgnore
      private User user;

      @ManyToOne
      @JoinColumn(name = "idProfile")
      private Profile profile;
    }

My Repository :

public interface IUserRepository extends JpaRepository<User, Integer>{
  User findByEmail(String email);
}

My Service :

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private IUserRepository userRepository;

    public User getUserByEmail(String email) {

        return userRepository.findByEmail(email);
    }

    public void updateUser(User user) {
        userRepository.save(user);
    }

}

My Controller :

@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @PutMapping(value="/user/{id}", consumes=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> updateUser(
            @PathVariable(value="id", required = true) Integer id,
            @RequestBody User user) {           

        userService.updateUser(user);

        return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
    }
}

Logs & error:

Hibernate: select user0_.id as id1_19_1_, user0_.email as email4_19_1_ from user user0_ where user0_.id=?
Hibernate: select profil0_.id as id1_13_0_, profil0_.code_profil as code_pro2_13_0_, profil0_.libelle_profil as libelle_3_13_0_ from profil profil0_ where profil0_.id=?
2018-12-30 00:38:15.385 DEBUG 10692 --- [nio-8080-exec-1] org.hibernate.SQL : select nextval ('hibernate_sequence') Hibernate: select nextval ('hibernate_sequence')
2018-12-30 00:38:15.389 WARN 10692 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
2018-12-30 00:38:15.389 ERROR 10692 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR : Therelation « hibernate_sequence » doesn't existe Position : 17

My json flux:

{
    "id": 1,
    "email": "email",
    "userProfiles": [
        {
            "profile": {
                "id": 1,
                "codeProfile": "ADMIN",
                "libelleProfile": "Administrateur",
            }
        },
        {
            "profile": {
                "id": 2,
                "codeProfile": "PROFILE2",
                "libelleProfile": "Profile 2",
            }
        }
    ]
}
1
It says there is no table named hibernate_sequence in your database schema. - K.Nicholas

1 Answers

-1
votes

What database are you using? And does this database support sequences?

But apart from that the sequenceName attribute is missing in @SequenceGenerator:

@Id
@SequenceGenerator(name = "seq_user", sequenceName = "seq_user")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_user")
private Integer id;

This sequenceName must be the name of the sequence in the database.

Next you have to make sure to set the back reference to User in the UserProfile class

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private IUserRepository userRepository;

    public User getUserByEmail(String email) {

        return userRepository.findByEmail(email);
    }

    public void updateUser(User user) {
        for (UserProfile profile : user.getUserProfiles()) {
          profile.setUser(user);
        }
        userRepository.save(user);
    }
}