哪里错了???

来源:1-11 通过Hibernate API编写访问数据库的代码

qq_孤独的根号三_3

2017-02-27 13:22

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)


写回答 关注

2回答

  • qq_孤独的根号三_3
    2017-02-28 12:43:01

    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);//保存对象进入数据库

    }

    }


  • 会笑的南瓜
    2017-02-28 01:34:10

    贴下源码部分吧,这个分析不全

    qq_孤独的... 回复会笑的南瓜

    可是没有用到b变量啊

    2017-02-28 12:18:47

    共 3 条回复 >

Hibernate初探之单表映射

Java持久化框架Hibernate入门教程,掌握Hibernate基本概念

74808 学习 · 793 问题

查看课程

相似问题