Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
V2 2.0.11
-
None
-
None
Description
I have a classic setup of users, roles and user_roles table. Users are defined as follows:
@Entity @Table(name = "users") public class User implements Serializable { ... @Getter @Setter @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"), uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "role_id"})} ) Role.class) private List<Role> roles; ... }
The problem is when I execute PATCH (PUT, MERGE) on Users endpoint
PATCH http://localhost:8080/admin/rest/odata/Users(7) { "RoleDetails":[ { "__metadata":{ "id":"http://localhost:8080/admin/rest/Roles(3L)", "uri":"http://localhost:8080/admin/rest/Roles(3L)", "type":"default.Role" }, "Code":"ROLE8", "Id":"3", "Name":"Role 8" } ] }
and the user has a role from the RoleDetails list then I get an error that results from PostgreSQL's
ERROR: duplicate key value violates unique constraint "pk_user_roles"
At the same time when I update as user with plain JPA repository it succeeds:
@PUT @Path("/{id}") @Produces("application/json") public User put(@Context SecurityContext securityContext, @PathParam("id") Long id, User user){ return userRepository.saveAndFlush(user); }
Adding @EdmNavigationProperty to roles field did not help:
@Getter @Setter @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id") ) @EdmNavigationProperty(toMultiplicity = Multiplicity.MANY, toType = Role.class) private List<Role> roles;
OData app uses Spring Boot entity manager as proposed in this tutorial: https://www.baeldung.com/odata
The question is: shoudn't olingo check for existing records in a join table? how can I further investigate the issue? Any working example of olingo2 + JPA + @JoinTable would be highly appreciated.