Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of multiple @ManyToMany associations returns duplicate objects in list #1961

Open
lmbuchholz opened this issue Nov 28, 2024 · 0 comments

Comments

@lmbuchholz
Copy link

I have a Book Entity which has many-to-many relationships with entities Author and Publisher. To fetch the Book entity and its relations in one query, I am using entity views. The Book entity view returned by the query unexpectedly contains duplicate objects.

This issue appears to be very similar to issue #660 .

Description

Model classes

@Lombok.Data
@Lombok.EqualsAndHashCode
@Entity
@Table(name = "books")
public class Book implements Serializable {

    @Id
    @GeneratedValue
    private long id;
    private String title;

   @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "books_authors",
            joinColumns = @JoinColumn(name = "book_book_id"),
            inverseJoinColumns = @JoinColumn(name = "author_book_id"))
    private List<Author> authors;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "books_publishers",
            joinColumns = @JoinColumn(name = "book_book_id"),
            inverseJoinColumns = @JoinColumn(name = "publisher_book_id"))
    private List<Publisher> publishers;
}
@Lombok.Data
@Lombok.EqualsAndHashCode
@Entity
@Table(name = "authors")
public class Author implements Serializable {

    @Id
    @GeneratedValue
    private long id;
    private String firstName;
    private String lastName;
}
@Lombok.Data 
@Lombok.EqualsAndHashCode
@Entity
@Table(name = "publishers")
public class Publisher implements Serializable {

    @Id
    @GeneratedValue
    private long id;
    private String name;
    private String country;
}

Repository

@Transactional(readOnly = true)
public interface BookBlazeRepository extends EntityViewRepository<BookView, Long> {

    List<BookView> findByTitle(String title);
}

Entity Views

@EntityView(Book.class)
public interface BookView {

    @IdMapping
    public long getId();

    public String getTitle();

    @Mapping(fetch = FetchStrategy.MULTISET)
    List<AuthorView> getAuthors();

    @Mapping(fetch = FetchStrategy.MULTISET)
    List<PublisherView> getPublishers();
}
@EntityView(Author.class)
public interface AuthorView {

    @IdMapping
    public long getId();

    public String getFirstName();

    public String getLastName();
}
@EntityView(Publisher.class)
public interface PublisherView {

    @IdMapping
    public long getId();

    public String getName();

    public String getCountry();
}

Expected behavior

The returned BookView will look as follows, with no objects duplicated:

[
    {
        "id": 1,
        "title": "Some Famous Book Title", 
        "publishers": [
            {
                "id": 2
                "name": "Another Publishing Company", 
                "country": "Canada"
            }
        ],
       "authors": [
            {
                "id": 1,
                "firstName": "John", 
                "lastName": "Smith"
            }
        ]
    }
]

Actual behavior

When my program executes the findByTitle method from the repository mentioned above, the publisher object is duplicated:

[
    {
        "id": 1,
        "title": "Some Famous Book Title", 
        "publishers": [
            {
                "id": 2
                "name": "Another Publishing Company", 
                "country": "Canada"
            },
            {
                "id": 2
                "name": "Another Publishing Company", 
                "country": "Canada"
            }
        ],
       "authors": [
            {
                "id": 1,
                "firstName": "John", 
                "lastName": "Smith"
            }
        ]
    }
]

Note that if I use JPARepository and the entity classes (rather than entity views), this list of books is generated correctly.

Steps to reproduce

Execute the findByTitle method from the repository mentioned above.

Environment

Java 21.0.4 2024-07-16 LTS
Spring Boot 3.3.3
Hibernate 6.5.2.Final
MySQL connector 8.0.2

@lmbuchholz lmbuchholz changed the title Use of multiple @ManyToMany associations returns duplicate items in list Use of multiple @ManyToMany associations returns duplicate objects in list Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant