débutant createQuery [Glassfish]
Bonjour,
Je début dans les Ejb et les web service. Je suis en train réaliser une conection à la base de donner permettant aux utilisateurs de se connecter sur une page jsf.
ma page index.xhtml:
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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:c="http://java.sun.com/jstl/core" >
<head>
<title>TODO supply a title</title>
</head>
<body>
<ui:composition template="/template.xhtml">
<ui:define name="content">
<h:form>
<h1>Test</h1>
<p>Hello World !</p>
<h:messages showDetail="false" errorClass="error" fatalClass="fatal" />
<table class="identForm" >
<tr>
<td class="form" >
<label><h:outputText value="Login :" /></label>
</td>
<td class="form" >
<h:inputText value="#{AuthentificationController.login}" required="true" label="Login" size="16" maxlength="16" />
</td>
</tr>
<tr>
<td class="form" >
<label><h:outputText value="Mot de passe :" /></label>
</td>
<td class="form" >
<h:inputSecret value="#{AuthentificationController.password}" required="true" label="Mot de passe" size="16" maxlength="16" />
</td>
</tr>
<tr>
<td class="form center" colspan="2" >
<h:commandButton value="S'identifier" action="#{AuthentificationController.login}" />
</td>
</tr>
</table>
</h:form>
</ui:define>
</ui:composition>
</body>
</html> |
ma classe anthentifiation:
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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.eqx.logdstage.managedBean;
import com.eqx.logdstage.ejb3.entity.User;
import com.eqx.logdstage.ejb3.session.LogDstageSessionBeanLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
/**
*
* @author LoReL
*/
public class AuthentificationController {
private User user;
private String login;
private String password;
@EJB
private LogDstageSessionBeanLocal<User> ufl;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login() {
user = ufl.getUserWithLoginAndPassword(login, password);
if(user==null) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Vous n'avez pas été identifié", "Vous n'avez pas été identifié");
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
return "";
} else {
login="";
password="";
return "goToLogger";
}
}
public String logout() throws IOException {
user = null;
//FacesContext context = FacesContext.getCurrentInstance();
//ExternalContext ec = context.getExternalContext();
//final HttpServletRequest request = (HttpServletRequest) ec.getRequest();
//request.getSession(false).invalidate();
return "goToIndex";
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} |
et ma class LogDstageSessionBean
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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.eqx.logdstage.ejb3.session;
import com.eqx.logdstage.ejb3.entity.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
/**
*
* @author Administrateur
*/
@Stateless
public class LogDstageSessionBean<T> implements LogDstageSessionBeanLocal<T> {
@PersistenceContext(unitName="LogDstage-ejbPU")
EntityManager em;
public T create(T entity) {
em.persist(entity);
return entity;
}
public T edit(T entity) {
return em.merge(entity);
}
public void remove(T entity) {
T entityToDel=em.merge(entity);
em.remove(entityToDel);
}
public List<T> find(Class<T> entityDescription) {
return em.createQuery("select object(o) from "+ entityDescription.getSimpleName() + "as o").getResultList();
}
public T findByPrimaryKey(Class<T> entityDescription, Object id){
return em.find(entityDescription,id);
}
public User getUserWithLoginAndPassword(String login, String password)
{
Query q = em.createQuery("select object(o) from `user` as o where o.login= :id");
q.setParameter("id",login);
List<User> l=q.getResultList();
if(l.size()>0)
{
User user = l.get(0);
if(user.getMdp().equals(password))
{
return user;
}
}
return null;
}
} |
J'ai généré mes class entity automatiquement à l'aide de ma base de données.
Mes fichier .xml ont l'air de bien fonctionner car quand je modifie la foncion getUserWithLoginAndPassword par
Code:
1 2 3 4 5
| public User getUserWithLoginAndPassword(String login, String password)
{
return null;
} |
Je ne peux pas m'identifier mais je n'est plus aucune erreur.
Le message que je reçoit lorsque que j'essaye de me connecter avec le bon ou le mauvais mdp est: javax.faces.FacesException: #{AuthentificationController.login}: javax.ejb.EJBException
Je pense que mon souci vient de ma base de données ou de mes class entity. Mais je ne parvient pas à trouver :?
Edit: ça vien bien du createQuery de cette fonction, mais pourquoi elle marche pas 8O?
Je reçoit cette erreur à la console:
Caused by: Exception [TOPLINK-8034] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.EJBQLException
Exception Description: Error compiling the query [select object(o) from user as o where o.login= :id]. Unknown abstract schema type [user].
personne pour m'orienter?? :cry:
:roll: