参照视频写完配置文件,映射文件,持久类,还有数据库表之后想小试牛刀,点Test方法运行为JUNIT的频繁出错,到处碰壁呀,求救求指点!!!持久类:package po;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
public User(){
}
public User(int i){
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}配置文件:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc.oracle.thin:@localhost:1521:orcl<!-- :///hibernate?useUnicode=trup&characterEncoding=UTF-8 --></property>
<property name="connection.username">MKADMIN</property>
<property name="connection.password">MK123456</property>
<!-- 辅助参数 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="htm2ddl.auto">create</property><!-- DDL语句生成策略 -->
<property name="current_session_context_class">true</property>
<!-- 映射文件 -->
<mapping resource="User.hbm.xml" />
</session-factory>
</hibernate-configuration>映射文件:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="po.User" table="users">
<id name="userName" type="java.lang.String">
<column name="username" length="10" />
</id>
</class>
</hibernate-mapping>测试类: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;
import po.User;
public class UserTest {
private SessionFactory sf=null;
private Session session=null;
private Transaction tran=null;
@Before
public void init(){
//创建配置对象
Configuration cfg=new Configuration().configure();
//创建注册服务对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
//创建工厂对象
sf=cfg.buildSessionFactory(serviceRegistry);
//创建会话对象
session=sf.getCurrentSession();
//开启事务
tran=session.beginTransaction();
}
@After
public void destroy(){
//释放资源之前需先提交事务
tran.commit();
session.close();
sf.close();
}
@Test
public void userTest(){
//生成用户对象
User su=new User();
su.setUserName("zhangsanfeng");
session.save(su);
}
}数据库SQL语句-- Create tablecreate table USERS( username VARCHAR2(10) not null constraint PK_USERS primary key)控制台信息:2017-5-3 18:51:04 org.hibernate.annotations.common.Version <clinit>INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}2017-5-3 18:51:04 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {4.2.4.Final}2017-5-3 18:51:04 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found2017-5-3 18:51:04 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist2017-5-3 18:51:04 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml2017-5-3 18:51:04 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml2017-5-3 18:51:04 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!2017-5-3 18:51:04 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: User.hbm.xml2017-5-3 18:51:04 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!2017-5-3 18:51:04 org.hibernate.cfg.Configuration doConfigureINFO: HHH000041: Configured SessionFactory: null2017-5-3 18:51:04 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)2017-5-3 18:51:04 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 202017-5-3 18:51:04 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000006: Autocommit mode: false2017-5-3 18:51:04 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc.oracle.thin:@localhost:1521:orcl]2017-5-3 18:51:04 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000046: Connection properties: {user=MKADMIN, password=****}JUNIT 错误信息:java.lang.NullPointerException at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:214) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776) at UserTest.init(UserTest.java:23) 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:597) 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:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
添加回答
举报
0/150
提交
取消