为了账号安全,请及时绑定邮箱和手机立即绑定

是不是导入的包错了??

源码:

import java.util.Date;

//import javax.transaction.Transaction;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;


import org.junit.After;

import org.junit.Before;

import org.junit.Test;




//测试类


public class StudentsTest {


private SessionFactory sessionFactory;

private Session session;

private Transaction transaction;

@Before

public void init(){

//创建配置对象

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

  //创建服务注册对象

//serviceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(config.getProperty());

//这里有错误,转换不了,用了强制转换

ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();

  //创建会话工厂对象

sessionFactory =config.buildSessionFactory(serviceRegistry);

//会话对象

session = sessionFactory.openSession();

//开启事务

      transaction = session.beginTransaction();

}

@After

public void destory(){

transaction.commit();//提交事务

session.close();     //关闭会话

sessionFactory.close();//关闭会话工厂

}

@Test

public void testSaveStudents(){

   //生成学生对象

Students s = new Students(1,"张三丰","男",new Date(),"武当山");

session.save(s);//保存对象进入数据库

}

}

报错:


java.lang.Error: Unresolved compilation problems: 

Cannot instantiate the type Configuration

Syntax error, insert ";" to complete BlockStatements

SerceRegistry cannot be resolved to a type

The method getproperties() is undefined for the type Configuration

Syntax error, insert ":: IdentifierOrNew" to complete ReferenceExpression

Syntax error, insert ";" to complete Statement

b cannot be resolved or is not a field

Type mismatch: cannot convert from org.hibernate.Transaction to javax.transaction.Transaction


at StudentsTest.init(StudentsTest.java:23)

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

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

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.RunBefores.evaluate(RunBefores.java:27)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)

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:678)

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

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


正在回答

1 回答

Transaction重新写一下

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

是不是导入的包错了??

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信