Java - Une erreur s'est produite lors du test. Veuillez m'aider à découvrir ce qui s'est passé.
某草草
某草草 2017-05-17 10:08:23
0
4
521

Ceci est un message d'erreur

java.lang.ClassCastException: org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl cannot be cast to org.hibernate.boot.registry.StandardServiceRegistry
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:655)
    at db.MyHibernateSessionFactory.getSessionFactory(MyHibernateSessionFactory.java:20)
    at service.impl.UsersDAOImpl.usersLogin(UsersDAOImpl.java:21)
    at service.impl.TestUsersDAOImpl.testUsersLogin(TestUsersDAOImpl.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access
package service.impl;

import org.junit.Test;

import entity.Users;
import junit.framework.Assert;

public class TestUsersDAOImpl {

    @Test
    public void testUsersLogin(){
        Users u = new Users(1,"kelvin","kelvin");
        UsersDAOImpl udao = new UsersDAOImpl();
        Assert.assertEquals(true, udao.usersLogin(u));
    }
}
0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Classe de test TestUsersDAOImpl.

package db;

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

public class MyHibernateSessionFactory {

    private static SessionFactory sessionFacotry;//会话工厂属性
    //构造方法私有化,保证单例模式
    private MyHibernateSessionFactory(){
        
    }
    //公有的静态方法,活得会话工厂对象
    public static SessionFactory getSessionFactory(){
        if(sessionFacotry==null){
            Configuration config = new Configuration().configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).getBootstrapServiceRegistry();
            sessionFacotry = config.buildSessionFactory(serviceRegistry);
            return sessionFacotry;
        }
        else{
            return sessionFacotry;
        }
    }
}

Personnalisez la fabrique de sessions MyHibernateSessionFactory.

package service.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import db.MyHibernateSessionFactory;
import entity.Users;
import service.UsersDAO;

public class UsersDAOImpl implements UsersDAO{

    @Override
    public boolean usersLogin(Users u) {
        // TODO Auto-generated method stub
        Transaction tx = null;
        String hql = "";
        try{
            Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            hql = "from Users where username=? and password=? ";
            Query query = session.createQuery(hql);
            query.setParameter(0, u.getUsername());
            query.setParameter(1, u.getPassword());
            List list = query.list();
            tx.commit();
            if(list.size()>0){
                return true;
            }
            else{
                return false;
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            return false;
        }
        finally{
            if(tx!=null){
                //tx.commit();
                tx=null;
            }
        }
    }

}

UtilisateursDAOImpl.

rrreee
某草草
某草草

répondre à tous(4)
我想大声告诉你

Vérifiez si le package jar est en conflit

小葫芦

MyHibernateSessionFactory.getSessionFactory(MyHibernateSessionFactory.java:20)
Erreur de conversion de classe de 20 lignes. . .

洪涛
sessionFacotry = config.buildSessionFactory(serviceRegistry); 个人感觉是这里出错,确定下config 和 serviceRegistry 这两个获取有没有问题
左手右手慢动作

Merci pour vos réponses, le problème est résolu. Ce n'est pas un problème de passer à la requête SQL.

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal