I have built an application using JPA in an EJB container. Here's my code
@PersistenceContext(unitName = "damate-pu")
private EntityManager em;
@Override
public Workspace find(String username, String path) {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Workspace> criteriaQuery = criteriaBuilder.createQuery(Workspace.class);
Root<Workspace> from = criteriaQuery.from(Workspace.class);
Predicate condition = criteriaBuilder.equal(from.get("Username"), username);
Predicate condition2 = criteriaBuilder.equal(from.get("Path"), path);
Predicate condition3 = criteriaBuilder.and(condition, condition2);
criteriaQuery.where(condition3);
Query query = em.createQuery(criteriaQuery);
return (Workspace) query.getSingleResult();
}
When I try to run this method from a webservice I get the following error:
java.lang.IllegalArgumentException: The attribute [Username] from the managed type....
What can be the problem? I think I have a problem with from.get("Username")
...
What do you think? And how to fix it?
Edit: Workspace.java
package com.ubb.damate.model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.util.Set;
/**
* The persistent class for the workspace database table.
*
*/
@Entity
@Table(name="workspace")
public class Workspace implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="WorkspaceId", unique=true, nullable=false)
private int workspaceId;
@Temporal( TemporalType.DATE)
@Column(name="CreationDate", nullable=false)
private Date creationDate;
@Lob()
@Column(name="Path", nullable=false)
private String path;
@Column(name="Username", nullable=false, length=20)
private String username;
//bi-directional many-to-one association to Project
@OneToMany(mappedBy="workspace")
private Set<Project> projects;
public Workspace() {
}
public int getWorkspaceId() {
return this.workspaceId;
}
public void setWorkspaceId(int workspaceId) {
this.workspaceId = workspaceId;
}
public Date getCreationDate() {
return this.creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public Set<Project> getProjects() {
return this.projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
}
CriteriaBuilderJPA interfaceUsed to construct criteria queries, compound selections, expressions, predicates, orderings. See JavaDoc Reference Page... interface serves as the main factory of criteria queries and criteria query elements. It can be obtained either by the EntityManagerFactory. persistence.
The CriteriaBuilder can be used to restrict query results based on specific conditions, by using CriteriaQuery where() method and providing Expressions created by CriteriaBuilder.
Using the CriteriaBuilder, we create a CriteriaQuery<Book>, which describes what we want to do in the query. It also declares the type of a row in the result. With CriteriaQuery<Book>, we declare the starting point of the query (Book entity), and store it in the book variable for later use.
public interface CriteriaBuilder. Used to construct criteria queries, compound selections, expressions, predicates, orderings. Note that Predicate is used instead of Expression<Boolean> in this API in order to work around the fact that Java generics are not compatible with varags.
When building your criteria query (or building jpql in a string), you want to use the entity property names, not the column names. Your database column is named "Username", but the property of the Workspace object is "username" without the capital U.
Have you tried using metamodel?
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
Metamodel m = em.getMetamodel();
EntityType<Workspace> WS = m.entity(Workspace.class);
CriteriaQuery<Workspace> criteriaQuery = criteriaBuilder.createQuery(Workspace.class);
Root<Workspace> from = criteriaQuery.from(Workspace.class);
Predicate condition = criteriaBuilder.equal(from.get(WS.username), username);
http://download.oracle.com/javaee/6/tutorial/doc/gjivm.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With