简体   繁体   中英

EclipseLink JPA CriteriaQuery is creating an erroneous SQL query

I 'm trying to implement a generic filter system in Primefaces datatable using LazyDataModel for any entity but is giving me problems when trying to search fields involved in one to many relationships as it generates erroneous SQL queries, my code is as follows i will skip some parts not relevant to keep it as simple as posible:

The entitys are Client and Fault:

@Entity
@Table(name = "ges_fault")
public class Fault extends Item implements Serializable, ItemIf{

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id", nullable = true)
private Client client;

// Getters and setters

Client class:

@Entity
@Table(name="ges_client")
public class Client extends Item implements Serializable, ItemIf {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;


@OneToMany(cascade = CascadeType.ALL,mappedBy="client", fetch = FetchType.LAZY)
private List<Fault> faults;

// Getters and setters

To abstract the problem the final criteria query looks as follow:

CriteriaBuilder qb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Fault> c = qb.createQuery(Fault.class);
Root<Fault> root = c.from(Fault.class);
c.select(root);
List<Predicate> predicates = new ArrayList<Predicate>();
Expression<String> exp = root.get("client");
predicates.add(qb.like(exp,"%" + 1 + "%"));
c.where(predicates.toArray(new Predicate[]{}));
List<Fault>result = q.getResultList();

return result;

The error i'm getting is:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.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 'LIKE '%1%' AND (t0.ID = t1.client_id)) LIMIT 0, 12' at line 1

Error Code: 1064 Call:

SELECT t1.ID AS a1, t1.NOMBRE AS a8, t1.client_id AS a9 FROM ges_client t0, ges_fault t1 WHERE ( LIKE ? AND (t0.ID = t1.client_id)) LIMIT ?, ? bind => [3 parameters bound] Query: ReadAllQuery(referenceClass=Falta sql="SELECT t1.ID AS a1, t1.NOMBRE AS a8, t1.client_id AS a9 FROM ges_client t0, ges_falta t1 WHERE ( LIKE ? AND (t0.ID = t1.client_id)) LIMIT ?, ?") at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:682) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558) at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002) at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570) at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)

Because what I'm trying to do to do I can not use this kind of querys:

Expression<String> exp = root.get(Fault_.client);

so it is not a possible solution.

I hope someone can help me.

You probably want to filter on the Client's name property. In this case, use one of the following syntaxes, depending if you are using typesafe MetaModel or not:

Path<Client> client = root.get(Fault_.client);
Path<String> exp = client.get(Client_.name);  

or:

Path<Client> client = root.get("client");
Path<String> exp = client.get("name");  

In both situations you can avoid specifying the class in Path, or you can even avoid instantiating explicitly the client variable at all:

Path exp = root.get(Fault_.client).get(Client_.name);

or

Path client = root.get(Fault_.client);
Path exp = client.get(Client_.name);  

are all legal, as well as their equivalent using attribute names "client" and "name" instead of metamodel.

Afterwards, proceed as you have already done for creating the Predicate expression.

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