如何在QueryDsl中编写SQL查询?

时间:2014-10-24 11:43:45

标签: querydsl

我有以下SQL查询:

select e.id,  pv.presence_KEY, pv.presence from employees e
left join presence p on p.employee_id = e.id
left join presence_values pv on pv.presence_id = p.id
where p.month = '2014-10-01' or p.month = '2014-10-01' is null;

如何在QueryDsl中编写?

    QPresence presence = QPresence.presence;
    MapPath<String, String, StringPath> values = presence.presence;
    QEmployee employee = QEmployee.employee;

我试过以下方式:

List<Tuple> list = query.from(employee).leftJoin(employee.presence, presence).leftJoin(presence.presenceValues, values)
            .where(presence.month.eq(date).isNull().or(presence.month.eq(date)).list(employee.id, presence);

但问题是如何离开加入MapPath <String, String, StringPath> values

Presence.class:

@Entity
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@JsonDeserialize(using = PresenceDeserializers.class)
public class Presence {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   private Date month;


   @ElementCollection(fetch = FetchType.EAGER)
   @CollectionTable(
            name="presence_values"
   )
   private Map<String, String> presenceValues;

   public Presence() {
            setPresence(new HashMap<String, String>());
   }

   @ManyToOne
   private Employee employee;

   //getters and setters
}

Employee.class:

@Entity
@Table(name = "employees")
@Audited
public class Employee {
     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE, mappedBy = "employee")
     private Set<Presence> presence;

     //other methods
     //getters and setters

}

0 个答案:

没有答案