Is it possible with Spring Data JPA to do smth. like this:
public interface UserDao extends CrudRepository<User, Long> {
@EntityGraph(value = :graphName, type = EntityGraph.EntityGraphType.LOAD)
@Query(value = "SELECT DISTINCT u FROM User u")
List<User> findAllWithDetailsByGraphName(@Param(value="graphName") String graphName);
}
to be able to pass the graphName into method at run-time and invoke the load with a need set of collections? This construction is not working, producing compile time error. Any plays around it also failed...
So, I have multiple collections in User class, which I want to load on condition;
@Entity
@Table(name="user")
@NamedEntityGraphs({
@NamedEntityGraph(name = "User.details", attributeNodes = {
@NamedAttributeNode("phones"), @NamedAttributeNode("emails"), @NamedAttributeNode("pets")}),
@NamedEntityGraph(name = "User.phones", attributeNodes =
{@NamedAttributeNode("phones")}),
@NamedEntityGraph(name = "User.emails", attributeNodes =
{@NamedAttributeNode("emails")}),
@NamedEntityGraph(name = "User.pets", attributeNodes =
{@NamedAttributeNode("pets")})
})
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO, generator="native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name="user_id")
private Long userId;
@Column(name="name")
private String name;
// more fields omitted
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Phone> phones;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Email> emails;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Pet> pets;
}
Now, I only must declare all the methods implicitly, like this:
@EntityGraph(value = "User.phones", type = EntityGraph.EntityGraphType.LOAD)
@Query(value = "SELECT DISTINCT u FROM User u")
List<User> findAllWithPhones();
Thnaks you for suggestions!