开发者

JPA Eclipselink Subquery in where clause with between, CriteriaBuilder and metamodel

I want to do this query with metamodel but I can't!! I dont know how to do this.

MYSQL QUERY (with this query I want to get all the rows from the Clases table that are teaching in this moment):

SELECT * FROM clases cl 
WHERE CURRENT_TIME() BETWEEN
(SELECT ml2.inicio FROM modulos ml2 WHERE cl.modulo_id=ml2.modulo_id ) AND 
(SELECT ml2.fin FROM modulos ml2 WHERE cl.modulo_id=ml2.modulo_id) AND
 cl.fecha=CURRENT_DATE();

These are my entities:

ENTITY MODULOS

@Entity
@Table(name = "modulos")
public class Modulos implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "modulo_id")
    private Integer moduloId;

    @Basic(optional = false)
    @Column(name = "inicio")
    @Temporal(TemporalType.TIME)
    private Date inicio;

    @Basic(optional = false)
    @Column(name = "fin")
    @Temporal(TemporalType.TIME)
    private Date fin;

    @Basic(optional = false)
    @Column(name = "modulo")
    private String modulo;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "modulos")
    private List<GruposHorarioHasModulos> gruposHorarioHasModulosList;

    //getters and setters...
}

ENTITY CLASES

@Entity
@Table(name = "clases")
public class Clases implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "clase_id")
    private Integer claseId;

    @Basic(optional = false)
    @Column(name = "aula")
    private String aula;

    @Basic(optional = false)
    @Column(name = "fusion")
    private boolean fusion = false;

    @Basic(optional = false)
    @Column(name = "clase_numero")
    private Integer claseNumero;

    @Basic(optional = false)
    @Column(name = "clase_impartida")
    private boolean claseImpartida = false;

    @JoinColumn(name = "modulo_id", referencedColumnName = "modulo_id")
    @ManyToOne(optional = false)
    private Modulos modulos;

    //getters and setters...
}

I have this:

EntityManager em1 = Persistence.createEntityManagerFactory("myPU").createEntityManager();
CriteriaBuilder cb = em1.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<Clases> root = cq.from(Clases.class);
cq.select(root.get(Clases_.claseId));

Subquery<Date> sqOne = cq.subquery(Date.class);
Root<Modulos> root2 = sqOne.from(Modulos.class);
sqOne.select(root2.get(Modulos_.inicio));

Subquery<Date> horaInicio = sqOne.select(root2.get(Modulos_.inicio)).where(
        cb.equal(
            root2.get(Modulos_.moduloId),
            root.get(Clases_.modulos).get(Modulos_.moduloId)
            )
        );

Subquery<Date> sqTwo = cq.subquery(Date.class);
Root<Modulos> root3 = sqTwo.from(Modulos.class);
Subquery<Date> horaFin = sqTwo.select(root3.get(Modulos_.fin)).where(
        cb.equal(
            root3.get(Modulos_.moduloId),
            root.get(Clases_.modulos).get(Modulos_.moduloId)
            )
        );

cq.where(cb.between(cb.currentTime(), horaInicio, horaFin));
em1.createQuery(cq).getResultList();

This code gives me the following exception:

Exception in thread "main" java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.querydef.SubQueryImpl cannot be cast to org.eclipse.persistence.internal.jpa.querydef.ExpressionImpl

If i change the where clause for this one...

cq.where(cb.between(cb.currentTime(), new Date(), new Date()));

... it works but without my Subquery, then i can see the error came from my subquery, but I don't know why, if I change the where clause for this one...

cq.where(cb.greaterThan(cb.currentTime(), horaInicio));

... I get this:

SELECT t0.clase_id FROM clases t0, clases t1 WHERE (CURRENT_TIME > (SELECT t2.inicio FROM modulos t3, modulos t2 WHERE ((t2.modulo_id = t3.modulo_id) AND (t3.modulo_id = t0.modulo_id))))

I can see the problem are the 2 subqueries in the between clause.

Please I need help with this, I spent 2 weeks looking for an answer but..开发者_运维知识库. nothing... help. I'm using JPA 2.0 with Netbeans and EclipseLink with metamodel generator and Java 6. I want to do it with metamodels and criteriasbuilder and criteriasquerys

As you can see i need to do a subquery in the where clause and in that where clause I need to do a between where each parameter have a subquery, like this:

SELECT * FROM X WHERE CURRENT_TIME BETWEEN **MY_SUBQUERY_ONE** AND **MY_SUBQUERY_TWO**


Abit late answer but..

Subquery<Date> sqOne = cq.subquery(Date.class);
Root<Modulos> root2 = sqOne.from(Modulos.class);
sqOne.select(root2.get(Modulos_.inicio));

Subquery<Date> horaInicio = sqOne.select(root2.get(Modulos_.inicio)).where(
    cb.equal(
        root2.get(Modulos_.moduloId),
        root.get(Clases_.modulos).get(Modulos_.moduloId)
    )
);

Not sure what you are trying to do here, and it might or might not be related to your error. You create sqOne and make a selection. Then you redo the selection, replacing the previous one and copy the result to horaInicio. What is the purpose of this? You might aswell skip sqOne.select(root2.get(Modulos_.inicio)); and keep using sqOne instead of horaInicio.

Also, I'm not sure you can mix and match Roots created from different queries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜