简体   繁体   中英

The right expression is not a valid expression in the JPQL/HQL query

I don't understand why is my JPQL expression wrong. I want to get the list with bikes that are not deleted from my database and is renting at the moment. Can you help me?

@Data
@MappedSuperclass
public abstract class AbstractEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Basic(optional = false)
    @Column(nullable = false)
    private boolean isRemoved;
}

@Data
@Entity
@NamedQueries({
        @NamedQuery(name = "Bike.findRent", query = "SELECT b FROM Bike b WHERE NOT b.isRemoved AND b.isRent")
})
public class Bike extends AbstractEntity {

    @Basic(optional = false)
    @Column(nullable = false)
    private boolean isRent;
}

@Repository
public class BikeDao extends BaseDao<Bike> {

    public BikeDao() {
        super(Bike.class);
    }

    public List<Bike> findRent() {
        return em.createNamedQuery("Bike.findRent", Bike.class).getResultList();
    }
}

And after I run my Spring app and through Postman try to post Bike data. But,

Caused by: org.eclipse.persistence.exceptions.EntityManagerSetupException: 
Exception Description: Deployment of PersistenceUnit [default] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT b FROM Bike b WHERE NOT b.isRemoved AND b.isRent]. 
[47, 55] The right expression is not a valid expression.

What does it mean? I cannot found out what is wrong.

According to the JPA specification (4.5 WHERE Clause):

A WHERE clause is defined as follows:

where_clause::= WHERE conditional_expression

And in conditional expression you can use only combinations of available simple expression such as comparison, between, in, like, null comparison (aka is null), empty collection, collection member, exist expressions.

So, you should correct your query in the following way:

SELECT b FROM Bike b WHERE b.isRemoved = false AND b.isRent = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM