How to expose a complete tree structure with Spring Data REST and HATEOAS? - jpa

I have a JPA tree structure
#Entity
public class Document {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String text;
#ManyToOne
#JoinColumn(name = "parent")
Document parent;
#OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
Set<Document> children;
(getters and setters)
}
and a projection
#Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {
int getId();
String getText();
Set<Document> getChildren();
}
When I make a GET request with url
localhost:8080/documents/1?projection=all
I only get the first children of the root document. Not children of the children. Is this possible with projections? Or is there an other way?

#Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {
int getId();
String getText();
Set<AllDocumentsProjection> getChildren();
}
This works perfect for me.

I'm almost certain there is no way to recursively embed resources via projections. Only other thing I think of is to handle this logic manually in the controller :/

Try excerpts.
You should add to your repository definition the excerptProjection field like below:
#RepositoryRestResource(excerptProjection = AllDocumentsProjection.class)
interface DocumentRepository extends CrudRepository<Document, Integer> {}

Related

duplicate results when sorting by collection property

Repost from here
Given entities and repository:
#Entity
public final class Partner {
#Id
private String id;
#OneToMany(mappedBy = "partner", fetch = FetchType.LAZY)
private List<Merchant> merchants;
...
}
#Entity
public final class Merchant {
#Id
private String id;
#Column
private String name;
#ManyToOne(fetch = FetchType.LAZY)
private Partner partner;
...
}
public interface PartnerRepository
extends JpaRepository<Partner, String>, QueryDslPredicateExecutor<Partner> {
}
If there is only one partner having two merchants in the DB then the following code incorrectly returns list with two instances of the same parnter.
partnerRepository.findAll(new Sort("merchants.name"));
This is caused internally by the DB join. By creating custom implementation that adds the distinct to the selection the result is correctly the single partner.
Wouldn't it be correct to do distinct selection per default?
Try
#OneToMany(mappedBy = "partner", fetch = FetchType.LAZY)
#OrderBy("name")
private List<Merchant> merchants;

Spring JPA query using specification and projection

I used spring jpa specification to build dynamically an entity query.
It's working perfect but the query returns all entity fields which makes the performance slower.
I want to fetch specific entity fields only and not fetching all entity fields and dependencies which I don't want and I will not use.
I search on the web, I tried some scenarios but without any lack.
Can anyone suggest any solution on this?
Thanks in advance
Here is what I have.I'm using spring boot 2.2.4
public class Concert {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column
private String name;
#Column
private String code;
#Column
private double totalIncome;
#Column
private double totalExpenses;
#Column
private double totalBudget;
#ManyToOne(targetEntity = Orchestra.class, fetch = FetchType.EAGER, optional = false)
#JoinColumn(name = "orchestra_id")
private Orchestra orchestra;
#ManyToOne(targetEntity = ConcertStatus.class, fetch = FetchType.EAGER, optional = false)
#JoinColumn(name = "concert_status_id")
private ConcertStatus status;
/* other fields */
}
Specification:
public class ConcertSpecification implements Specification<Concert> {
#Override
public Predicate toPredicate(Root<Concert> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
//add add criteria to predicates
for (Criterion criteria : criteriaList) {
/* predicates builder here */
}
return builder.and(predicates.toArray(new Predicate[0]));
}
}
Repository:
public interface ConcertDao extends JpaRepository<Concert, Long>, JpaSpecificationExecutor<Concert>, PagingAndSortingRepository<Concert, Long> { }
ConcertService:
public interface ConcertService {
Page<Concert> findAll(#Nullable Specification<Concert> spec, Pageable pageable);
}
ConcertServiceImpl:
#Service(value = "concertService")
public class ConcertServiceImpl implements ConcertService {
public Page<Concert> findAll(#Nullable Specification<Concert> spec, Pageable pageable){
List<Concert> list = new ArrayList<>();
concertDao.findAll(spec).iterator().forEachRemaining(list::add);
return new PageImpl<Concert>(list);
}
}
Usage of projections with specifications are not supported and there is a PR for it that has been hanging for over five years.

Spring Data JPA order by value from OneToMany relation

I am trying to sort a result by nested collection element value. I have a very simple model:
#Entity
public class User {
#Id
#NotNull
#Column(name = "userid")
private Long id;
#OneToMany(mappedBy = "user")
private Collection<Setting> settings = new HashSet<>();
// getters and setters
}
#Entity
public class Setting {
#Id
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userid")
private User user;
private String key;
private String value;
// getters and setters
}
public interface UserRepository extends JpaRepository<User, Long>, QuerydslPredicateExecutor<User> {
}
I want to have a result returned sorted by the value of one setting.
Is it possible to order by user.settings.value where settings.name = 'SampleName' using Spring Data JPA with QueryDSL?
I've used JpaSpecificationExecutor. let's see findAll for example.
Page<T> findAll(#Nullable Specification<T> spec, Pageable pageable);
Before call this method you can create your specification dynamically (where condition) and Pageable object with dynamic Sort information.
For example
...
Specification<T> whereSpecifications = Specification.where(yourWhereSpeficiation);
Sort sortByProperty = Sort.by(Sort.Order.asc("property"));
PageRequest orderedPageRequest = PageRequest.of(1, 100, sortByProperty);
userRepository.findAll(whereSpecifications, PageRequest.of(page, limit, orderedPageRequest));

Query in Spring JPA using List of two parameters

public interface InventoryRepository extends JPARepository<Inventory, Long> {
List<Inventory> findByIdIn(List<Long> ids);
}
Above is working fine, however in same way I am trying to fetch the List or Map, based on multiple params List ids and List sortNumber.
I would be also happy with return type Map from the method.
I came up with below things, which isn't correct.
List<Inventory> findByIdANDSortNumberIn(List<Long> ids, List<Long> sortNumbers);
Should do it with help of Criteria ? Is there any better way to do it?
Entity :
#Entity
#Table(name = Constants.T_INVENTROTY)
#Data
public class Inventory implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = Constants.COLUMN_IN_DM)
private Long id;
#Column(name = Constants.COLUMN_PROD_DESCRIPTION)
private String prodDescription;
#Column(name = Constants.COLUMN_PROD_DESCRIPTION)
private Long sortNumber;
#Column(name = Constants.COLUMN_QUANTITY)
private long quantity
}
This should work
List<Inventory> findByIdInAndSortNumberIn(List<Long> ids, List<Long> sortNumbers);
You can specify And and do the same for multiple fields.

How can I share an Entity for other entities' OneToMany

I have an Entity look like this.
#Entity
class Property extends BaseEntity {
#Basic
private String name;
#Basic
private String value;
}
The basic intention is using this Entity as other Entities properties.
#Entity
class MyEntity extends BaseEntity {
#OneToMany
private List<Property> properties;
}
#Entity
class YourEntity extends BaseEntity {
#OneToMany
private List<Property> properties;
}
How can I do this? Do I have to define each owner's field in Property?
#Entity
class Property extends BaseEntity {
#Basic
private String name;
#Basic
private String value;
#ManyToOne(optional = true)
private MyEntity myEntity;
#ManyToOne(optional = true)
private YourEntity yourEntity;
#ManyToOne(optional = true)
private OtherEntity otherEntity;
}
Basically it is a good solution You represented here. There is the option to create a join table which will help you to keep the entity "cleaner" (and also could be used as a ManyToMany. In most of the cases I prefer to use the option You provided [simplicity is a gooooood thing :) ], but other colleagues got different view on this problem.
TL.DR: Your provided code is working and I personally prefer it. There are other ways but those are a bit slower etc.