Problème d'écriture d'une requête:QueryParameterException
Bonjour,
J'ai un soucis avec la méthode suivante :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
private String jpql = "SELECT NEW Project(p.name, p.company, p.technology, p.comment) FROM User u JOIN u.projects p";
public List<Project> findUserProjectByCriteria(HashMap<String,String> columnValues, String username) throws FicheProjetException {
if(StringUtil.isEmpty(username)) {
throw new FicheProjetException(FicheProjetError.USER_ID_IS_EMPTY);
}
String criteria = jpql + " WHERE u.username = :username AND";
for(String key : columnValues.keySet()) {
criteria += " p."+ key +" = :"+ key + " AND";
}
criteria = criteria.substring(0,(criteria.length()>=1)? criteria.length()-3 : 0) + " ORDER BY p.daydate";
System.out.println(criteria);
//SELECT NEW Project(p.name, p.company, p.technology, p.comment) FROM User u JOIN u.projects p WHERE u.username = :username AND p.comment = :comment ORDER BY p.daydate
List<Project> projects = new ArrayList<Project>();
try {
Query query = getEntityManager().createQuery(jpql, Project.class);
query.setParameter("username", username);
for(String key : columnValues.keySet()) {
query.setParameter(key, columnValues.get(key));
}
projects = query.getResultList();
} catch (PersistenceException ex) {
throw new FicheProjetException(FicheProjetError.DAO_FIND_LIST_ERROR, ex);
}
return projects;
} |
Lors de mon test unitaire, j'ai cette erreur qui est renvoyée :
java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [username].
Classe User:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/**
* Entity users from database
*/
@Entity
@Table(name="users")
@NamedQuery(name = "findUsersByAuthority",
query = "SELECT u FROM User u JOIN u.authorities a WHERE a.authority = :authority")
public class User extends AbstractEntity<String> {
/**
* Attributes from entity users
*/
@Id
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private boolean enabled;
@Column(name = "first_name", nullable = false)
private String firstname;
@Column(name = "last_name", nullable = false)
private String lastname;
@Column(nullable = false, unique = true)
private String email;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "iduser", referencedColumnName = "username")
private List<Authority> authorities;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "users_project",
joinColumns = {@JoinColumn(name="iduser", referencedColumnName = "username")},
inverseJoinColumns = {@JoinColumn(name="idproject", referencedColumnName = "name")})
private List<Project> projects;
/**
* Methods from entity users
*/
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Authority> getAuthority() {
if(authorities == null) {
return new ArrayList<Authority>();
}
return authorities;
}
public void setAuthority(List<Authority> authorities) {
this.authorities = authorities;
}
public List<Project> getProject() {
if(projects == null) {
return new ArrayList<Project>();
}
return projects;
}
public void setProject(List<Project> projects) {
this.projects = projects;
}
/**
* Method from Superclass AbstractEntity
*/
@Override
public String toString() {
return "User [username=" + username + ", password=" + password
+ ", enabled=" + enabled + ", firstname=" + firstname
+ ", lastname=" + lastname + ", email=" + email
+ ", authorities=" + authorities + "]";
}
@Override
protected String getId() {
return username;
}
} |
Et Classe Project:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/**
* Entity project from database
*/
@Entity
@Table(name = "project")
@NamedQuery(name = "findUserProject", query = "SELECT NEW Project(p.name, p.company, p.technology, p.comment) FROM User u JOIN u.projects p WHERE u.username = :username ORDER BY p.daydate")
public class Project extends AbstractEntity<String> {
public Project() {
}
public Project(String name, String company, String technology,
String comment) {
this.name = name;
this.company = company;
this.technology = technology;
this.comment = comment;
}
/**
* Attributes from entity project
*/
@Id
private String name;
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false, updatable = false)
private Date daydate;
@Column(nullable = false)
@Basic(fetch = FetchType.LAZY, optional = false)
private String company;
@Column(nullable = false)
private String technology;
@Column(nullable = false)
private String activity;
@Column(name = "initial_need", nullable = false)
private String initialneed;
@Column(nullable = false)
private String environment;
@Column(nullable = false)
private Integer volume;
@Column(nullable = false)
private Integer template;
@Column(nullable = false)
private String comment;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "idproject", referencedColumnName = "name")
private List<Picture> pictures;
/**
* Methods from entity project
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDaydate() {
return daydate;
}
public void setDaydate(Date daydate) {
this.daydate = daydate;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getTechnology() {
return technology;
}
public void setTechnology(String technology) {
this.technology = technology;
}
public String getActivity() {
return activity;
}
public void setActivity(String activity) {
this.activity = activity;
}
public String getInitialneed() {
return initialneed;
}
public void setInitialneed(String initialneed) {
this.initialneed = initialneed;
}
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
public Integer getVolume() {
return volume;
}
public void setVolume(Integer volume) {
this.volume = volume;
}
public Integer getTemplate() {
return template;
}
public void setTemplate(Integer template) {
this.template = template;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public List<Picture> getPictures() {
if (pictures == null) {
return new ArrayList<Picture>();
}
return pictures;
}
public void setPictures(List<Picture> pictures) {
this.pictures = pictures;
}
@PrePersist
protected void onCreate() throws ParseException {
this.daydate = new Date();
}
/**
* Method from Superclass AbstractEntity
*/
@Override
public String toString() {
return "Project [name=" + name + ", date=" + daydate + ", company="
+ company + ", technology=" + technology + ", activity="
+ activity + ", initialneed=" + initialneed + ", environment="
+ environment + ", volume=" + volume + ", template=" + template
+ ", comment=" + comment + ", pictures=" + pictures + "]";
}
@Override
protected String getId() {
return name;
}
} |
J'ai tester avec des paramètres ?1 sa me donne une autre erreur. Je me demandai si il était réellement possible de faire cette requete avec une createQuery ?? La même requête fonctione en @NamedQuery sans la clause where.
Si quelqu'un à une idée, je suis débutant en Jpa....
Merci encore