JPA - Finding nested entities using combined criterias - jpa

I have an entity named FileEntity which contains a list of reports of the type ReportEntity.
FileEntity has an field which determines, which user has created the file containing a number of reports.
#Entity
public class FileEntity {
#Id
private Long id;
#JoinColumn(name = "user")
#ManyToOne(optional = true)
#NotNull
private User user;
#OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
orphanRemoval = true)
#NotNull
private List<Report> reports = new ArrayList<>(5);
...
}
#Entity
public class Report {
#Id
private Long id;
...
}
I am currently trying to fetch a single report with a given report ID and the ID of the person who issued the file containing the report. The combination is unique, so it should only return one report for a certain combination of report and user ID. But I am unable to retrieve a single result using the following criteria:
public Report findReportByUserAndReportId(Long reportId, Long userId) {
Objects.nonNull(reportId);
Objects.nonNull(userId);
try {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Report> cq = cb.createQuery(Report.class);
final Root<FileEntity> fileEntity = cq.from(FileEntity.class);
final Root<Report> report = cq.from(Report.class);
final Join<FileEntity, Report> join = fileEntity.join(FileEntity_.reports);
final Predicate[] predicates = new Predicate[]{
cb.equal(join.get("id"),
userId),
cb.equal(join.get(Report_.id),
reportId)};
cq.select(report).where(predicates);
return entityManager.createQuery(cq).getSingleResult();
} catch (NoResultException |
NonUniqueResultException ne) {
LOG.log(Level.WARNING,
"Could not find report: {0}",
ne.getMessage());
}
return null;
}
Has someone an idea what I am doing wrong?

First, don't use multiple roots here, because they generate a carthesian product of all the elements, without joining them. Use joins or Paths instead. Second, in the first predicate. join object denotes a Path of reports, not a user, therefore it doesnt' make sense to look for the userid there.
Root<FileEntity> fileEntity = cq.from(FileEntity.class);
Path<User> user = fileEntity.get(FileEntity_.user);
Join<FileEntity, Report> reports = fileEntity.join(FileEntity_.reports);
Predicate[] predicates = new Predicate[]{
cb.equal(user.get(User_.id),
userId),
cb.equal(reports.get(Report_.id),
reportId)};

Related

Crieria API query using criteriabuilder.construct with a non existing relationship

Given this very simple DTO:
#Entity
public class Employee implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
#OneToOne
private Employee boss;
}
I'd like to make a query that gathers all employee names and their boss' id, put in a nice clean POJO:
public class EmployeeInfo {
private String name;
private Long bossId;
public EmployeeInfo(String name, Long bossId) {
this.name = name;
this.bossId = bossId;
}
}
This query should be of use:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<EmployeeInfo> query = cb.createQuery(EmployeeInfo.class);
Root<Employee> root = query.from(Employee.class);
query.select(
cb.construct(EmployeeInfo.class,
root.get("name").as(String.class),
root.get("boss").get("id").as(Long.class)));
result = em.createQuery(query).getResultList();
When a bossId is present in the employee column this works just fine. But when no boss id is set the record will be completly ignored. So how do i treat this non existing boss relation as null or 0 for the construct/multiselect?
In pure SQL it is easy:
SELECT name, COALESCE(boss_id, 0) FROM EMPLOYEE;
But for the love of god i cannot make the criteria api do this.
cb.construct(EmployeeInfo.class,
root.get("name").as(String.class),
cb.coalesce(root.get("boss").get("id").as(Long.class), 0L)));
The problem is that root.get("boss") generate query with cross join like this from Employee employee, Employee boss where employee.boss.id=boss.id. So records where employee.boss.id is null are ignored.
To solve the problem you should use root.join("boss", JoinType.LEFT) instead of root.get("boss")

JPA Specification: Select all entities which have at least one param with attribute from list

I have 2 entities with relationship ManyToMany
#Entity
#Table
public class TranslationUnit implements Serializable {
#Id
private Long id;
#ManyToMany(mappedBy = "translationUnit", fetch = FetchType.EAGER)
private Set<Category> categories = new HashSet<>();
}
#Entity
#Table
public class Category implements Serializable {
#ManyToMany
#JoinTable(name = "category_translation_unit",
joinColumns = #JoinColumn(name = "categories_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "translation_units_id", referencedColumnName = "id"))
private Set<TranslationUnit> translationUnits = new HashSet<>();
}
In Category I have 1 field, which should be used for filtering:
String name;
I need to be able to specify list of Category names (List), and select those TranslationUnits which have at least one Category with specified name.
I have several other filtering options, which should be used together, and I successfully built Specifications for them. But I've stuck with this one.
Please help.
P.S. One of my existing Specifications looks like this:
Specification idSpec = (Specification) (r, q, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (!filterRequest.getTranslationUnitIds().isEmpty())
predicates.add(r.get(TranslationUnit_.id).in(filterRequest.getTranslationUnitIds()));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
Good day. You could use IN for filtering translation units by category names list. I believe, it will look like this using Criteria API:
Root<TranslationUnit> itemsRoot = ...;
Join join = itemsRoot.join("categories");
List<Predicate> predicates = new ArrayList<>();
predicates(join.get("name").in(categoryNamesList));

Spring projections select collection

I am attempting to have a station projection include a list of associated logos. Below is my domain:
#Table(name = "Station")
public class Station implements Serializable {
#Id
#Column(name = "Id")
private int id;
#OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy = "station")
private Set<Logo> logos;
}
The #OneToMany associated logos:
#Table(name = "Logo")
public class Logo {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Transient
private String fullUrl; // calculated
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "StationId", nullable = false)
private Station station;
}
My repository and query is as follows:
#Query(value = "SELECT s.id AS Id, s.logos As Logos FROM Station s JOIN s.users su WHERE su.username = ?1")
Collection<StationListViewProjection> findStationsByUsername(String username);
My station projection expects the Id and a list of logoProjections
#Projection(name = "StationListViewProjection", types = Station.class)
public interface StationListViewProjection {
int getId();
Set<LogoProjection> getLogos();
}
The logoProjection only needs the url
#Projection(name = "LogoProjection", types = Logo.class)
public interface LogoProjection {
String getFullUrl();
}
When i execute my query I get a strange response:
MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as col_5_0_, . as col_6_0_, stationlog3_.id as id1_20_0_'
If I understand this
#Transient
private String fullUrl; // calculated
correct, your fullUrl gets calculated inside your java code and more important, it doesn't have a matching column in the database. You can't use such field in projections directly. You might be able to use an Open Projection and specify the calculation to obtain the fullUrl using a #Value annotation.

JPA Criteria Query: how to automatically create LEFT JOIN instead of WHERE conditions

I have two entity:
public class public class Person implements Serializable {
private static final long serialVersionUID = -8729624892493146858L;
#Column(name="name")
private String name;
...
#JoinColumn(name = "idcity",referencedColumnName = "id",nullable = true)
#ManyToOne(targetEntity = City.class, fetch = FetchType.EAGER)
private City city
...
}
and the related entity (extract):
public class City{
Long id;
String name;
...
}
Now i'm creating a criteria query in a standard way, querying the Person class:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery query = cb.createQuery(selectClass);
Root<T> root = query.from(this.entityClass);
Selection selezioni[] = new Selection[selections.length];
for(int i=0; i< selections.length; i++){
selezioni[i] = CriteriaHelper.getField(selections[i], cb, root);
}
query.select(cb.construct(selectClass, selezioni));
where entityClass is Person and selection and selectClass are used to compile the SELECT clause. In the select i've person.city.name field.
This system create a query with where clause:
select person.name, ..., city.name from person, city WHERE person.idcity = city.id...
but city is not required, so the records without city are not fetched.
Without changing all my automatic system, does exists a simpler way to force the use on LEFT JOIN for the relationship than adding a system to create root.join("field",LEFT)?
Note: the method CriteriaHelper.getField() return a Path starting from the root object

Hibernate Envers - custom RevisionEntity - how to get record

I have written my custom RevisionEntity class to store additional data (for example username), like below:
#Entity
#RevisionEntity(AuditListener.class)
#Table(name = "REVINFO", schema = "history")
#AttributeOverrides({
#AttributeOverride(name = "timestamp", column = #Column(name = "REVTSTMP")),
#AttributeOverride(name = "id", column = #Column(name = "REV")) })
public class AuditEntity extends DefaultRevisionEntity {
private static final long serialVersionUID = -6578236495291540666L;
#Column(name = "USER_ID", nullable = false)
private Long userId;
#Column(name = "USER_NAME")
private String username;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
I can see that all rows in database are correctly stored, REVINFO table contains also username.
I would like to query database to get detailed information from my custom RevisionEntity, like username.
How can I do it? Is there any supported API to get it?
Lets assume you know the identifier of the entity you're interested in the revision entity metadata for, you can easily query that information using the following approach:
final AuditReader auditReader = AuditReaderFactory.get( session );
List<?> results = auditReader.createQuery()
.forRevisionsOfEntity( YourEntityClass.class, false, false )
.add( AuditEntity.id().eq( yourEntityClassId ) )
.getResultList();
The returned results will contain an Object array, e.g. Object[] where results[1] will hold the revision entity instance which contains the pertinent information your wanting.
For more details, you can see the java documentation comments here
If you only have the revision number, you can access just the revision entity instance directly by:
// I use YourAuditEntity here because AuditEntity is actually an Envers class
YourAuditEntity auditEntity = auditReader
.findRevision( YourAuditEntity.class, revisionId );
For more details on the AuditReader interface, you can see the java documentation here