java提示单个cass怎么办,求助解决hibernate报错,java.lang.casscastexception

确定一下session.beginTransaction();返回的 Transaction对象是不是javax.transaction.Transaction的实例。

我记不清了,这里也没hibernate的环境....

baggio0105 老兄的建议是对的,我发现确是我倒入的环境包有点问题;

现在换成:import net.sf.hibernate.Transaction;

就可以了,但是出现了一个新的问题:

eclipse下运行

控制台报错如下:

net.sf.hibernate.exception.SQLGrammarException: Could not save object

at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:58)

at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)

at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4131)

at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:794)

at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)

at org.redsaga.HibernateTest.testInsert(HibernateTest.java:51)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Caused by: java.sql.SQLException: ORA-00942: 表或视图不存在

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:180)

at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)

at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543)

at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1451)

at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:651)

at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2117)

at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2331)

at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:422)

at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:366)

at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:94)

at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)

at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:776)

... 18 more

junit测试报错情况:

junit.framework.AssertionFailedError: Could not save object

at junit.framework.Assert.fail(Assert.java:47)

at org.redsaga.HibernateTest.testInsert(HibernateTest.java:57)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

上面的报错看不太懂啊,大家也来帮忙解决一下了,多谢了!

HibernateTest.java最新的源代码

package org.redsaga;

import java.io.File;

import junit.framework.Assert;

import junit.framework.TestCase;

import net.sf.hibernate.HibernateException;

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.Transaction;

import net.sf.hibernate.cfg.Configuration;

import net.sf.hibernate.collection.List;

import org.redsaga.quickstart.Tuser;

public class HibernateTest extends TestCase {

Session session = null;

protected void setUp(){

try{

final File fileurl = new File("D:/bea/user_projects/applications/StrutsStudy/DefaultWebApp/WEB-INF/classes/hibernate.cfg.xml");

Configuration config =  new Configuration().configure(fileurl);

SessionFactory sessionFactory = config.buildSessionFactory();

//System.out.println("=========="+config+"==============");

session = sessionFactory.openSession();

}catch(HibernateException e){

e.printStackTrace();

}

}

protected void tearDown() throws Exception {

session.close();

}

public void testInsert(){

Transaction tran = null;

try{

tran = session.beginTransaction();

//System.out.println("=========="+tran+"==============");

Tuser user = new Tuser();

user.setName("emma");

session.save(user);

session.flush();

tran.commit();

Assert.assertEquals(user.getId().intValue()>0,true);

}catch(HibernateException e){

e.printStackTrace();

Assert.fail(e.getMessage());

if(tran!=null){

try{

tran.rollback();

}catch (HibernateException e1) {

// TODO 自动生成 catch 块

e1.printStackTrace();

}

}

}

}

public void testSelect(){

String hql = "from Tuser where name = 'emma'";

//System.out.println("=========="+hql+"==============");

List userList;

try {

userList = (List) session.createQuery(hql).list();

Tuser user = (Tuser)userList.get(0);

Assert.assertEquals(user.getName(), "emma");

} catch (HibernateException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

Assert.fail(e.getMessage());

}

}

}

[本帖最后由 lht811008 于 2008-2-20 08:34 编辑]

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值