Git Product home page Git Product logo

duplicate-parent-entities's Introduction

Spring Data JPA - duplicated parent entities in 'join fetch' repository query methods

Repository query methods with 'join fetch' leads to the duplicate parents. Number of duplicates corresponds to number of nested children.

To workaround this situation we can use 'distinct' in the query. But this still does not work with projection.

The similar methods with @EntityGraph work as expected.

Example:

@Entity
class Parent extends BaseEntity {
	private String name;
	@OneToMany(mappedBy = "parent")
	private List<Child> children;
}

@Entity
class Child extends BaseEntity {
	private String name;
	@ManyToOne(fetch = LAZY)
	private Parent parent;
}

interface ParentProjection {
	Integer getId();
	String getName();
	List<Child> getChildren();
}

interface ParentRepo extends JpaRepository<Parent, Integer> {

	@EntityGraph(attributePaths = "children")
	@Override
	List<Parent> findAll(); // work as expected 

	@Query("select p from Parent p left join fetch p.children")
	List<Parent> findWithQuery(); // has duplicated parents
	
	@Query("select distinct p from Parent p left join fetch p.children")
   	List<Parent> findDistinctWithQuery(); // does not have duplicated parents
	
	@EntityGraph(attributePaths = "children")
	List<ParentProjection> findProjectionsBy(); // work as expected
		
	@Query("select distinct p from Parent p left join fetch p.children")
   	List<ParentProjection> findDistinctProjectionsWithQuery(); // has duplicated parents 
}

More tests is here.

duplicate-parent-entities's People

Contributors

cepr0 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

schauder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.