Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

TsrUser N+1 from ManyToMany #145

Description

@gorhack

To reproduce:

  1. Login for the first time (clear database, new user, etc)
  2. See org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "tsr_user_user_id_key" error in the logs
2021-01-13 19:02:14.830 DEBUG 79816 --- [nio-8080-exec-8] org.hibernate.SQL                        : select count(*) as col_0_0_ from tsr_user tsruser0_
2021-01-13 19:02:14.830 DEBUG 79816 --- [nio-8080-exec-9] org.hibernate.SQL                        : select count(*) as col_0_0_ from tsr_user tsruser0_
2021-01-13 19:02:14.833 TRACE 79816 --- [nio-8080-exec-9] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([col_0_0_] : [BIGINT]) - [0]
2021-01-13 19:02:14.833 TRACE 79816 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([col_0_0_] : [BIGINT]) - [0]
2021-01-13 19:02:14.866 DEBUG 79816 --- [nio-8080-exec-8] org.hibernate.SQL                        : insert into tsr_user (email_address, phone_number, role, user_id, username) values (?, ?, ?, ?, ?)
2021-01-13 19:02:14.866 DEBUG 79816 --- [nio-8080-exec-9] org.hibernate.SQL                        : insert into tsr_user (email_address, phone_number, role, user_id, username) values (?, ?, ?, ?, ?)

...


2021-01-13 19:02:14.887  WARN 79816 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23505
2021-01-13 19:02:14.887 ERROR 79816 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: duplicate key value violates unique constraint "tsr_user_user_id_key"
  Detail: Key (user_id)=(0bd59112-8b66-4719-ae08-4c14eb27256c) already exists.

Hibernate ManyToMany possibly causing the N+1 issue.

Tried @Fetch(FetchMode.JOIN) and FetchType.LAZY without success. May want to investigate changing the owning side to Organization. Needs further investigation.

Related threads:
https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/
https://thorben-janssen.com/best-practices-for-many-to-many-associations-with-hibernate-and-jpa/
https://stackoverflow.com/questions/9821914/how-to-persist-manytomany-relation-duplicate-entry-or-detached-entity

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions