亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java - 測試的時候出現錯誤,幫忙看下怎么回事。

瀏覽:98日期:2023-11-27 11:42:53

問題描述

這是錯誤提示

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$1.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$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.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)

測試類TestUsersDAOImpl。

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)); }}

自定義session工廠MyHibernateSessionFactory。

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;} }}

UsersDAOImpl。

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 stubTransaction 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; }} }}

問題解答

回答1:

看看是不是jar包沖突了

回答2:

MyHibernateSessionFactory.getSessionFactory(MyHibernateSessionFactory.java:20)20行類轉換錯誤。。。

回答3:

sessionFacotry = config.buildSessionFactory(serviceRegistry); 個人感覺是這里出錯,確定下config 和 serviceRegistry 這兩個獲取有沒有問題回答4:

感謝各位大神回答,問題解決了。換成SQL查詢就沒問題。

標簽: java
主站蜘蛛池模板: 免费一级毛片清高播放 | 国产精品va欧美精品 | 黄色在线视频网址 | 国产欧美日韩综合精品无毒 | 一级全黄色毛片 | 欧美日韩综合精品一区二区三区 | 亚洲欧美另类日韩 | 国产成人在线观看免费网站 | 欧美3p在线观看 | 1024人成网站色 | 91青青| 亚洲欧美日韩另类在线一 | 日本中文字幕乱码免费 | 草草国产 | 免费高清特级毛片 | 亚洲免费午夜视频 | wwwwww黄| 含羞草麻豆 | 久久人人网 | 国产精选自拍 | 国产青青在线 | 91在线|亚洲 | 日本xxx高清免费视频 | 全黄色毛片 | 成年啪啪网站免费播放看 | 日韩 国产 欧美视频一区二区三区 | 午夜视| 国产高清在线视频 | 亚洲欧美一区二区三区四区 | 薰衣草视频高清在线观看免费 | 一区二三区国产 | 亚洲欧美国产高清va在线播放 | 欧美日韩综合视频 | 五月天婷婷一区二区三区久久 | 黄色a三级三级三级免费看 黄色a三级免费看 | 欧美日韩无线码免费播放 | aaa在线 | 欧美精品综合 | 在线观看一区二区三区视频 | 国产免费私拍一区二区三区 | 在线观看免费高清激情爱爱 |