哪里錯(cuò)了???
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)
2017-02-28
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(){
//創(chuàng)建配置對象
Configuration config = new Configuration().configure();
? //創(chuàng)建服務(wù)注冊對象
//serviceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(config.getProperty());
//這里有錯(cuò)誤,轉(zhuǎn)換不了,用了強(qiáng)制轉(zhuǎn)換
ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
? //創(chuàng)建會(huì)話工廠對象
sessionFactory =config.buildSessionFactory(serviceRegistry);
//會(huì)話對象
session = sessionFactory.openSession();
//開啟事務(wù)
? ? ? transaction = session.beginTransaction();
}
@After
public void destory(){
transaction.commit();//提交事務(wù)
session.close(); ? ? //關(guān)閉會(huì)話
sessionFactory.close();//關(guān)閉會(huì)話工廠
}
@Test
public void testSaveStudents(){
? ?//生成學(xué)生對象
Students s = new Students(1,"張三豐","男",new Date(),"武當(dāng)山");
session.save(s);//保存對象進(jìn)入數(shù)據(jù)庫
}
}
2017-02-28
貼下源碼部分吧,這個(gè)分析不全