0

我正在尝试使用以下文件开发一个简单的登录过程:

用户.java

package ttfjsf.backend;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="utenti")
public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="username")
    String uname;

    @Column(name="password")
    String pwd;
}

用户存储库.java

package ttfjsf.backend;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import org.hibernate.Session;

import lombok.Data;

import ttfjsf.backend.User;

@Data
@ManagedBean(name = "userRepository")
@ApplicationScoped
public class UserRepository {
    public User getUserByUsername(String username) {
        Session session = null;
        User u = null;

        try {
            session = HibernateUtil.getSession();
            u = session.get(User.class, username);
        } catch(Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (session != null)
                    session.close();
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }

        return u;
    }
}

HibernateUtil.java

package ttfjsf.backend;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import ttfjsf.backend.User;

public class HibernateUtil {

    private static SessionFactory sessionFactory = null;

    static {
        try {
            loadSessionFactory();
        } catch(Exception e) {
            System.err.println("Exception while initializing HibernateUtil");
            e.printStackTrace();
        }
    }

    public static void loadSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        configuration.addAnnotatedClass(User.class);
        ServiceRegistry servReg = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(servReg);
    }

    public static Session getSession() throws HibernateException {
        Session retSession = null;
        try {
            retSession = sessionFactory.openSession();
        } catch(Throwable t) {
            System.err.println("Exception while getting session...");
        }
        if (retSession == null) {
            System.err.println("Session is discovered null");
        }
        return retSession;
    }

}

休眠.cfg.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>   
      <property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name = "hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>      
      <property name = "hibernate.connection.url">jdbc:mysql://localhost/carrello_ttf?serverTimezone=CET</property>
      <property name = "hibernate.connection.username">filippi</property>      
      <property name = "hibernate.connection.password">password</property>  
   </session-factory>
</hibernate-configuration>

登录BackingBean.java

package ttfjsf.frontend;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import lombok.Data;
import ttfjsf.backend.Carrello;
import ttfjsf.backend.User;
import ttfjsf.backend.UserRepository;

@Data
@ManagedBean(name="login")
@RequestScoped
public class LoginBackingBean {

    private String username, pass, error = null;

    @ManagedProperty(value = "#{mySession}")
    private MySession s;

    @ManagedProperty(value = "#{userRepository}")
    private UserRepository userRepo;

    @ManagedProperty(value = "#{carrello}")
    private Carrello c;

    public String logout() {

        s.setUser(null);

        return "login";
    }

    public String doLogin() {

        User u = userRepo.getUserByUsername(username);

        if (u!=null && pass.equals(u.getPwd())) {

            c.init();
            error = null;
            s.setUser(u);

            return "home";
        }

        error = "Login errato";

        return "login";                 
    }

    public boolean getShowError() {
        return error != null;
    }

}

登录.xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://xmlns.jcp.org/jsf/passthrough">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Home</title>
</head>
<body>
    <div class="main-content">
        <div class="loginForm">
            <h:form>
                <h:outputLabel rendered="#{login.getShowError()}" value="#{login.error}"><hr/></h:outputLabel>
                <h:inputText value="#{login.username}" style="border: none; border-bottom: 1px solid black; outline: none; padding-bottom: 5px;" p:placeholder="Username" autocomplete="off"></h:inputText>
                <br/><br/>
                <h:inputSecret value="#{login.pass}" style="border: none; border-bottom: 1px solid black; outline: none; padding-bottom: 5px;" p:placeholder="Password"></h:inputSecret>
                <br/><br/><br/>
                <h:commandButton value="LOGIN" styleClass="myButton" action="#{login.doLogin}"></h:commandButton>
            </h:form>       
        </div>
    </div>
</body>
</html>

当我单击登录时,服务器会出现此错误

Type Exception Report

Message java.lang.IllegalArgumentException: Unable to locate persister: ttfjsf.backend.User

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: java.lang.IllegalArgumentException: Unable to locate persister: ttfjsf.backend.User
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: Unable to locate persister: ttfjsf.backend.User
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.IllegalArgumentException: Unable to locate persister: ttfjsf.backend.User
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3333)
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3274)
    ttfjsf.backend.UserRepository.get(UserRepository.java:33)
    ttfjsf.frontend.LoginBackingBean.doLogin(LoginBackingBean.java:37)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

org.hibernate.UnknownEntityTypeException: Unable to locate persister: ttfjsf.backend.User
    org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:721)
    org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2936)
    org.hibernate.internal.SessionImpl.access$1800(SessionImpl.java:194)
    org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2665)
    org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2648)
    org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1130)
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3297)
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3274)
    ttfjsf.backend.UserRepository.get(UserRepository.java:33)
    ttfjsf.frontend.LoginBackingBean.doLogin(LoginBackingBean.java:37)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

我也尝试不使用注释(@Entity,@Table ecc。)并User.hbm.xml像这样创建:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="ttfjsf.backend.User" table="utenti">
        <id name="uname" column="username" type="string" />
        <property name="pwd" column="password" type="string" />
    </class>
</hibernate-mapping>

然后hibernate.cfg.xml看起来像这样:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>   
      <property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name = "hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>      
      <property name = "hibernate.connection.url">jdbc:mysql://localhost/carrello_ttf?serverTimezone=CET</property>
      <property name = "hibernate.connection.username">filippi</property>      
      <property name = "hibernate.connection.password">password</property>   
      <mapping resource = "ttfjsf/backend/User.hbm.xml" />  
   </session-factory>
</hibernate-configuration>

但它给出了完全相同的错误。

我正在使用 Hibernate 5.4.12.Final。

有任何想法吗?

4

0 回答 0