Hi!
First of all: Thank you for all your work on QueryDSL. We enjoy querying with it a lot :-). I'm still fairly new to QueryDSL so please bear with me if this question is trivial.
I would like to Query a Supertype or Interface in a specifc way, but don't know how to progress / having some trouble finding resources on how to do this properly for the following usecase:
(Querydsl 4.1.4, JPA Annotation Processor, Hibernate 4, JEE 6)
We have three databases which basically have the same Entity with minor differences (Assignments). We previously used a abstract class annotated with @MappedSuperClass and then creating actual implementations like StandardAssignment annotated with @Entity, @Table, and @AttributeOverrides for specific column name changes (slight differences in database naming schemes).
We need to select a different implementation (specific database) depending on the date selected. So if date is < X days, we need to select StandardAssignments, If its older, HistAssignments, but if its in a specific Period it need to be SpecialAssignments. I want to prevent writing every query three times for each specific datasource.
I thought i could do this by passing the Date to a function determining the correct Datasources, giving me back the correct QClass (and Datasource/Factory). But the Implementations dont actually extend from the QSuperClass, so I cannot return it. I then tried to create a Assigment Interface with @QuerySupertype, but the Annotationprocessor does not create a QClass for this Interface, so i also cannot return and Query on it.
So in ~pseudo code i basically want to do something like this:
@QuerySupertype
public interface Assignment {
BigDecimal getId();
void setId(BigDecimal fbId);
}
@Entity
@Table(name = "AUFTRAEGE", schema = "dbo")
public class StandardAssignment implements Assignment {
@Id
@Column(name="nr",...)
BigDecimal id;
...
}
@Entity
@Table(name = "AUFTRAEGE", schema="hist")
public class HistAssignment implements Assignment {
@Id
@Column(name="id",...)
BigDecimal id;
...
}
public JPAQueryFactory getCorrectFactoryFor(@NotNull Date d) {
LocalDate selectedD = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int diff = calculateDiffBetweenDates(selectedD); // and today
if (diff < DAYS_UNTIL_HIST) {
return standardFactory;
} else {
return selectedD.isBefore(SPECIFIC_DATE) ? histFactory : otherHistFactory;
}
}
public QAssignment getCorrectQAssignment(@NotNull Date d) {
LocalDate selectedD = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int diff = calculateDiffBetweenDates(selectedD); // and today
if (diff < DAYS_UNTIL_HIST) {
return QStandardAssignment.;
} else {
return selectedD.isBefore(SPECIFIC_DATE) ? histFactory : otherHistFactory;
}
}
And then use it something like this:
public Assignment getAssignments(Date d) {
QAssignment qAssignment = getCorrectQAssignment(d);
List<Assignment> result = getCorrectFactoryFor(d).selectFrom(qAssignment).where( qAssignment... ) //not good example with IDs but we have other stuff too
.fetch()
...
}
I would be glad if you could guide me on the proper path to do this!
Thanks & kind regards
AFB