question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Join fetch returns duplicate results

See original GitHub issue

Hello,

Join fetch on a Repository returns duplicate entities depending on the number of records in the one to many association

Example project here - https://github.com/kirans9731/repo-join-fetch.git

@Entity
@Table(name = "customer")
public class Customer {

    private Long id;
    private String name;
    private String email;
    private String address;

    private Set<Order> orders = new HashSet<>();

    ....

    @OneToMany (mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    public Set<Order> getOrders() {
        return orders;
    }
}

@Entity
@Table(name = "orders")
public class Order {

    private Long id;
    private String product;

    private Customer customer;

    ....
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_id")
    public Customer getCustomer() {
        return customer;
    }
}

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> {

    @Join(value = "orders", type = Join.Type.FETCH)
    Collection<Customer> findByName(String name);
}

@MicronautTest
public class CustomerRepositoryTest {

    @Inject
    CustomerRepository customerRepository;

    @BeforeEach
    public void init() {

        Customer customer = new Customer();
        customer.setName("Kiran");
        customer.setAddress("USA");
        customer.setEmail("user.name@email.com");

        Order order1 = new Order();
        order1.setCustomer(customer);
        order1.setProduct("Prod1");

        Order order2 = new Order();
        order2.setCustomer(customer);
        order2.setProduct("Prod2");

        Order order3 = new Order();
        order3.setCustomer(customer);
        order3.setProduct("Prod1");

        customer.getOrders().add(order1);
        customer.getOrders().add(order2);
        customer.getOrders().add(order3);

        customerRepository.save(customer);
    }

    @Test
    public void test_findByName() {

        Collection<Customer> customers = customerRepository.findByName("Kiran");
        Assertions.assertEquals(1, customers.size()); // this is 3
    }
}
    

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
graemerochercommented, Oct 8, 2019

@psoares I agree we need to make this easier, but altering the behaviour of @Join(..) is not the correct thing to do and will create more confusion since for me @Join(..) by its name indicates you want to perform a join query (INNER JOIN, LEFT JOIN etc.)

My current plan is to add an additional annotation probably named @Select(..) that will allow you to give a hint to Micronaut Data to perform an additional select to retrieve the association as well in cases where you need it.

1reaction
psoarescommented, Oct 8, 2019

@graemerocher Although your explanation makes perfect sense, I think either micronaut-data should handle the initialization of collection properties in a simpler manner, or the docs should have more examples of how to handle different situations. From the docs, I find it hard to understand that the @Join annotation can be used in a @ManyToOne or @OneToOne relationships, but not in a @OneToMany.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicates using left join fetch - hibernate - Stack Overflow
I'm using a HQL query to obtain certain records. If I use LEFT JOIN FETCH a collection ...
Read more >
Why does JOIN FETCH return duplicates? - Hibernate Forums
I would have expected that only the distinct BuddyRelation entries are returned, with properly filled buddyPermissions Map. What is wrong?
Read more >
Query with FETCH JOIN returns multiple results instead of one.
Query with FETCH JOIN returns multiple results instead of one. ... You should be able to filter duplicates with DISTINCT:
Read more >
FETCH JOIN with DISTINCT returns duplicate values
When I make a fetch join (left outer join with FETCH clause) I get duplicated return values even when I specify DISTINCT. Without...
Read more >
left join fetch returns duplicates - Similar Threads - CodeRanch
Left join fetch cat.kittens kitten. Any idea how to avoid duplication in result list?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found